LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 03-04-2011, 10:16 PM   #1
voda87
LQ Newbie
 
Registered: Mar 2011
Posts: 6

Rep: Reputation: 0
Smile awk help pls.. a bit complex...


Hi all, I am using an awk command to print a line from a cvs file.
the awk command includes an if statement that filter the output
-lets say i want to print all the lines that the price field is greater than 30.
i have it working when i put the parameters myself.. but when i try to send them with vars it wont work..
i am sending the sign of the if statement - can only be: == , < , >

it looks like this:
cat file.csv | awk -v sign=">" -v field="2000" '{if($3 sign field) printf "%-12s%-12s%-12s%-12s\n",$1,$2,$3,$4}' FS=\,

the bold part is the problem , because when i put the sign parameter myself t works great.. i guess its a chars issue but i cant spot it..

Thanks, i'll appreciate any idea..
 
Old 03-05-2011, 12:14 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
First of all, please use [code][/code] tags around your code, to preserve formatting and to improve readability.

It usually also helps to give a sample of the input text, and the exact error messages you get from the commands.

Anyway, I can't say for sure, but after working with it for a bit, it looks to me like awk simply doesn't recognize conditional operators when they're stored in variables. Perhaps it has something to do with the way awk treats variable contents as regexes when used in tests, instead of as simple strings, or something.

After stripping your command above down to the essentials, the only easy way I could find around it was to use creative quoting, so the shell variable can be used directly in the statement.
Code:
sign=">"
test_shell=2000
$ awk -v test=$test_shell '{ if ($3 '"$sign"' test) {print}}' file.csv
Notice how I unquoted the $sign variable (and re-quoted it in double-quotes). This way it will be expanded before the expression is passed to awk. You could do the same thing with the test variable too, of course.

Finally, there's no point in using cat and a pipe when awk can read directly from the file.
 
Old 03-06-2011, 04:03 PM   #3
voda87
LQ Newbie
 
Registered: Mar 2011
Posts: 6

Original Poster
Rep: Reputation: 0
TY David, worked perfectly!
 
Old 03-06-2011, 11:45 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Glad to help. Now please follow the advice in my signature.
 
  


Reply



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
Need a bit of help in line-editing awk\sed and foreach Jykke Linux - General 12 10-14-2010 08:17 PM
[SOLVED] How to Awk Paragraph in complex text file? VMthinker Linux - General 1 09-24-2010 05:41 AM
[SOLVED] How to Awk Paragraph in complex text file? VMthinker Linux - Newbie 1 09-24-2010 01:15 AM
awk script help pls cmontr Programming 5 10-24-2007 10:49 PM
pls pls pls help me ! i'm tired with httpd config on fedora apache 2.0.48 AngelOfTheDamn Fedora 0 01-24-2004 05:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 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
Open Source Consulting | Domain Registration