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 02-11-2010, 10:02 PM   #1
puth
LQ Newbie
 
Registered: Feb 2010
Posts: 19

Rep: Reputation: 0
How to access command line variables from awk


hi,

I am taking an argument from the command prompt for my shell script ie $1 and i need to use $1 in my awk part of the script.But it actually doesn't get any value when used in awk.
Can anyone help me in accessing this command line argument in awk?
 
Old 02-11-2010, 10:11 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
There are several ways of doing it. Can you post what you have tried? Then we can suggest how to make it work.
 
0 members found this post helpful.
Old 02-11-2010, 10:30 PM   #3
puth
LQ Newbie
 
Registered: Feb 2010
Posts: 19

Original Poster
Rep: Reputation: 0
I take domain name as the argument from the user and use it as $1.

domain=$1
In the awk statement i am using it as
if (match(toupper($0),"DOMAIN $domain")>0)
then perform some checks

DOMAIN $domain doesn't match to any text in my file and doesn't enter the loop.
 
Old 02-11-2010, 11:49 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
use the -v option of awk.
 
1 members found this post helpful.
Old 02-12-2010, 01:29 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by puth View Post
I take domain name as the argument from the user and use it as $1.

domain=$1
In the awk statement i am using it as
if (match(toupper($0),"DOMAIN $domain")>0)
then perform some checks

DOMAIN $domain doesn't match to any text in my file and doesn't enter the loop.
Please show the full awk statement within the script. It would be helpful if you put it in code tags (that's a link to instructions or you may prefer to use "Advanced Edit" mode which has a # button for code tags).
 
0 members found this post helpful.
Old 02-12-2010, 12:06 PM   #6
puth
LQ Newbie
 
Registered: Feb 2010
Posts: 19

Original Poster
Rep: Reputation: 0
i used

awk -v domain=${DOMAIN} ' BEGIN { dflag=0; }
if (match((toupper($0)),"DOMAIN "domain) > 0 ) and it worked.

Thanks.
 
  


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
awk command line: blank line record sep, new line field sep robertmarkbram Programming 4 02-21-2010 05:25 AM
sed / awk command to print line number as column? johnpaulodonnell Linux - Newbie 2 01-22-2007 07:07 AM
How to use awk command to parse fields in a line johnsanty Programming 9 05-25-2006 09:56 PM
Awk - get a parameter from the command line benjalien Programming 1 01-24-2006 09:06 AM
Awk command-line arguments lowpro2k3 Programming 1 03-28-2005 09:09 PM

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

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