Operating System - Linux
1752592 Members
3000 Online
108788 Solutions
New Discussion юеВ

Re: Print from matched string on....

 
SOLVED
Go to solution
dev44
Regular Advisor

Print from matched string on....

Hi,

How can I print or get the output from a certain string on...like everything after that string printed out to the screen?

Thanks,
Sally
whatever
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: Print from matched string on....

Hi Sally:

# cat stuff
this is a file
of stuff
which I want to
dump from here
to the end.
Hence, here it is...
from there to the end.

# sed -ne '/here/,$p' stuff
dump from here
to the end.
Hence, here it is...
from there to the end.

Regards!

...JRF...
Pete Randall
Outstanding Contributor

Re: Print from matched string on....

Sally,

For questions like this I always turn to "Handy One-Liners for Sed" (attached) which I found here on the Forums years ago.


Pete

Pete
Hein van den Heuvel
Honored Contributor

Re: Print from matched string on....

Try this:

awk '/certain string/,0' file.txt

Hein.
dev44
Regular Advisor

Re: Print from matched string on....

Thanks guys!
whatever
James R. Ferguson
Acclaimed Contributor

Re: Print from matched string on....

Hi Sally:

If you mean literally "everything AFTER that string" but not anything that preceeds it on the same line, I'd do it thusly:

# perl -nle 'if ($n) {print} elsif (m/(here.*)/) {print $1;$n++}'

It this example, the string to match is "here". As written it could be a part of the string "where". Thus, if you wanted only the isolated word "here" (at a boundary) , change this to:

# perl -nle 'if ($n) {print} elsif (m/(\bhere\b.*)/) {print $1;$n++}'

Now use this variation and compare the difference of the two:

# cat stuff
this is a file
of stuff
where I want to
dump from here to
the end.
Hence, here it is...
from there to the end.

Regards!

...JRF...
Hein van den Heuvel
Honored Contributor

Re: Print from matched string on....

This topic is closed, problem solved. Excellent!
But I had an extra thought...

'Normally' (is there such thing?) the awk range is used as: /start/,/end/

In my example I used a hardcoded 0 as end test, so the end match is never true (near). It ends when the data ends.

Sometimes I want to see a few lines after a match. I just realized that using a similar technique one could use:

awk '/error/,!++x%5' file.log

This is only a quick and dirty solution.
One would probably want a seperator between matching zones.
And what to do on re-match while in the print zone?
If you solve all that, then it quickly becomes a full blown script.
Cute?

Hein.