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? |
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?
|
Do you get some error message or does the report just result empty?
|
Quote:
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! |
Quote:
i tried awk and also nawk. i get messages like: "syntak error" or "command not found" :( |
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.
|
Quote:
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) |
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 |
All times are GMT -5. The time now is 06:56 PM. |