grep and assign it to variable

Hi ,

Help required

i want to grep a pattern from a file using "grep -n" command
then cut the field (i.e line number return by cut command) and
assign it to a variable

e.g

var=grep -n "end" FILE1 | cut -f1 -d":"

But i am not able to perform this operation.

i am performing all this operation in a script do need to use "eval"

Thanks in advance

Your command is right. you just need to use backticks to execute that.

var=`grep -n "end" FILE1 | cut -f1 -d":"`

assuming there is only one "end" in the file.

thanks,

i tried it out but it gives error cannot open file.
i have checked for permission ever thing is ok.

if i perform this operation at command line it works fine.but as i am
doing this in a script it gives error.

i works fine if i write the command in this way:

eval grep -n "end" FILE | cut -f1 -d":"

But i want to storee the result in a variable,
Please help in about this.

Thanks,

If it works without the backticks, it should work with the backticks.

The eval isn't useful here.

What's the error message? Can you do ls=`ls FILE` (or is it FILE1?)?

In your eval, you have given file name as FILE. But in your earlier post, you have given it as FILE1. Please check the file name before executing the command with backticks.

hi
the actuall requirement are as such

grep -n "end" $FILE | cut -f1 -d":"

here $FILE is variable which contains the path of the file which
need to be grepped.

i am performing this operation in a while loop,which reads from a file (say xx) and for each path given in file xx, it assign path to variable
$FILE and then perform the grep operation

if i just write the expression as given above in my script it gives error saying "grep: canot open file ......"

And if i perform in this way

eval grep -n "end" $FILE | cut -f1 -d":"

it gives proper result at the scree
i.e 200 (line number expected)

But now i want this line number to be store it in a variable instead displaying at the screen

HOPE so now you got my problem...

Thanks,

Does your script cd to a different directory while looping?

The eval is still quite needless.