![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| assign subst|grep|sed command result to a variable | snowbiker99 | Shell Programming and Scripting | 5 | 11-14-2007 04:08 PM |
| Assign o/p of awk to a variable | c2b2 | Shell Programming and Scripting | 7 | 02-01-2007 09:30 AM |
| assign value to variable using AWK | HAA | Shell Programming and Scripting | 4 | 12-06-2006 08:43 AM |
| assign a value to variable | markjason | Shell Programming and Scripting | 3 | 10-10-2006 11:05 AM |
| assign to variable | AkumaTay | UNIX for Dummies Questions & Answers | 1 | 05-18-2002 11:11 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Your command is right. you just need to use backticks to execute that.
Code:
var=`grep -n "end" FILE1 | cut -f1 -d":"` |
|
#3
|
|||
|
|||
|
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, |
|
#4
|
|||
|
|||
|
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?)? |
|
#5
|
|||
|
|||
|
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.
|
|
#6
|
|||
|
|||
|
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, |
|
#7
|
|||
|
|||
|
Does your script cd to a different directory while looping?
The eval is still quite needless. |
|||
| Google The UNIX and Linux Forums |