LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 07-29-2011, 01:59 AM   #1
915086731
Member
 
Registered: Apr 2010
Posts: 144
Blog Entries: 6

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


Please see the following code, I can not pass the var "a" to awk.It seems that $a is not identified. How to resolve it ?
Thanks

Code:
[river@localhost sda5]$ a=2
[river@localhost sda5]$ awk 'NR==$a' t.txt
[river@localhost sda5]$ awk 'NR==2' t.txt
All rights reserved. Use is subject to license terms.
 
Old 07-29-2011, 02:02 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,809

Rep: Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069Reputation: 3069
Check out '-v' option
 
Old 07-29-2011, 02:02 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Change from single quotes to double quotes, from awk 'NR==$a' t.txt to awk "NR==$a" t.txt
 
Old 07-29-2011, 02:04 AM   #4
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
Thanks , but why? What is the difference between single quotes and double quotes.
 
Old 07-29-2011, 02:09 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Quote:
Originally Posted by 915086731 View Post
Thanks , but why? What is the difference between single quotes and double quotes.
Strings within single quotes are not changed by bash. Inside double quotes, bash substitutes variable values as you want (and does other things). This is explained in all bash tutorials, the bash man page and here (which is the man page plus, online).

Last edited by catkin; 07-29-2011 at 02:10 AM. Reason: clearer formatting
 
Old 07-29-2011, 02:12 AM   #6
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
Thanks .
 
Old 08-11-2011, 08:33 AM   #7
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
But problem comes with following command:
Code:
[river@localhost Desktop]$ awk  "NR==$a {print $2}" t.txt 
All rights reserved. Use is subject to license terms.
Why does it print all fields? How to resolve it.
Thanks!!
 
  


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
how can i create a global variable called "dogpatch" with a value of "woof" earthdog Linux - Newbie 9 12-15-2009 02:03 AM
printing hh in hh:mm using "awk '{FS=":";print $1}'" misses first line of output!! mayankmehta83 Linux - Newbie 2 12-03-2009 03:55 AM
How to get the "data type" of an "unknown variable" in "C Language" ? Affair Programming 8 06-20-2009 01:30 PM
how to pass an external variable to awk??? akitty Programming 7 11-13-2008 09:47 AM
"Broken" envirnment variable (MANPATH) and "/etc/profile.d" question. ErV Slackware 3 03-20-2007 10:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:48 PM.

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