grep and assign it to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep and assign it to variable
# 1  
Old 04-18-2008
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
# 2  
Old 04-18-2008
Your command is right. you just need to use backticks to execute that.

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

assuming there is only one "end" in the file.
# 3  
Old 04-18-2008
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  
Old 04-18-2008
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  
Old 04-18-2008
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  
Old 04-18-2008
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  
Old 04-18-2008
Does your script cd to a different directory while looping?

The eval is still quite needless.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Assign value to variable

Hi Guys, I need to assign the value of which has rows to a variable, Can you advise how to do that hive --orcfiledump /hdfs_path/ | grep "Rows" Rows: 131554 I need to assign this row count itself to a unix variable count=$(hive --orcfiledump /hdfs_path/ | grep "Rows") Expected ... (6 Replies)
Discussion started by: Master_Mind
6 Replies

2. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

3. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

4. Shell Programming and Scripting

If grep value is null then assign 0 to variable

DELETE=`cat $logfile1 | egrep -i "Delete" | sed 's/ */ /g' | cut -d" " -f2` INSERT=`cat $logfile1 | egrep -i "Insert" | sed 's/ */ /g' | cut -d" " -f2` UPDATE=`cat $logfile1 | egrep -i "Update" | sed 's/ */ /g' | cut -d" " -f2` I need soming like below: if value is null... (8 Replies)
Discussion started by: Veera_V
8 Replies

5. Shell Programming and Scripting

Assign grep match to variable question

Hi, I'm trying to assign a grep result to a variable but instead of having all grep result's assigned to the variable, would it be possible to assign the first match, do something, then move onto the next match and assign it to that variable and so on until all matches have been completed I... (4 Replies)
Discussion started by: Jazmania
4 Replies

6. Shell Programming and Scripting

Shell assign variable to another variable

How can I assign a variable to an variable. IE $car=honda One way I can do it is export $car=honda or let $car=2323 Is there any other ways to preform this task (3 Replies)
Discussion started by: 3junior
3 Replies

7. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

8. Shell Programming and Scripting

assign subst|grep|sed command result to a variable

Hi, I'm quite new to scripting and I want to modify following line of an existing script: MYVAR=`subst |grep 'L:\\\:' | sed -e 's/.*\\\//'`; What I have to do is to use the content of a variable instead of the constant expression 'L:\\\:' as the grep string to be matched. Assuming I already... (5 Replies)
Discussion started by: snowbiker99
5 Replies

9. Shell Programming and Scripting

assign a value to a variable

I have a list of names in a file. i want to assign those names to a variable in such a manner eg: $cat file.txt pete lisa john var=pete-lisa-john how do i do this in shell scripting? (10 Replies)
Discussion started by: Shivdatta
10 Replies

10. Shell Programming and Scripting

assign a value to variable

I have to assign a result of a query to a vairable like this how can i do this Query = select count(*) from table x=`db2 ${Query}| sed -n '4p'` but this doesn't work, is there any other way to assign the result without redirecting the result to temp file. . Thanks Mark. (3 Replies)
Discussion started by: markjason
3 Replies
Login or Register to Ask a Question