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 01-15-2018, 11:48 PM   #1
L4Z3R
Member
 
Registered: Jan 2018
Posts: 34

Rep: Reputation: Disabled
How can I use variables with Awk


Hi

I googled this and found some ideas. Unfortunately, it didn't work for me. Maybe I missed something or I wrote it wrong. It's a basic search script.

Code:
#!/bin/bash

var1=05
var2=07

awk -v a=$var1 -v b=$var2 "/a/ && /b/" nums.txt
Code:
./testscript
When I run the script there is no errors nor any output from above code

However, if I run the code without using variables, it does work.

Code:
awk "/05/ && /07/" nums.txt 
05 07 09 11 33
I appreciate any help. Thanks
 
Old 01-16-2018, 12:22 AM   #2
L4Z3R
Member
 
Registered: Jan 2018
Posts: 34

Original Poster
Rep: Reputation: Disabled
Code:
#!/bin/bash

var1=05
var2=07


awk "/$var1/ && /$var2/" nums.txt
I modified the script and took a gamble and use dollar signs in the variables. I saw many tutorials that awk doesn't use variable with dollar signs like bash does. Anyway I did it and it did work.

Code:
./testscript
05 07 09 11 33
Very surprising
 
Old 01-16-2018, 12:33 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,320
Blog Entries: 3

Rep: Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725
In your solution, it is the shell doing the work rather than awk. Pay attention to the quotes.

A way to follow through on the method you show in the first post can be done. However, it might not be so obvious. /pattern/ is really a shortcut for $0 ~ /pattern/ so with that in mind:

Code:
awk -v a=$var1 -v b=$var2 '$0~a && $0~b'
Again see 'man awk' for the details. Scroll down to the ~ explanation. You can also bind patterns to specific fields.

Code:
$1 ~ /pattern/
There only the first field is examined for the pattern, not the whole line.
 
1 members found this post helpful.
Old 01-16-2018, 01:13 AM   #4
L4Z3R
Member
 
Registered: Jan 2018
Posts: 34

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
In your solution, it is the shell doing the work rather than awk. Pay attention to the quotes.

A way to follow through on the method you show in the first post can be done. However, it might not be so obvious. /pattern/ is really a shortcut for $0 ~ /pattern/ so with that in mind:

Code:
awk -v a=$var1 -v b=$var2 '$0~a && $0~b'
Again see 'man awk' for the details. Scroll down to the ~ explanation. You can also bind patterns to specific fields.

Code:
$1 ~ /pattern/
There only the first field is examined for the pattern, not the whole line.
Hi Turbocapitalist. I just tried your solution and it worked like a charm!!!

LQ is such a good source to learn from the linux gurus.

+1
 
Old 01-16-2018, 10:32 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,665
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
And, for whatever it's worth, I tend to "take a lesson from Larry Wall," the long-ago inventor of the Perl programming language. Back in the day, Larry was dissatisfied with Awk and set out to improve upon it – and, I think, he did. The rest is history ...

In fact, there is a tool called a2p which does a reasonably good automatic job of converting Awk to Perl.

I simply find that awk, good and venerable tool that it unarguably is, has "certain limitations and annoyances" that, well, annoy me. Perl goes farther. (Of course, "much, much farther.") So, in cases where "I need to roll a new awk-script," I turn to Perl instead.

Last edited by sundialsvcs; 01-16-2018 at 10:34 AM.
 
2 members found this post helpful.
Old 01-16-2018, 12:30 PM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,320
Blog Entries: 3

Rep: Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725
I second the recommendation for perl5. It's such a vital tool when it comes to text manipulation that learning even a little will pay for itself many times over. Awk is quite good for certain one-liners, it's useful and easy, but truth be told I got by for over two decades without it, just using perl instead. With just a little familiarity it is possible to use it as quickly as with Awk. I tend to reach for it first over sed or awk.
 
1 members found this post helpful.
Old 01-16-2018, 02:01 PM   #7
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
My own learning sake, I coded a test file up called nums.txt .

However if I run the awk command that was recommended, nothing happens. Not sure how to run this awk statement against the nums.txt file.




Quote:
Originally Posted by Turbocapitalist View Post
In your solution, it is the shell doing the work rather than awk. Pay attention to the quotes.

A way to follow through on the method you show in the first post can be done. However, it might not be so obvious. /pattern/ is really a shortcut for $0 ~ /pattern/ so with that in mind:

Code:
awk -v a=$var1 -v b=$var2 '$0~a && $0~b'
Again see 'man awk' for the details. Scroll down to the ~ explanation. You can also bind patterns to specific fields.

Code:
$1 ~ /pattern/
There only the first field is examined for the pattern, not the whole line.
 
Old 01-16-2018, 02:04 PM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,320
Blog Entries: 3

Rep: Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725
Quote:
Originally Posted by JockVSJock View Post
My own learning sake, I coded a test file up called nums.txt .

However if I run the awk command that was recommended, nothing happens. Not sure how to run this awk statement against the nums.txt file.
Did you define the shell variables first? Please show the full context.

Last edited by Turbocapitalist; 01-16-2018 at 02:09 PM. Reason: spelling
 
Old 01-16-2018, 08:12 PM   #9
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
Just like above

Code:
awk -v a=$var1 -v b=$var2 '$0~a && $0~b'
Except I created the file nums.txt too. Not sure how to feed this into the awk statement.

thanks



Quote:
Originally Posted by Turbocapitalist View Post
Did you define the shell variables first? Please show the full context.
 
Old 01-17-2018, 04:00 AM   #10
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,320
Blog Entries: 3

Rep: Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725
Since you didn't show the creation of the variables, I'm guessing that's what's missing:

Code:
var1='05'
var2='20'
awk -v a=$var1 -v b=$var2 '$0~a && $0~b' num.txt
 
Old 01-17-2018, 05:09 PM   #11
JockVSJock
Senior Member
 
Registered: Jan 2004
Posts: 1,420
Blog Entries: 4

Rep: Reputation: 164Reputation: 164
This code outputs

Code:
#!/bin/bash 

var1=05
var2=07

awk -v a=$var1 -v b=$var2 '$0~a && $~b' nums.txt
nums.txt was what I forgot and this is the contents if someone was to cat that file:

Code:
cat nums.txt

05 07 09 11 33
03 05 11 15 07
09 07 20 19 05
The only thing that I've noticed is that it returns all rows, should it?

thanks
 
Old 01-18-2018, 12:25 AM   #12
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,320
Blog Entries: 3

Rep: Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725
Quote:
Originally Posted by JockVSJock View Post
The only thing that I've noticed is that it returns all rows, should it?
Yes, it should if $var1 and $var2 both contain numbers found in all the rows. Setting them to 05 and 07 would do that. If you add a fourth row with just one of those two numbers, it will not be printed. Only rows containing both numbers, in either order, will be printed.
 
Old 01-18-2018, 01:45 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,894

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
Quote:
Originally Posted by JockVSJock View Post
Code:
awk -v a=$var1 -v b=$var2 '$0~a && $~b' nums.txt
                                   ^^^

The only thing that I've noticed is that it returns all rows, should it?

thanks
A 0 is missing probably.
 
  


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
[SOLVED] awk and variables allan426 Linux - Software 5 10-04-2012 02:05 PM
Awk variables carlr Programming 3 03-17-2012 06:05 PM
Awk variables danicobos Linux - General 7 10-27-2011 09:43 AM
Using variables with awk dots Linux - Newbie 4 07-07-2009 03:00 PM
using variables in awk vgr12386 Programming 14 06-24-2009 04:19 AM

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

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