Computing.Net > Forums > Unix > Unix, Shell Script, awk & variables

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Unix, Shell Script, awk & variables

Reply to Message Icon

Name: maria g
Date: June 12, 2003 at 08:22:46 Pacific
OS: Solaris
CPU/Ram: ??
Comment:

Hi!

I'm trying to use my shell script variables in my awk-line. Is not working!

month3 is my variable and i'm aware that I can't use the $-prefix in awk.

awk ´$2==$month3{printf ("%16d,%14.1f\n",i+1,\$4);i++}´ $REPORT $REPORT1 $REPORT2 $REPORT3 $REPORT4 >> $AGR

Thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: June 12, 2003 at 08:47:01 Pacific
Reply:

Maria:

Surround your variables with double quote, single quote, double quote:

awk '{print "'"$VAR1"'", "'"$VAR2"'"}' file

Regards,

Nails


0

Response Number 2
Name: WilliamRobertson
Date: June 12, 2003 at 10:21:11 Pacific
Reply:

Or try,

awk -v param=value '{ ... }' filename.dat

The -v option may not work in all versions of awk. If not, try

awk param=value '{ ... }' param=value filename.dat


0

Response Number 3
Name: WilliamRobertson
Date: June 12, 2003 at 10:25:11 Pacific
Reply:

Forgot to add that you then use param (without $) in the awk code, e.g:

test.dat:
1 blue
2 red
3 green

awk -v colour=red '{ if ($2 == colour) print $0 }' test.dat

2 red


0

Response Number 4
Name: maria
Date: June 13, 2003 at 00:41:28 Pacific
Reply:

Thank You nails! it worked just perfect!

// maria


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Unix, Shell Script, awk & variables

Unix shell script problem... www.computing.net/answers/unix/unix-shell-script-problem/6217.html

Unix Shell Script www.computing.net/answers/unix/unix-shell-script/3580.html

Unix shell script runing SQL files www.computing.net/answers/unix/unix-shell-script-runing-sql-files/5286.html