awk and regex with {}

0

I want to print whole record if it contain word and some character, which are optional. Script:

/mount[ein]{0,2}/{
print $0}

Unfortunately it doesn't work. Without "{" and "}" it work. I tried with "\" befor "{":

/mount[ein]\{0,2\}/{
    print $0}

Other example. For file:

mountx
mounte
mountee
mounteee

Awk:

awk '/mount[ein]{0,2}$/' file

And result:

//nothing

System: Debian 7.8 Linux ibm 3.2.0-4-amd64 #1 SMP Debian 3.2.68-1+deb7u1 x86_64 GNU/Linux

diego9403

Posted 2015-08-29T17:15:23.713

Reputation: 807

Show your input and desired output. – John1024 – 2015-08-29T19:05:25.433

Input is a text (part of book). I only want to know why regex doesn't work. Are {} don't work in awk. – diego9403 – 2015-08-29T19:21:49.537

1What makes you believe the regex doesn't work? Without telling the input, the expected output and the actual output, it's hard for us to guess. – jlliagre – 2015-08-29T19:53:50.340

Answers

0

Your regular expression works with both a POSIX compliant awk and GNU awk.

Given the fact the the characters e, i, and n must be present from zero to three times but that there is no further constraint, this part can be removed from the pattern as mount followed by anything or nothing does match anyway. The action (print $0) being also the default one need not to be specified.

Your command can then be simplified to:

awk '/mount/'

Should you want the range to be used, you need to specify something after it, here is an example:

$ cat input
foo
mountx
mounte
mountee
mounteee
$ /usr/xpg4/bin/awk '/mount[ein]{0,2}$/' input
mounte
mountee

That is on Solaris where the POSIX awk is in /usr/xpg4/bin.

Same behavior on the Debian based Mint 17.1:

$ awk -V | head -1      
GNU Awk 4.0.1
$ awk '/mount[ein]{1,2}$/' input     
mounte
mountee

However, it looks like mawk, which claims POSIX compliance, doesn't support the interval (brace) expression.

$ mawk -W version
mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan
$ mawk '/mount[ein]{1,2}$/' input
$ 

This is a known bug.

jlliagre

Posted 2015-08-29T17:15:23.713

Reputation: 12 469

I copied your input and command, but in my Debian it does't work. But without "{}" awk show the result, so maybe there is something wrong with my OS? – diego9403 – 2015-08-30T06:08:20.750

Again, please explain what you mean with "it doesn't work", i.e. the actual command run, its input, its expected output and its actual output. – jlliagre – 2015-08-30T07:57:02.300

I am sorry. Output is not this what I suspect. I think it's because of regex, because I try other without "{}". Yesterday I check the same script on other system (Slackware) and it's work, so I think there is something wrong with my Debian.

Even I copy your script and Debian showed nothing, – diego9403 – 2015-08-30T08:09:43.117

Please edit your question with the requested details : What version of awk you are running on that Debian, what output do you get with what command and what input. – jlliagre – 2015-08-30T08:13:47.290

A reason for the command not to output anything might be the input lines are ended with CRLF (Windows style) vs LF (Unix style). Try awk '/mount[ein]{1,2}\r$/' input – jlliagre – 2015-08-30T08:17:10.707

It doesn't work too. Maybe it's something with configuration? – diego9403 – 2015-08-30T08:23:19.020

You might be using mawk which indeed doesn't support this syntax. See my updated answer. – jlliagre – 2015-08-30T08:47:35.213

mawk 1.3.3 Nov 1996, Copyright (C) Michael D. Brennan

compiled limits: max NF 32767 sprintf buffer 2040 Can I change it? – diego9403 – 2015-08-30T08:47:50.227

Yes, I figure it it in the meantime. Install GNU awk to fix the issue. – jlliagre – 2015-08-30T08:49:45.687

2

Example tst.txt:

mountx
mounte
mountee
mounteee

awk (GNU Awk 3.1.7) script

awk --re-interval '/mount[ein]{0,2}$/' tst.txt > tst1.txt

result

mounte
mountee

man awk

-W re-interval
--re-interval
          Enable the use of interval expressions in regular expression matching (see Regular Expressions, below).  Interval expressions were not traditionally available in the AWK
          language.  The POSIX standard added them, to make awk and egrep consistent with each other.  However, their use is likely to break old AWK programs, so  gawk  only  pro-
          vides them if they are requested with this option, or when --posix is specified.

ДядюшкаАУ

Posted 2015-08-29T17:15:23.713

Reputation: 21

0

Not sure about your mount command format, and the question is listed as a "awk" question, with no awk in your comment.

Is this what you're looking for?

"print all lines that contain a 0" (or whatever string you're searching for) from a file - use the command

cat temp.txt | grep 0 | awk '{print $0}'

will result in

0 9 8 7 6 5 4 3 2

5 4 3 2 1 0 9 8

where temp.txt is

1 2 3 4 5 6 7

0 9 8 7 6 5 4 3 2

9 8 7 6 5 4

8 7 6 5 4 3

7 6 5 4 3

6 5 4 3

5 4 3 2 1 0 9 8

4

3

2

WillW

Posted 2015-08-29T17:15:23.713

Reputation: 13

Result is ok, but I have to write it entirely in awk script and I don't know why "{}" doesn't work.Do you know why my regex doesn't work? – diego9403 – 2015-08-29T17:50:25.530

Not without knowing what /mount[ein] is – WillW – 2015-08-29T18:12:03.810

"mount" <- this is the word, which is constant, but characters e i n can be in line, but don't have to and 3 characters (e i n) can be repeated zero, one or two times. – diego9403 – 2015-08-29T18:22:06.080