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 |
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.
|
 |
03-03-2017, 12:44 PM
|
#1
|
Member
Registered: Nov 2015
Posts: 397
Rep: 
|
AWK(ward) pattern matching problem?
Code:
#works as expected
~/dir $ awk '{ print $0}' inventory-shipped
Jan 13 25 15 115
Feb 15 32 24 226
Mar 15 24 34 228
Apr 31 52 63 420
May 16 34 29 208
Jun 31 42 75 492 lastCol
Jul 24 34 67 436
Aug 15 34 47 316
Sep 13 55 37 277
Oct 29 54 68 525
Nov 20 87 82 577
Dec 17 35 61 401
# Match any word that has a "J". Works as expected
~/dir $ awk '/J/ { print $0}' inventory-shipped
Jan 13 25 15 115
Jun 31 42 75 492 lastCol
Jul 24 34 67 436
~/dir $ awk '/J.*/ { print $0}' inventory-shipped
Jan 13 25 15 115
Jun 31 42 75 492 lastCol
Jul 24 34 67 436
# /J*/ matches any word/line containing J, JJ, JJJ, JJJJ, ...
#Does NOT work as expected.
#Incorrect output:
#There should be ONLY outputs for lines with J, JJ, JJJ, JJJJ, ...
~/dir $ awk '/J*/ { print $0}' inventory-shipped
Jan 13 25 15 115
Feb 15 32 24 226
Mar 15 24 34 228
Apr 31 52 63 420
May 16 34 29 208
Jun 31 42 75 492 lastCol
Jul 24 34 67 436
Aug 15 34 47 316
Sep 13 55 37 277
Oct 29 54 68 525
Nov 20 87 82 577
Dec 17 35 61 401
What did I missed?
|
|
|
03-03-2017, 01:01 PM
|
#2
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
J* means any line with zero or more J's. See
and then scroll down to "An atom followed by '*' matches a sequence of 0 or more matches of the atom."
Maybe you mean a plus + instead.
Code:
awk '/J+/' inventory-shipped
By the way, you can leave off print $0 since it goes without saying. There are a lot of shortcuts like that in awk
|
|
1 members found this post helpful.
|
03-03-2017, 01:02 PM
|
#3
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep: 
|
Thank you.
|
|
|
03-03-2017, 01:06 PM
|
#4
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
No worries. Also, you can anchor the search to the beginning of the line with a caret ^ at the start of the pattern:
Code:
awk '/^J+/' inventory-shipped
That will match
Code:
Jan 13 25 15 115
Jun 31 42 75 492 lastCol
Jul 24 34 67 436
but not
Code:
aJn 13 25 15 115
uJn 31 42 75 492 lastCo
Aug 24 34 67 436 J
|
|
|
03-03-2017, 01:11 PM
|
#5
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
Quote:
An atom followed by '*' matches a sequence of 0 or more matches of the atom.
|
I must be reading this wrong.
If I have a string equal to: 1111111111111111111111111
And I search for '/A*/' it will not 'match' and will not exit as successful. Even though there are 0 occurrences. So which is it? Does '/A*/' successfully match when there are no matches (0) or not?
|
|
|
03-03-2017, 01:17 PM
|
#6
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
Quote:
Originally Posted by szboardstretcher
I must be reading this wrong.
If I have a string equal to: 1111111111111111111111111
And I search for '/A*/' it will not 'match' and will not exit as successful. Even though there are 0 occurrences. So which is it? Does '/A*/' successfully match when there are no matches (0) or not?
|
Which versions of awk are you using? It should match all the lines. It works for me on GNU Awk, OpenBSD's Awk, and Mawk, just to check three.
|
|
1 members found this post helpful.
|
03-03-2017, 01:26 PM
|
#7
|
Senior Member
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278
|
My bad: My eyes didn't detect that i had '/AA*/' and not '/A*/'
I blame the writers of awk of course.
|
|
|
All times are GMT -5. The time now is 05:50 AM.
|
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
|
|