LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 11-13-2008, 02:00 AM   #1
akitty
LQ Newbie
 
Registered: Nov 2008
Posts: 9

Rep: Reputation: 0
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...
what is wrong?
 
Old 11-13-2008, 02:33 AM   #2
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
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?
 
Old 11-13-2008, 03:01 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Do you get some error message or does the report just result empty?
 
Old 11-13-2008, 06:49 AM   #4
akitty
LQ Newbie
 
Registered: Nov 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theYinYeti View Post
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!
 
Old 11-13-2008, 06:52 AM   #5
akitty
LQ Newbie
 
Registered: Nov 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
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"
 
Old 11-13-2008, 07:00 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
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.
 
Old 11-13-2008, 08:13 AM   #7
akitty
LQ Newbie
 
Registered: Nov 2008
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
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)
 
Old 11-13-2008, 08:47 AM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
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.
 
  


Reply


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
pass the variable contain lrios Programming 7 04-18-2008 04:56 PM
pass & retrieve variables from awk sharathkv25 Programming 3 02-16-2007 03:05 AM
awk on a variable onradius Programming 8 02-22-2006 08:34 AM
pass value from awk back to bash shell cjs_pro Programming 2 03-07-2005 04:53 PM
Pass text to variable Zed Linux - Software 6 05-12-2003 03:02 PM


All times are GMT -5. The time now is 09:28 PM.

Main Menu
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