LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 08-11-2011, 08:51 AM   #1
915086731
Member
 
Registered: Apr 2010
Posts: 120
Blog Entries: 5

Rep: Reputation: 2
How can I pass variable to "awk" and use print ?


Problem comes with following command:
Code:
[saturn@localhost Desktop]$ a=2
[saturn@localhost Desktop]$ awk  'NR==2 {print $2}' about.txt 
rights
[saturn@localhost Desktop]$ awk  "NR==2 {print $2}" about.txt 
All rights reserved. Use is subject to license terms.           ????why print all fields????
[saturn@localhost Desktop]$ awk  "NR==$a {print $2}" about.txt 
All rights reserved. Use is subject to license terms.           ????why print all fields????
[saturn@localhost Desktop]$ awk  'NR==$a {print $2}' about.txt 
                                                               !!!print nothing!!!
Why does it print all fields? How to resolve it.
Thanks!!

Last edited by 915086731; 08-11-2011 at 08:53 AM.
 
Old 08-11-2011, 09:01 AM   #2
mayursingru
Member
 
Registered: Nov 2010
Location: Pune
Distribution: CentOS
Posts: 51

Rep: Reputation: 5
Hi,
I think you are looking for this. Please correct me if i am wrong.

Code:
 awk '{ if(FNR%2==0) print $2 }' filename
 
Old 08-11-2011, 09:13 AM   #3
915086731
Member
 
Registered: Apr 2010
Posts: 120
Blog Entries: 5

Original Poster
Rep: Reputation: 2
Sorry , I just want to use $a, not "2", may be $a is equal 3 .
And I want to use the format: NR==$a , not FNR%2==0
I don't know can it be realized or not .
 
Old 08-11-2011, 09:26 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
In
Code:
awk  "NR==$a {print $2}" about.txt
presumably the shell's $2 is empty so it becomes
Code:
awk  "NR==2 {print }" about.txt
One way to solve it is to "backslash escape" the $2 so, after bash has substituted in the double quoted string it becomes
Code:
awk  "NR==2 {print $2}" about.txt
the code for that would be
Code:
awk  "NR==$a {print \$2}" about.txt
 
1 members found this post helpful.
Old 08-11-2011, 09:43 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,469

Rep: Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888Reputation: 1888
So pass the variable to awk:
Code:
awk -va=$a 'NR==a {print $2}' about.txt
 
1 members found this post helpful.
Old 08-11-2011, 10:02 AM   #6
915086731
Member
 
Registered: Apr 2010
Posts: 120
Blog Entries: 5

Original Poster
Rep: Reputation: 2
Yes, I get what I want.
but why?
 
Old 08-11-2011, 10:34 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Assuming you are replying to my post and not grail's it works because the $ in $2 is "backslash escaped"; the \ in front of $ tells bash not to treat the following $ specially so bash does not replace $2 with the value of $2, it simply passes the string $2 to awk which is what you want.
 
1 members found this post helpful.
Old 08-11-2011, 10:57 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
When you post a question, it's usually helpful to give us all the relevant context of what you're doing.

In this case, you should give us a raw sample of the input text and the exact data, and format, that you want to extract from it, in addition to the command itself.

It also helps to explain what you're doing and why. Is the awk command being used inside a script, for example, or just on the command line?

Anyway, remember that awk has a different variable system from your shell. You can't use bash variables in an awk command without either importing them with the "-v" option or using complex quoting.

Code:
$ echo "foo bar baz" > file.txt

$ bashvar=3
$ echo "$bashvar"
3

$ awk -v awkvar="$bashvar" '{ print awkvar , $awkvar }' file.txt
3 baz
"bashvar" is a bash variable, in this case it contains the number "3".
"awkvar" is an awk variable, which we set to the value of "$bashvar" using the -v option.

Using an awk variable alone (without the "$") prints the value it contains, in this case "3". When you add the "$" to it, it will now reference the value of the input text field of the number stored in the variable (if any), which in this case is field number 3 or "baz".

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.informatik.uni-hamburg.de.../gawk_toc.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/

Last edited by David the H.; 08-11-2011 at 11:03 AM. Reason: minor clarification
 
1 members found this post helpful.
Old 08-11-2011, 11:37 AM   #9
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,149

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Also, your use of double-quotes (") around the awk program string instructs the shell to parse the string, which, for example, will remove the curly bracket symbols. The single-quote passes the contents verbatim. So, the you examples where you used the double-quotes, there was no process section defined, and the default awk process is to copy the input to the output.
 
Old 08-11-2011, 02:50 PM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by PTrenholme View Post
Also, your use of double-quotes (") around the awk program string instructs the shell to parse the string, which, for example, will remove the curly bracket symbols.
AFAIK that is not correct when applied to { ... } which do not have a preceding $, which is the common case when the double quoted string is an awk program. From the GNU Bash Reference on double quotes:

3.1.2.3 Double Quotes

Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘`’, ‘\’, and, when history expansion is enabled, ‘!’. The characters ‘$’ and ‘`’ retain their special meaning within double quotes (see Shell Expansions). The backslash retains its special meaning only when followed by one of the following characters: ‘$’, ‘`’, ‘"’, ‘\’, or newline. Within double quotes, backslashes that are followed by one of these characters are removed. Backslashes preceding characters without a special meaning are left unmodified. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ‘!’ appearing in double quotes is escaped using a backslash. The backslash preceding the ‘!’ is not removed.


Bash will remove curly brackets following a $, as in it will substitute the value of bashvar when it finds ${bashvar} and other similar ${ ... } bash parameter expansions.

Last edited by catkin; 08-11-2011 at 02:53 PM. Reason: sub-editing
 
1 members found this post helpful.
Old 08-11-2011, 06:33 PM   #11
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,149

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Quote:
Originally Posted by catkin View Post
AFAIK that is not correct when applied to { ... } which do not have a preceding $, which is the common case when the double quoted string is an awk program. [...]
O.K.,

I should have used the $<number> example, not the {...} one. The point I was trying to make was just that bash does mess with the contents of double-quoted strings, but not with single-quoted ones.
 
Old 08-11-2011, 09:35 PM   #12
915086731
Member
 
Registered: Apr 2010
Posts: 120
Blog Entries: 5

Original Poster
Rep: Reputation: 2
Thanks all.
 
  


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
[SOLVED] How can I pass variable to "awk" ? 915086731 Linux - Software 6 08-11-2011 07:33 AM
[SOLVED] Access/Print a C Variable "as it is stored in the memory" paliga Programming 14 10-25-2010 11:06 AM
printing hh in hh:mm using "awk '{FS=":";print $1}'" misses first line of output!! mayankmehta83 Linux - Newbie 2 12-03-2009 02:55 AM
How do you do ps ax | grep mozilla | awk '{print "kill " $1}' pppaaarrrkkk Programming 4 03-19-2008 07:17 PM
Sed/Awk: print lines between n'th and (n+1)'th match of "foo" xaverius Programming 17 08-20-2007 11:39 AM


All times are GMT -5. The time now is 02:01 AM.

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