2

I am facing a rather strange issue when trying to escape a parenthesis in awk. My file has the following line

79-Au-196 isomer state population  0.153658     mb (m1 E= 0.5957MeV Jp=-12.0)

So I want to extract column 5 (i.e. 0.153658). To do that I am running the following

>gawk "/79-Au-196 isomer state population/{if($7=="\(m1")print $5}" Au_test.out

and what I get is

> gawk: /79-Au-196 isomer state population/{if($7==\(m1)print $5} gawk: 
>                                                  ^ backslash not last character on line

I tried replacing "\(m2" with "%s(m2" or '\(m2' but nothing actually worked.

Any idea on how to make it work?

mklement0
  • 382,024
  • 64
  • 607
  • 775
Thanos
  • 594
  • 2
  • 7
  • 28
  • Just use single quotes instead of double, like this: `gawk '/79-Au-196 isomer state population/{if($7=="(m1")print $5}'` – randomir Jul 02 '17 at 18:24
  • @randomir : Thanks a lot for your comment. I also tried this, but I got the following `gawk: '/79-Au-196 gawk: ^ invalid char ''' in expression` – Thanos Jul 02 '17 at 18:32
  • @randomir: Good advice in general (Unix platforms), but the OP is running the command on _Windows_. – mklement0 Jul 02 '17 at 18:43
  • 1
    @mklement0, You're right. I haven't used Windows in a while. :) – randomir Jul 02 '17 at 19:10

1 Answers1

1

tl;dr:

gawk "/79-Au-196 isomer state population/{if($7==\"(m1\")print $5}" Au_test.out

  • On Windows, you must use "..." (double quote) around your gawk script.

    • '...' quoting (single quotes) - which should always be used to delimit awk scripts on Unix platforms - is not recognized by cmd.exe.
  • This then requires \-escaping any " instances inside the script.

  • By contrast, ( (the parenthesis) does not need escaping in $7==\"(m1\", because from gawk's perspective you're using it inside a string literal that is compared to a field value with ==.

mklement0
  • 382,024
  • 64
  • 607
  • 775