LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 04-13-2012, 08:34 PM   #1
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Rep: Reputation: 17
awk search on a variable


Hi,

I would like to do the following, however /matchvar/ is the string being searched and not the variable passed to gawk from the shell, that is being searched for.

Do I have to use some special awk quoting, or a different method of searching ?

Code:
 $ match=1000 ; echo '2000 1000' | gawk -F' ' -v matchvar=$match '{if ($2 ~ /matchvar/){print "it worked"}}'

Last edited by dazdaz; 04-13-2012 at 08:37 PM.
 
Old 04-13-2012, 10:57 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by dazdaz View Post
Code:
 $ match=1000 ; echo '2000 1000' | gawk -F' ' -v matchvar=$match '{if ($2 ~ /matchvar/){print "it worked"}}'
With those slashes you are trying to match against the literal characters they enclose, i.e., the 8-character string "matchvar". Just use the name alone.
Code:
 gawk -F' ' -v matchvar=$match '{if ($2 ~ matchvar){print "it worked"}}'
 
1 members found this post helpful.
Old 04-14-2012, 12:18 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

From man awk:
Quote:
In the special case that FS is a single space, fields are
separated by runs of spaces and/or tabs and/or newlines.
AFAIK, space is the default value of FS, so you can omit -F' '. You can also perform matching in the pattern section of the script:
Code:
$ match=1000 ; echo '2000 1000' | gawk -v matchvar=$match '$2 ~ matchvar {print "it worked"}'
it worked
 
Old 04-14-2012, 05:36 PM   #4
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333

Original Poster
Rep: Reputation: 17
Hi, I also need to match 0, and from further testing, if I set match to 10, I also see an invalid positive match result.

Afaik ~ is for strings, so it worked when I used == for numbers instead.

Code:
$ match=0 ; echo '2000 1000' | gawk -F' ' -v matchvar=$match '{if ($2 ~ matchvar){print "it worked"}}'
it worked

$ match=10 ; echo '2000 1000' | gawk -F' ' -v matchvar=$match '{if ($2 ~ matchvar){print "it worked"}}'
it worked
* This works :
Code:
$ match=0 ; echo '2000 1000' | gawk -F' ' -v matchvar=$match '{if ($2 == matchvar){print "it worked"}}'

Last edited by dazdaz; 04-14-2012 at 05:39 PM.
 
Old 04-15-2012, 06:21 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
If this is SOLVED please mark it as so.
 
  


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
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 12:44 AM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 02:18 AM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM

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

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