LinuxQuestions.org
Review your favorite Linux distribution.
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 03-03-2009, 07:37 AM   #1
uncle-c
Member
 
Registered: Oct 2006
Location: The Ether
Distribution: Ubuntu 16.04.7 LTS, Kali, MX Linux with i3WM
Posts: 299

Rep: Reputation: 30
Awk regex question


Code:
[uncle-c@localhost temp]$ cat awktxt
Type    Memory (Kb)     Location        Serial #        HD Size (Mb)
XT      640             D402            MG0010          0
386     2048            D403            MG0011          100
486     4096            D404            MG0012          270
386     8192            A423            CC0177          400
486     8192            A424            CC0182          670
286     4096            A423            CC0183          100
286     4096            A425            CC0184          80
Mac     4096            B407            EE1027          80
Apple   4096            B406            EE1028          40
68020   2048            B406            EE1029          80
68030   2048            B410            EE1030          100
$unix   16636           A405            CC0185          660
"trs80"  64             Z101            EL0020          0
Example 1:

Code:
[uncle-c@localhost temp]$ awk ' $1 ~ /[(A|M)*]/  { print $0 }' awktxt
Mac     4096            B407            EE1027          80
Apple   4096            B406            EE1028          40
Example 2:

Code:
[uncle-c@localhost temp]$ awk ' $1 ~ /[(A|M)]*/  { print $0 }' awktxt
Type    Memory (Kb)     Location        Serial #        HD Size (Mb)
XT      640             D402            MG0010          0
386     2048            D403            MG0011          100
486     4096            D404            MG0012          270
386     8192            A423            CC0177          400
486     8192            A424            CC0182          670
286     4096            A423            CC0183          100
286     4096            A425            CC0184          80
Mac     4096            B407            EE1027          80
Apple   4096            B406            EE1028          40
68020   2048            B406            EE1029          80
68030   2048            B410            EE1030          100
$unix   16636           A405            CC0185          660
"trs80"  64             Z101            EL0020          0
Could someone explain the difference between the two regexes ? I had initially thought that "example 2" would have also printed any lines in the first field containing "A" or "M". The regex [(A|M)]+ gives the same output as "example 1." It has got me quite confused !

Thanks,
UC
 
Old 03-03-2009, 07:52 AM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
The first expression, [(A|M)*], matches any string containing any of the symbols "(", "A", "|", "M", ")" or "*"; the second [(A|M)]*] matches any expression containing zero or more of the symbols "(", "A", "|", "M" or ")"; the third is the same as the second except one or more match is required.

To do what I think you want to do, the correct expression is [AM]+.

Last edited by PTrenholme; 03-03-2009 at 07:57 AM.
 
Old 03-03-2009, 08:30 AM   #3
uncle-c
Member
 
Registered: Oct 2006
Location: The Ether
Distribution: Ubuntu 16.04.7 LTS, Kali, MX Linux with i3WM
Posts: 299

Original Poster
Rep: Reputation: 30
Ahhhhh ! I see !! Thanks for explaining !

cheers,
UC
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
awk regex question Guest1234 Programming 6 12-25-2007 01:31 PM
Awk scripting and usage of regex to locate a hyperlink grinch Programming 12 04-30-2007 03:50 PM
Newbie SED / AWK / Regex command help request Critcho Linux - Newbie 10 03-19-2007 11:22 AM
regex question Toadman Linux - General 0 12-30-2005 12:59 PM

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

All times are GMT -5. The time now is 09:02 PM.

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