ProgrammingThis 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.
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.
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.
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.
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.
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.
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.