The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pass csh variable to Perl Raynon Shell Programming and Scripting 9 10-19-2007 10:46 PM
How do I pass a variable to awk? eja UNIX for Dummies Questions & Answers 12 04-03-2007 06:46 PM
How to pass a variable to Awk ? Raynon Shell Programming and Scripting 24 02-26-2007 09:25 AM
getting the file name and pass as variable u263066 Shell Programming and Scripting 1 09-12-2006 05:38 AM
Pass variable to sed? kristy Shell Programming and Scripting 2 04-04-2002 04:14 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 12-18-2003
inquirer's Avatar
inquirer inquirer is offline
Registered User
  
 

Join Date: Aug 2001
Posts: 79
pass variable to awk

i would like to pass a variable to awk wherein the variable comes from external loop.

i tried this...

let x=0

until test $x -eq 32
do
cat file | awk '{ print $1 , "Number" , $($x) }' >> output
done

thanks,
  #2 (permalink)  
Old 12-19-2003
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,934
Try the following

cat file | awk -v xx=$x '{ print $1 , "Number" , xx }' >> output

- Finnbarr
  #3 (permalink)  
Old 12-19-2003
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
cat file | awk '{ print $1 , "Number" , $($x) }' >> output

-- Note: Awk is a little different from shell in that you do not need to use the $ sign to access a variable. The only time you would do so is if you wanted to get the value of a field variable. Example: $1 $2 ..$n where n is a value up to 199
  #4 (permalink)  
Old 02-11-2004
inquirer's Avatar
inquirer inquirer is offline
Registered User
  
 

Join Date: Aug 2001
Posts: 79
Thumbs down sorry but not working

actually this is a sample of what i want to do:

from the sample code:


Code:
grep "keyword" filename | awk '{print $2}' >> outfile
grep "keyword" filename | awk '{print $3}' >> outfile
        .
        .
        .
grep "keyword" filename | awk '{print $32}' >> outfile

i want to shorten it to loop like this:

Code:
let x = 2

until test $x > 32
do

grep "keyword" filename | awk ...???

let x = x + 1

done

so you see i want to pass a variable to awk where the value will also be the variable for print.

thanks a lot
  #5 (permalink)  
Old 02-11-2004
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
Well, I'm assuming "keyword" is the same word in all those lines you have in your first section of code.... and I'm assuming the line it returns is gonna be a long line, and you're trying to print each part of the returned line on it's own line...

See if this works for you:
Code:
grep "keyword" filename | awk '{ for (i = 1; i <= NF; i++ ) print $i}' >> outfile

Closed Thread

Bookmarks

« FTP Output | cut »
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 03:27 PM.


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