LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-10-2017, 10:45 AM   #1
goofygoober
Member
 
Registered: Nov 2017
Posts: 37

Rep: Reputation: Disabled
awk patterns


Hey there, I'm trying to figure out what makes an awk pattern.
I understand generally it goes
pattern {action}
for example
/blah/ {
printf $1;
}

but do functions also count as patterns?
if i have:
while (rose is red){
printf $1;
}
is the "while" considered a pattern in this case?

also, what is an empty pattern? I've tried searching around but keep getting brought to
https://www.gnu.org/software/gawk/ma...ode/Empty.html
which provides a single line awk command but i'd like to see an empty pattern within a script file.
 
Old 12-10-2017, 10:58 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,930

Rep: Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321Reputation: 7321
usually awk will process a file (or stdin) line by line.
as you wrote, awk works like:
Code:
pattern { action }
where pattern means you want to select which lines should be involved. Empty or missing pattern means you want to process all the lines - or input records.
Pattern always written as /<string>/ so it is a string between slashes. while is not a pattern.
 
1 members found this post helpful.
Old 12-10-2017, 11:00 AM   #3
goofygoober
Member
 
Registered: Nov 2017
Posts: 37

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
usually awk will process a file (or stdin) line by line.
as you wrote, awk works like:
Code:
pattern { action }
where pattern means you want to select which lines should be involved. Empty or missing pattern means you want to process all the lines - or input records.
Pattern always written as /<string>/ so it is a string between slashes. while is not a pattern.
Ah! Okay!
So then
{
printf $1
}
IS considered a pattern block? just an empty one?
 
Old 12-10-2017, 11:07 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,331
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
The pattern is still considered to be there it's just that if it is empty then it happens to always evaluate to true and thus always excutes the action.
 
1 members found this post helpful.
Old 12-10-2017, 11:08 AM   #5
goofygoober
Member
 
Registered: Nov 2017
Posts: 37

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by Turbocapitalist View Post
The pattern is still considered to be there it's just that if it is empty then it happens to always evaluate to true and thus always excutes the action.
Awesome, thanks for the info!
 
Old 12-10-2017, 11:22 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,331
Blog Entries: 3

Rep: Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726Reputation: 3726
No problem. Be sure to test things in awk as you think of them. It's pretty simple and practice is the best way of learning. There are a lot of shortcuts, such as the empty pattern, that can be used to great advantage.

Also, the manual page is also worth visiting frequently but the different awk variants have different quality of manual pages.
 
1 members found this post helpful.
  


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: Insert a blank line after each group of patterns matched cristalp Programming 6 10-22-2013 08:07 AM
Awk to Count Multiple patterns in a huge file reach.sree@gmail.com Programming 6 06-08-2012 01:04 PM
Extracting all the matching patterns in each line -Regular expression AWK ksvinaykumar Linux - Newbie 7 04-10-2012 09:48 AM
Using file content as input for awk search patterns srn Programming 2 09-13-2011 02:49 AM
[SOLVED] Awk varying patterns to different file Tauro Linux - Newbie 6 07-29-2011 04:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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