Computing.Net > Forums > Unix > sed, deleting 2nd matching line

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

sed, deleting 2nd matching line

Reply to Message Icon

Name: WilliamRobertson
Date: October 4, 2006 at 04:42:48 Pacific
OS: Linux
CPU/Ram: 400/512
Comment:

I have a file where the heading line is duplicated within the file, for example:

HEADING
11111
2222222
HEADING
333

I need to delete all occurrences of the heading line after the first one, so that I am left with

HEADING
11111
2222222
333

I can't seem to get it in sed. In vi/vim/ed/ex it would be

2,$g/^HEADING/d

i.e. from line 2 to the end of the file, find occurrences of ^HEADING and delete them. However this syntax is not valid in sed:

$ sed '2,$g/^HEADING/d' testit.dat

sed: -e expression #1, char 5: Extra characters after command

Any ideas? It doesn't have to be sed, but the file is almost 4GB so I think vi and ed are out unless there is an option I'm missing.

Thanks



Sponsored Link
Ads by Google

Response Number 1
Name: lchi2000g
Date: October 4, 2006 at 07:17:33 Pacific
Reply:

It's not beautiful, but it works:

sed -e '2,$s/^HEADING$/non-existing-string/' -e '/^non-existing-string$/d' testit.dat

Luke Chi


0

Response Number 2
Name: James Boothe
Date: October 4, 2006 at 07:47:21 Pacific
Reply:

On line #1, just branch to the end.

sed -e 1b -e /^HEADING/d testit.dat


0

Response Number 3
Name: WilliamRobertson
Date: October 4, 2006 at 08:47:56 Pacific
Reply:

Thanks James, never knew about the b command.

Neat hack, Luke ;)



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: sed, deleting 2nd matching line

grep/sed to display following lines www.computing.net/answers/unix/grepsed-to-display-following-lines/4578.html

sed - delete duplicate lines www.computing.net/answers/unix/sed-delete-duplicate-lines/7507.html

Script to delete lines from a file www.computing.net/answers/unix/script-to-delete-lines-from-a-file/7408.html