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
How to print a value in the variable using awk ? jisha Shell Programming and Scripting 1 01-14-2008 03:45 AM
print variable in file using awk kamel.seg Shell Programming and Scripting 3 01-03-2008 02:43 AM
use variable for sed print line atchleykl UNIX for Dummies Questions & Answers 2 04-24-2007 09:44 AM
can awk print column using a variable ?? jambesh Shell Programming and Scripting 36 09-26-2006 04:39 AM
print the name of variable sun-guy Shell Programming and Scripting 1 08-06-2006 02:03 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-13-2004
Junior Member
 

Join Date: Feb 2004
Posts: 9
Stumble this Post!
Unhappy print variable in awk

i read the variable ph from file and i wanna to print it in awk.
example
ph=`cat tmpbatch`
tail h.txt|grep "| |"|awk -F"|" '{ print "@unpdx.sql",$5 }'"$ph"
i try this but it does not work
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-13-2004
google's Avatar
Moderator
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
Stumble this Post!
try using the -v switch when passing variables into Awk

awk -v foo=$ph ......

Here is a link Using Shell Variables in Awk

Last edited by google; 05-13-2004 at 04:52 AM.
Reply With Quote
  #3 (permalink)  
Old 05-14-2004
Junior Member
 

Join Date: Feb 2004
Posts: 9
Stumble this Post!
Post i need more

i try it but it does not work it give me error
suppose "ph" is a variable were calculated before
ph= bla bla bla
and i have an input file as
aaa|1234
bbb|6325
ccc|7454
aaa|5467

i wanna to print in out put file second field of the line that contian aaa and at the end of each line the value of the variable.
so the output will be

1234 bla bla bla
5467 bla bla bla

and thnx in advance
Reply With Quote
  #4 (permalink)  
Old 05-15-2004
google's Avatar
Moderator
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
Stumble this Post!
For something like this, i would think you would want to use an egrep or grep expression. I dont think (or dont know how) to use a variable passed into Awk as a pattern to search. But you can do something like this, thought it may not run too fast.

awk -v ph=bbb 'BEGIN{ FS="|" ; OFS=" " } {if($1 ==ph) {print $2, $1}} ' file
Reply With Quote
  #5 (permalink)  
Old 05-15-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,426
Stumble this Post!
If I read the question right....

awk -v ph="bla bla bla" 'BEGIN{ FS="|";OFS=" "} /aaa/{print $2, ph}' file
Reply With Quote
  #6 (permalink)  
Old 05-16-2004
Junior Member
 

Join Date: Feb 2004
Posts: 9
Stumble this Post!
Thumbs down print variable in awk

i try the folowing code "just for testing" i get the following error
operate >tail ACCL.txt|awk -v ph="bla bla bla" '{print $2, ph}'
awk: syntax error near line 1
awk: bailing out near line 1
and does this work if i ph calculated by exprestion as
ph = `cat file`
Reply With Quote
  #7 (permalink)  
Old 05-16-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,426
Stumble this Post!
What system are you using? Run "uname -a" and post the results.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:57 AM.


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

Content Relevant URLs by vBSEO 3.2.0