INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)

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

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

Hi

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

Feherke.
http://feherke.github.com/

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

(OP)
yes, you are right.

on expected output from the example should have been:

one
two
two

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

Hi

Ok. Then here is how I would do it :

CODE

sed '/one/{x;/^$/!Q;g}' /input/file 
Tested with GNU sed.

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.
http://feherke.github.com/

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

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

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

Hi

Thanks you for the note on legacy sed behavior.

But I definitely not understand what you mean in your second paragraph, especially the red line :

CODE --> command-line

master # awk '/one/{++n}n{if(n==1)print;else exit}' w5000.txt 
one
two
two

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

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

master # 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 gawk 4.0.1 and sed 4.2.1.

Feherke.
http://feherke.github.com/

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

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

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

w5000, just to know: is your problem solved ?

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

(OP)

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

chapeau bas! smile
kind regards,

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close