Finding the line with the exact same number

Hello All,

What i am doing is , i tail a file from certain chatacter and then cat -n to get the line numbers.I search for a particular string and gets it line number. What i am interested in is the next line immediately after the pattern i search.

But grep gives me result for all line statring with those digits.
For Ex:

I got the pattern in line as:

10 Pattern i searched.

Now, I need contents of line 11(i store in variable say $next_line).

so i do :

tail -c [number]  | grep "^[[:space:]]* $next_line

But it gives me result for the lines like 11, 111, 110 and so on .

Is there a way to only search for the exact number in grep.

If I understood correct, you do not really need the line numbers but used them to identify the next line.

Maybe these examples works for you:

$> cat infile
eins
zwei
drei
vier
fuenf
$> sed -n '/drei/ {n;p;q}' infile
vier
$> awk '/drei/ {getline; print}' infile
vier

Thanks for reply.
Yes, you are right, i just track numbers to get the contents, but the thing is there are several lines in file with the same contents i grep. So i process them one by one . Also its on different boxes, i do ssh and use this. By line number i keep track which i processed and which didn't.
Will be good if i get something in grep itself or anything which allows only to search the exact number.

Ok, not sure how you correlate the stuff you get via ssh, but basically processing a file step by step should be no problem. Here is an example with awk, where the pattern is more than once in the file (if I undestood correct):

$> cat infile
1       one
2       two
3       three
4       one
5       two
6       four
7       five
8       one
9       two
10      one
$> awk '/two/ {getline; print}' infile
3       three
6       four
10      one

I added numbers so you can see that it is working on all three patterns it found which is in this case "three" :wink:

awk processes the whole file so you could put something it should do with the found patterns (ie. the following line) into the curled brackets.

Thanks a lot , it works :b:.

Can you please explain the command ( not much familiar with awk).

/two/

match this pattern somewhere in a line; if true, execute the commands in the curled braces

getline

get the next line in the file

print

and print it

That's all.