Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

remove lines from second matched line (including) the the end of file 2

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL

input:
one
two
two
one
one
there
one

so lest say I am looking for "one" and starting from second occurence I want to remove to the end of file.

expected output to above example input:

one
two
 
Hi

From your description I understand this :
[pre]
one [gray]<--- 1st occurrence[/gray]
two
two
[red][black]one[/black][/red] [gray]<--- 2nd occurrence[/gray]
[red][black]one[/black][/red] [gray]|[/gray]
[red][black]there[/black][/red] [gray]|- remove these lines[/gray]
[red][black]one[/black][/red] [gray]<-'[/gray]
[/pre]
But then why the output contains a single "two" ?

Feherke.
[link feherke.github.com/][/url]
 
Something like this ?
awk '/one/{++n}n{if(n==1)print;else exit}' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
yes, you are right.

on expected output from the example should have been:

one
two
two
 
Hi

Ok. Then here is how I would do it :
Code:
sed '/one/{x;/^$/!Q;g}' /input/file
Tested with GNU [tt]sed[/tt].

Note that my solution is not extensible. If there are chances to ever change your mind and what to remove starting from the 3rd occurrence, then use PHV's Awk code.

Feherke.
[link feherke.github.com/][/url]
 
Tested with GNU sed
With legacy sed, the result is:
sed: Unrecognized command: /one/{x;/^$/!Q;g}

With GNU sed, I get:
[!]input:[/!]
one
two
two
 
Hi

Thanks you for the note on legacy [tt]sed[/tt] behavior.

But I definitely not understand what you mean in your second paragraph, especially the red line :
Code:
[blue]master #[/blue] awk '/one/{++n}n{if(n==1)print;else exit}' w5000.txt 
one
two
two

[blue]master #[/blue] sed '/one/{x;/^$/!Q;g}' w5000.txt
one
two
two

[blue]master #[/blue] diff <( awk '/one/{++n}n{if(n==1)print;else exit}' w5000.txt ) <( sed '/one/{x;/^$/!Q;g}' w5000.txt )

[blue]master #[/blue] diff -y <( awk '/one/{++n}n{if(n==1)print;else exit}' w5000.txt ) <( sed '/one/{x;/^$/!Q;g}' w5000.txt )
one                                                             one
two                                                             two
two                                                             two
Teste with [tt]gawk[/tt] 4.0.1 and [tt]sed[/tt] 4.2.1.

Feherke.
[link feherke.github.com/][/url]
 
This is the output given with GNU sed version 4.1.5
The red colour was to outline the garbage output.
Furthermore, I get the same result with the cygwin sed 4.2.1.

BTW, this is the 1st line of my test input file !
I did that to ensure to start at 1rst occurence of /one/ and stop at the 2nd
 
w5000, just to know: is your problem solved ?
 

yes, thank you both PHV and Feherke for your valuable posts.

chapeau bas! [smile]
kind regards,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top