LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to pass an external variable to awk??? (https://www.linuxquestions.org/questions/programming-9/how-to-pass-an-external-variable-to-awk-683062/)

akitty 11-13-2008 02:00 AM

how to pass an external variable to awk???
 
hy, i'm new on this forum (so, hello everyone) and also new to shell scripting. I could really use some help with the script below:

for var in `ls file*`
do
awk -F" " -v myvar=$var ' /pattern1/ {print myvar ","}
/pattern2/ {print $2}
END {printf("\n");} ' $var >> report
done

i can't get awk to recognize the external variable "var"
i've tried other ways too, but nothing...:confused:
what is wrong?

theYinYeti 11-13-2008 02:33 AM

It seems all right to me. You're aware that you both set myvar to the file name AND use the file name as the file to read with awk, aren't you?

colucix 11-13-2008 03:01 AM

Do you get some error message or does the report just result empty?

akitty 11-13-2008 06:49 AM

Quote:

Originally Posted by theYinYeti (Post 3340318)
It seems all right to me. You're aware that you both set myvar to the file name AND use the file name as the file to read with awk, aren't you?

yes , i am aware of that. lets say i have a number of files containig info about some employees and the files are named after the department they work in. so, what i want is a report containing 2 fields : department(which i find only in the file name) and employee name.
i'm not doing this, but this is an example so u can understand what i need.
and yes, seems all right, but obviously it's not, couse it's not working.
thanks anyway for the reply!

akitty 11-13-2008 06:52 AM

Quote:

Originally Posted by colucix (Post 3340358)
Do you get some error message or does the report just result empty?

10x for the reply!
i tried awk and also nawk. i get messages like: "syntak error" or "command not found" :(

colucix 11-13-2008 07:00 AM

Ok. Ten billions dollars questions: what system are you running on? And which version of awk do you have? If you're not using the GNU awk (gawk) maybe the -v option is not valid. Anyway, it would be useful if you could post the exact (copy & paste) error message.

akitty 11-13-2008 08:13 AM

Quote:

Originally Posted by colucix (Post 3340546)
Ok. Ten billions dollars questions: what system are you running on? And which version of awk do you have? If you're not using the GNU awk (gawk) maybe the -v option is not valid. Anyway, it would be useful if you could post the exact (copy & paste) error message.

Well, i'm running Sun0S 5.9 with '/usr/bin/awk' and '/usr/bin/nawk' and for each file in the "for" loop i get:

awk: syntax error near line 1
awk: bailing out near line 1

I tried other ways too (if u say it's because of de -v option ), for instance:

for var in `ls file*`
do

nawk -F" " 'BEGIN {myvar="'"$var"'"}
/pattern1/ {print myvar ","}
/pattern2/ {print $2}
END {printf("\n");} ' $var >> report
done

this is not working either...(i didn't received error message after this but the report is empty)

colucix 11-13-2008 08:47 AM

nawk has the -v option, anyway the last version of your code should work (using quotes is another way to pass shell variables to awk). Indeed, you did not received any error. The reason for the empty report can be simply that the regexp /pattern1/ and /pattern2/ did not match.

So - please - can you try a simple test like this and post the result?
Code:

echo "This is just a test" > testfile
nawk -v myvar="yes"  '/just/{print $1, myvar}' testfile

Also, notice that the -F" " option is not needed, since the default Field Separator in awk already is the blank space.


All times are GMT -5. The time now is 06:56 PM.