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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-13-2012, 08:34 PM
|
#1
|
Member
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333
Rep:
|
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.
|
|
|
04-13-2012, 10:57 PM
|
#2
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,811
|
Quote:
Originally Posted by dazdaz
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.
|
04-14-2012, 12:18 AM
|
#3
|
Member
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709
|
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
|
|
|
04-14-2012, 05:36 PM
|
#4
|
Member
Registered: Aug 2003
Location: Europe
Distribution: RHEL, CentOS, Ubuntu
Posts: 333
Original Poster
Rep:
|
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.
|
|
|
04-15-2012, 06:21 AM
|
#5
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,033
|
If this is SOLVED please mark it as so.
|
|
|
All times are GMT -5. The time now is 07:53 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|