LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Awk regex question (https://www.linuxquestions.org/questions/linux-newbie-8/awk-regex-question-708788/)

uncle-c 03-03-2009 07:37 AM

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

PTrenholme 03-03-2009 07:52 AM

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]+.

uncle-c 03-03-2009 08:30 AM

Ahhhhh ! I see !! Thanks for explaining !

cheers,
UC


All times are GMT -5. The time now is 01:59 AM.