LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-12-2009, 12:01 AM   #1
khaos83
Member
 
Registered: Dec 2007
Posts: 97

Rep: Reputation: 15
Help needed with awk


I am trying to extract information from a file using awk

e.g. My file
Code:
10001   aaa111   bbb222
10002   ccc333   ddd444
10003   eee555   fff666
If I want to extract information of 10001...

Code:
awk '/10001/ { print $1 " " $2 " " $3 }' myfile.txt
The above works. It outputs what i wanted




But if I want to hard code it and pass in the 10001 as a parameter into the script or using a variable...

Code:
mycode=10001

awk '/$mycode/ { print $1 " " $2 " " $3 }' myfile.txt
awk '/$1/ { print $1 " " $2 " " $3 }' myfile.txt
The above does not work. It does nothing and output nothing.
Is there something wrong with it?
I can't figure out why.

Last edited by khaos83; 02-12-2009 at 12:03 AM.
 
Old 02-12-2009, 12:16 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You have to put $mycode outside single quotes, otherwise it is not processed by the shell:
Code:
awk '/'$mycode'/ { print $1 " " $2 " " $3 }' myfile.txt
See the Awk Programming Guide about Using Shell Variables in awk programs, for further details.
 
Old 02-12-2009, 12:24 AM   #3
raut.vijay
LQ Newbie
 
Registered: Feb 2009
Posts: 4

Rep: Reputation: 0
The '$' sign which u r actually using has a different meaning in awk. I think, the site mentioned below would assist u in solving the problem.

http://www.vectorsite.net/tsawk_2.html#m2

Bye & Good Luck
 
Old 02-12-2009, 01:07 AM   #4
khaos83
Member
 
Registered: Dec 2007
Posts: 97

Original Poster
Rep: Reputation: 15
Wink

thanks!
 
Old 02-12-2009, 01:21 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
The $mycode variable in post #2 is evaluated by the shell before the arguments are filled out.
Code:
set awk '/'$mycode'/ { print $1 " " $2 " " $3 }' myfile.txt
> echo $1
awk
> echo $2
/1001/ { print $1 " " $2 " " $3 }
> echo $@
awk /1001/ { print $1 " " $2 " " $3 } myfile.txt
Pay attention to what is inside the single quotes and what isn't.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help needed in awk printing ZAMO Linux - General 1 01-30-2009 03:00 AM
awk or sed help needed cmontr Programming 42 11-02-2007 11:43 PM
Sed/Awk command help needed. farmerjoe Programming 3 03-02-2005 11:13 AM
awk experts help needed ferreirafm Linux - General 2 07-28-2004 08:38 PM
Help needed in writing Awk Scripts.. TheDarktrooper Red Hat 1 05-04-2004 06:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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