LinuxQuestions.org
Help answer threads with 0 replies.
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 10-16-2010, 11:03 PM   #1
Abid Malik
LQ Newbie
 
Registered: Sep 2010
Posts: 25

Rep: Reputation: -1
Thumbs down set value with awk


hello!

i want to change the value of a variable if a condition met.

i=0
awk '{if( $1 == $d ) i=2 }' file > temp
echo i = $i



but in the above code i is always equal to 0 whether the conditions are met.
 
Old 10-17-2010, 12:44 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hint #1:

Code:
i=abc
echo $i

i=$(echo def)
echo $i
Hint #2:
Code:
echo "ghi jkl" |awk '{print $2}'
And by all means, please post back when you arrive at a solution, marking your post "SOLVED". You've asked a number of questions recently, but haven't responded back to anybody who's tried to help you.

PS:
Another good link, besides those already suggested to you:

http://tldp.org/LDP/Bash-Beginners-G...ect_03_04.html

Last edited by paulsm4; 10-17-2010 at 12:46 AM.
 
Old 10-17-2010, 12:51 AM   #3
A.Thyssen
Member
 
Registered: May 2006
Location: Brisbane, Australia
Distribution: linux
Posts: 158

Rep: Reputation: 44
awk is a completely seperate command and language to shells. the variables do NOT match up.

You can feed in shell variables into awk by using double quotes, but that only sets the variable contents as a constant value, not as a varable into awk.

For output you need to some how get that result back into a shell variable. typically you get awk to print it and then read the output into a shell variable.

Code:
d="string"
i=`awk '{if( $1 == "'"$d"'" ) i=2 }  END {print i}' file`
echo "$i"
If you are outputting a file and want the variable, then you need to start getting even trickier, and use awk and shell file destriptors and or temporary files.

For multiple variables you can have awk generate 'shell commands' and "eval" the output results. THIS IS DANGERIOUS security wise.

Code:
d="string"
eval `awk '{if( $1 == "'"$d"'" ) a=2; b=3 }  END {print "a=" a "; b=" b }' file`
echo "a=$a  b=$b"
Another less dangeriosu method is to use "set" on the awk output to place the output 'words' into the shell variable $1 $2 etc..

Code:
d="string"
set -- `awk '{if( $1 == "'"$d"'" ) a=2; b=3 }  END {print a, b }' file`
echo "a=$1  b=$2"
All these tricky methods are listed (somewhere) in teh Advanced BASH Shell Scripting Guide
http://www.tldp.org/LDP/abs/html/index.html

PS: I have been shell programming for more than 30 years now, and have developed and used such methods continuiously over than time. No just for awk, but sed, perl, and many other programs. The methods are pretty much the same no matter what command you use.

NOTE this topic is not really appropriate for non-*nix forum. But then as there is not specific 'shell programming' forum, where it should go is a problem. Best place seems to be "linux-general".

Last edited by A.Thyssen; 10-17-2010 at 12:54 AM.
 
Old 10-17-2010, 01:35 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
A.Thyssen -

1. Abid Malik's question was *entirely* appropriate for this forum:
Quote:
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.
2. The modern, bash equivalent of the backquote ("eval `CMD`") you mentioned is now the "$(CMD)" syntax I cited above.

Abid Malik -

Please do post back with what you find (marking the post "SOLVED")! That way, your question (and your solution) can help others.
 
Old 10-17-2010, 02:55 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@OP - depending on what you are trying to do, obviously very hard to tell from your snippet, you may be better off just doing the whole script in awk ... just a thought
 
Old 10-17-2010, 03:13 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by A.Thyssen View Post
You can feed in shell variables into awk by using double quotes,
....
PS: I have been shell programming for more than 30 years now ..
in your 30 years of shell programming, did you happen to know awk's -v option ?
Code:
awk -vd="$d" '$1==d{ ... }' file
this is a cleaner way to pass shell variable to awk without messy quoting.
 
Old 10-17-2010, 06:07 AM   #7
A.Thyssen
Member
 
Registered: May 2006
Location: Brisbane, Australia
Distribution: linux
Posts: 158

Rep: Reputation: 44
Quote:
Originally Posted by ghostdog74 View Post
in your 30 years of shell programming, did you happen to know awk's -v option ?
Code:
awk -vd="$d" '$1==d{ ... }' file
this is a cleaner way to pass shell variable to awk without messy quoting.
Yes and I have used it, but I also find it awk-ward (sorry could not resist the pun). Most often you don't need the shell value in an actual variable, as it s only in one place, so I use that quoting method shown to put it there (at least for a string). Other times the shell variable contains optional code, as a sort of macro expansion. And finally it does not work for other commands like sed, and perl.

As for backquotes, yes I do know that $(...) is the new way to handle command substitutions, but generally I write my shell scripts for multiple computers, including some very old manchine that either use a very old version of bash, but more likely original bourne sh (SunOS for example). I have even needed shell script than had to run without change, on ultrix. ASIDE: the default shell does not even handle functions properly!

Even Mac's do not run a very modern version of bash, and don't use many GNU versions of awk, sed, etc. I have had to re-write many scripts to make them MacOSX-compatible as well as linux compatible.

As such I usually write scripts to be as compatible as possible, and only go to bash-isms, or other less compatible feature, only I really need a bash specific feature, such as nested command substitutions (which is why $(..) was invented), or variable arrays. At that time I change the 'bang' line from "sh" to "bash" to mark the script as bash specific. Not enough people do that!

Last edited by A.Thyssen; 10-17-2010 at 06:08 AM.
 
  


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
[SOLVED] bash and sed or awk commands to set tabs ? mythcat Programming 18 04-28-2010 03:32 AM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
why is doesnt work? (awk, set) DoME69 Programming 4 09-21-2008 06:32 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM

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

All times are GMT -5. The time now is 12:24 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