reading specific line from file

Hi all...

I not a expert unix script programmer, Kindly adjust.

My requirement is that, i have a file which contains the about 10 lines -
say

1
2
3
...
8 war of the worlds: => text in this line
9 9000,80,78,77,334,445 => this line contains some numbers separted by commas
10

now i know that i have to search for the string "war of the worlds" which I can just grep and get.. but I want the line immediately after this line.

can any body help !!!

thank you in advance

Boss.

Check your man page for the grep command - should have an option to print the line number that the text is in ( -n on my OS)

Thank you for that quick answer.. I tried it out..

and I got the line number as-

linenum=`cat $ROOT/$1/$2 | grep -n "war of worlds:"|cut -d: -f1`;
echo $linenum;
linen=`expr $linenum + 1`;
echo $linen;

now what do i do with this line number.. i am stuck again.. "cat" doesnt have a option to read the file with specified line number.

Actually i want to change the line after this line, say -

war of worlds:
200,300,400

to

war of worlds:
100,200,300,400 => append a 100, to the begining of this line..

Pls show some more light

thank you !!

Boss

You can use sed -n to get the content at a particular line

sed -n '2,3p' filename

echo -e "100,\c" ; sed -n '5 p' file1

sed '/war of worlds/{n;s/^/100,/;}' file1

Thank you everybody.. I tried all of them and they all work.. but the one by Ygor suits me best !! :slight_smile:

Thank you !

However, in this line -

sed '/war of worlds/{n;s/^/100,/;}' file1

the number 100 is not fixed. I need to run this shell script recursively and so this number should be the output of a variable, like

sed '/war of worlds/{n;s/^/$3,/;}' file1

But unfortunately, unix doesnt seem to replace the variable with its value and prints $3 instead of the value. Any workarounds ?

Boss

yep got it !!
just replaced the single quotes with double quotes and its all done !!

now it looks like -

sed "/war of worlds/{n;s/^/$3,/;}" file1

Thank you all !!

:slight_smile:

Is there anyway I can change this command and try to get , lets say 3 lines after the line which matches the pattern .
I am trying

sed '/war of worlds/{n ; 3p }' file1 .

But it doesnt work . I can get 1 line when I say

sed '/war of worlds/{n ; p }' file1 .

How do i get 3 lines .

There may be better solution than this ...

sed -n '/war of worlds/{n;p;n;p;n;p;};' file1

Thanks Bhargav. That works . So I guess if we have to get lets say x number of lines after the match , we'll just have to print it x number of times .