The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 04-17-2008
Registered User
 

Join Date: Apr 2008
Posts: 6
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
Reply With Quote
Forum Sponsor
  #2  
Old 04-17-2008
Registered User
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 160
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.
Reply With Quote
  #3  
Old 04-17-2008
Registered User
 

Join Date: Apr 2008
Posts: 6
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,
Reply With Quote
  #4  
Old 04-17-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
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?)?
Reply With Quote
  #5  
Old 04-18-2008
Registered User
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 160
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.
Reply With Quote
  #6  
Old 04-18-2008
Registered User
 

Join Date: Apr 2008
Posts: 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,
Reply With Quote
  #7  
Old 04-18-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Does your script cd to a different directory while looping?

The eval is still quite needless.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0