The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
use variable for sed print line atchleykl UNIX for Dummies Questions & Answers 3 02-23-2009 01:49 PM
How to print a value in the variable using awk ? jisha Shell Programming and Scripting 1 01-14-2008 07:45 AM
print variable in file using awk kamel.seg Shell Programming and Scripting 3 01-03-2008 06:43 AM
can awk print column using a variable ?? jambesh Shell Programming and Scripting 36 09-26-2006 08:39 AM
print the name of variable sun-guy Shell Programming and Scripting 1 08-06-2006 06:03 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-13-2004
kazanoova2 kazanoova2 is offline
Registered User
  
 

Join Date: Mar 2004
Posts: 9
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
  #2 (permalink)  
Old 05-13-2004
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
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 08:52 AM..
  #3 (permalink)  
Old 05-15-2004
kazanoova2 kazanoova2 is offline
Registered User
  
 

Join Date: Mar 2004
Posts: 9
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
  #4 (permalink)  
Old 05-15-2004
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
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
  #5 (permalink)  
Old 05-15-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,119
If I read the question right....

awk -v ph="bla bla bla" 'BEGIN{ FS="|";OFS=" "} /aaa/{print $2, ph}' file
  #6 (permalink)  
Old 05-16-2004
kazanoova2 kazanoova2 is offline
Registered User
  
 

Join Date: Mar 2004
Posts: 9
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`
  #7 (permalink)  
Old 05-16-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,119
What system are you using? Run "uname -a" and post the results.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0