LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-23-2005, 12:22 PM   #1
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,203

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
passing vars to awk


hi lq, i have this bash script, how do i pass the bash variable to the awk command? :

Code:
/temp> cat test.ksh
#!/bin/bash

cycle=$2
echo $cycle
awk                                          \
        '{                                   \
          if($2 == $cycle)                 \
          {                                  \
            printf( $0 "   cycle = " $cycle "\n"); \
          }                                  \
        }' $1

/temp> cat test.lst
l33t    h4x0rz  line1
hello   world   line2
chun-li akuma   line3

/temp> test.ksh test.lst world
world
awk: 0602-562 Field $() is not correct.
 The input line number is 1. The file is test.lst.
 The source line number is 2.
thanks all,
 
Old 09-23-2005, 12:41 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

use -v name="$var" directly after the (n)awk command.

I.e:
Code:
#!/bin/ksh

cycle=$2
echo $cycle
awk -v cycle="$cycle" '
{
  if ( $2 == cycle ) {
     printf("%s cycle = %s\n",$0,cycle)
  }
 }' $1
Output of testrun:
Code:
$ ./test.ksh test.lst world
world
hello   world   line2 cycle = world
I 'corrected' some other stuff as well.

Hope this helps.
 
Old 09-23-2005, 12:45 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Another point, it is unwise to name .ksh as extension to a bash script.
 
Old 09-23-2005, 12:58 PM   #4
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,203

Original Poster
Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
thanks druuna, it works magically.

thanks for the advice jll. (bad habit i suppose).
 
Old 09-25-2005, 11:14 AM   #5
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Re: passing vars to awk

[QUOTE]Originally posted by schneidz
[B]hi lq, i have this bash script, how do i pass the bash variable to the awk command? :
/quote]
By using quoting sensibly:
Code:
#!/bin/bash

cycle=$2
echo $cycle
awk                                          \
        '{
          if('$2' == $cycle)
          {
            printf( $0 "   cycle = " $cycle "\n");
          }
        }' $1
Spot the extra single quotes I've added. Your lesson for today is to figure out why this makes it work.

Last edited by eddiebaby1023; 09-25-2005 at 11:18 AM.
 
Old 09-25-2005, 11:44 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
should be
Code:
if('$2' == '$cycle')
...
printf( $0 "   cycle = " '$cycle' "\n");
 
Old 09-25-2005, 02:52 PM   #7
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Originally posted by jlliagre
should be
Code:
if('$2' == '$cycle')
...
printf( $0 "   cycle = " '$cycle' "\n");
Oops! I was concentrating on passing in $2. Sorry.
 
  


Reply

Tags
awk, variable


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing passing variable in Java as reference djgerbavore Programming 3 11-10-2004 03:18 PM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 05:32 AM
script problem while using awk and passing varibales ulto Programming 6 04-23-2004 12:58 PM
'bash'ing vars (dumping same) 3inone Programming 1 07-26-2003 11:06 AM
Environment vars AMDPwred Linux - General 2 02-28-2003 08:11 AM


All times are GMT -5. The time now is 11:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration