|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
sed regexp question for deleting lines
I have a file that is being piped through sed and want to remove lines the contain the pattern ABC if the pattern falls within the 1st 20 characters of the line. If the line does not contain the pattern or if the pattern falls after the 20th character, the line goes to standardout.
The statement I've tried to use is: cat file1 | sed '/ABC/d' | "another UNIX utility" I'm open to using grep instead of sed, if anybody can help with the regexp to get it to just search the 1st 20 characters for the pattern. grep -v ABC file1 | "another UNIX utility" Thanks for any suggestions, Don |
|
#2
|
|||
|
|||
|
I don't know how to do it in sed. Until someone answers you here's an awk method:
My file: Code:
line 01 andy line 02 black line 03 andy line 04 black line 05 andy line 06 black line 07 andy line 08 black line 09 andy line 10 black line 11 andy line 12 black line 13 andy line 14 black line 15 andy line 16 black line 17 andy line 18 black line 19 andy line 20 black Code:
nawk -v LINETO=5 -v STRING=andy '{if(NR<=LINETO && index($0,STRING) >0)
{print $0" : this line matched"}
else
{print $0" : this line didnt match"}
}' myfile
line 01 andy : this line matched
line 02 black : this line didnt match
line 03 andy : this line matched
line 04 black : this line didnt match
line 05 andy : this line matched
line 06 black : this line didnt match
line 07 andy : this line didnt match
line 08 black : this line didnt match
line 09 andy : this line didnt match
line 10 black : this line didnt match
line 11 andy : this line didnt match
line 12 black : this line didnt match
line 13 andy : this line didnt match
line 14 black : this line didnt match
line 15 andy : this line didnt match
line 16 black : this line didnt match
line 17 andy : this line didnt match
line 18 black : this line didnt match
line 19 andy : this line didnt match
line 20 black : this line didnt match
|
|
#3
|
|||
|
|||
|
sauldls please note: cat|sed is for dos-experts
sed ... filename does it. sure sed in able to do it, my *nix is down at the moment, spontaneous i would say something like: sed -n '/.*\{20\}ABC/p' filename i repeat NOT tested, man sed can help you |
|
#4
|
|||
|
|||
|
Thanks guggach! That gets me closer. In the regexp you suggested: '/.*\{20\}ABC/p', it works in cases where the string falls after the 20th character BUT I also need to print lines in which the string done not appear at all. It is only the lines that contain the string in the first 20 characters that I need to remove, all others need to be printed.
xxxxABCxxxxxxxxxxxxxxxxxxxxxxx (not printed) xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (printed) xxxxxxxxxxxxxxxxxxxxxxxxxABCxx (printed) xABCxxxxxxxxxxxxxxxxxxxxxxxxxx (not printed) Thanks, Don |
|
#5
|
|||
|
|||
|
sauldls
sure sed (and other tools) can easy do that i am actually on a stupid dos sys and have no access to my solaris sys (disk probls) so i cannot ... maybe y Q is not clean, try (just for fun) sed -n '/ABC/p' filename or clearify the Q if you only need the string 'ABC' try sed -n 's/\(.*\)\(ABC\)\(.*\)/\2/' Last edited by guggach : November 5th, 2004 at 06:24 AM. Reason: changes |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > sed regexp question for deleting lines |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|