gary_w
June 23, 2011, 10:34am
1
Hello sed gurus. I am using ksh on Sun and have a file created by concatenating several other files. All files contain header rows. I just need to keep the first occurrence and remove all other header rows.
header for file
1111
2222
3333
header for file
1111
2222
3333
header for file
1111
2222
3333
Should look like:
header for file
1111
2222
3333
1111
2222
3333
1111
2222
3333
I know how to remove lines matching a pattern, but I am brain-locking on the pattern needed to skip the first occurrence.
Removes lines starting with "header"
sed '/^header/d' filename
Thanks for any help!
Gary
panyam
June 23, 2011, 10:42am
2
an awk one:
awk '/header/&&c++>0 {next} 1' input_file
1 Like
ctsgnb
June 23, 2011, 10:49am
3
sed '1!{/^header/d;}' inputfile
or
sed '2,${/^header/d;}' inputfile
1 Like
awk ' /^header/ && FNR > 1 {next} {print $0} ' infile > newfile
1 Like
gary_w
June 23, 2011, 11:19am
5
Thanks ctsgnb I could net get that syntax for specifying the range to operate on.
Thanks to all for the different methods!
Gary
herot
October 6, 2011, 11:50am
6
Is there a way to do this for multiple regex's? I have got part of it with sed:
sed '/pattern1/d;/pattern2/d;/pattern3/d;/pattern4/d' file > filetest
However, I didn't figure out how to skip the first instance of each pattern...
This one seems to double the data in the file...:
awk '/pattern1/&&c++>0 {next} 1;/pattern2/&&c++>0 {next} 1' file > testfile
awk '/header/&&c++ {next} 1'
herot
October 7, 2011, 9:05am
8
but how do allow for more than one regex? /header/;/header2/;/header3/ ??
awk 'c++&&(/pattern1/||/pattern2/||/pattern3/) {next} 1'