LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 08:37 AM   #1
uncle-c
Member
 
Registered: Oct 2006
Location: The Ether
Distribution: Lubuntu 14.04 LTS, Kali
Posts: 297

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, 08:52 AM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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 08:57 AM.
 
Old 03-03-2009, 09:30 AM   #3
uncle-c
Member
 
Registered: Oct 2006
Location: The Ether
Distribution: Lubuntu 14.04 LTS, Kali
Posts: 297

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 07:23 AM
awk regex question Guest1234 Programming 6 12-25-2007 02:31 PM
Awk scripting and usage of regex to locate a hyperlink grinch Programming 12 04-30-2007 04:50 PM
Newbie SED / AWK / Regex command help request Critcho Linux - Newbie 10 03-19-2007 12:22 PM
regex question Toadman Linux - General 0 12-30-2005 01:59 PM

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

All times are GMT -5. The time now is 12:32 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration