1834497 Members
2679 Online
110067 Solutions
New Discussion

Sed script

 
SOLVED
Go to solution
Aminur Rizal
Advisor

Sed script

Hi guys,
I have been working with this sed but can't get it work properly.
I have a file - a long one - with data and need to grep the phrases 2 lines after the keyword "var_out" hence :-

USIM=`grep -E -n "var_out" filename.txt | awk '{print $1+2}'`
This will result in number the line numbers of the data such as :-
20 43 56 75 88

Later I will use sed to grep all the phrases in the line numbers obtained from the first script.

sed -n "$USIMp" filename.txt > result.txt
But it doesn't work.
Can you please help.
Thanks in advance.
Aminur Rizal Afip
4 REPLIES 4
Rodney Hills
Honored Contributor
Solution

Re: Sed script

sed -n "$USIMp" filename.txt >result.txt
will look see if a variable named USIMp exists, which we assume doesn't.

Usually you would use curly brackets like-
sed -n "${USIM}p" filename.txt >result.txt
so that will evaluate to
sed -n "20 43 56 75 88p" filename.xt >result.txt
But this probabily isn't going get what you want.

You could try-
grep -E -n "var_out" filename.txt | awk '{printf "%dp\n",$1+2}' >/tmp/mylist
sed -n -f /tmp/mylist filename.txt >result.txt

Or perl can do it-
perl -n -e '$n=$.+2 if /var_out/; print $_ if $n == $.' filename.txt >result.txt

HTH

-- Rod Hills
There be dragons...
Aminur Rizal
Advisor

Re: Sed script

Hi Rodney,
Great! It really works for me.
I will stick with the awk and sed for the time being. Perl is too complex for me :)
Many thanks for the help.


Aminur Rizal Afip
Peter Godron
Honored Contributor

Re: Sed script

Hi,
if you want to pull out the line following 2 after the key_word "var_out" try:

sed -n '/var_out/n;n;p;' filename.txt

The n;n;p; means next;next;print;
Is this of any help?
Regards
Aminur Rizal
Advisor

Re: Sed script

Hi Peter,
It won't work that way. The output will include some unwanted lines.
Aminur Rizal Afip