finding the line number of a particular line in a file

Hi Frnds,

I need to find the line number of a particular line in a file and store that line number to a variable.

if a file named myfile contains following

look at the sun
look at the moon
look at the star
look at the ocean

i need to get the line number of the line 'look at the moon'

i need to get the value as 2 and store it in a variable for manipulation.

how to do this ?

Thanks in advance for the help.

Start with:

grep -n "look at the moon" file | cut -d":" -f1
1 Like

Thanks a lot for quick reply.

now how can i assign it to a variable

var1 = grep -n "look at the moon" file | cut -d":" -f1

above stmt gives me error.

Please suggest.

Thanks in advance

var1=`grep -n "look at the moon" file | cut -d":" -f1`
1 Like