find out line number of matching string using grep

Hi all,

I want to display line number for matching string in a file. can anyone please help me.

I used

grep -n "ABC" file

so it displays
6 ABC.

But i only want to have line number,i don't want that it should prefix matching context with line number.

Actually my original problem is that i want to use this number & used to print next line.

So can anyone please provide me code to first find out a particular pattern in file & displays the content of next line.

i used to print the content using sed -n '8p' file, if i have found the matching string on line 7.

Thanks in advance
sarbjit

grep -n "ABC" file | awk '{print $1}'
awk '/ABC/{print NR}' file

Regards

Thanks 4 reply
but it is still showing me the part of pattern.
eg i need to search "abc -x"
it displays now 7abc:

Thanks

Try:

awk '/pattern/{print FNR}' infile

or:

sed -n '/pattern/=' infile

thanks man, it really works, but can you please provide me code to display the contents of next line, actually i was thinking that i could store this number in variable & then use
sed -n "vp" file
where v is the output from the code + 1 you provided, but it is not working . any alternative please

use "$v" instead of "v" in sed.

in awk something like this :

awk '/pattern/ {p=1;next} p--' file_name.txt

Thanks man but it is not working. In case of sed it gives me following message
sed: -e expression#1, char3 : expected newer vesrion of sed.
for command
set v=8
sed -n '$vp' file

awk '/ABC/{getline;print}' file

thanks man, it is working perfect now

---------- Post updated at 01:47 PM ---------- Previous update was at 12:51 PM ----------

Can you please tell me if i have to print specific line number from the found pattern. Eg, if i have found pattern on line 6 , i need to print next to next line or say third line from the found pattern

Hi sarbjit,
sed -n "$vp" file won't work as shell will search for value of vp variable instead of variable v. Try this.
sed -n "${v}p" file
Also if you have multiple matches of the search string & you need line no of first match try this:
v=`awk '/searchtext/{print NR ; exit}' file`