Frage

I'm trying to parse a file that has, for whatever reason, the string "&($)" as record separator and "(@)$" as a field separator. I couldn't get awk to parse the file by specifying these as RS and FS in the BEGIN block. I'm using gnu awk 3.1.7 and it complains saying that there's a syntax error but couldn't find how to escape the dollar sign (assuming that's what it doesn't like).

$ awk 'BEGIN{FS="(@)$" RS="&($)"} {} END{print NR}' some-file.txt
awk: BEGIN{FS="(@)$" RS="&($)"} {} END{print NR}
awk:                   ^ syntax error

Appreciate help from the experts.

Thanks, K

War es hilfreich?

Lösung

you need escape those chars with special meaning in regex.

kent$  cat f                                               
foo(@)$bar(@)$blah&($)foo2(@)$bar2(@)$blah2

kent$  awk 'BEGIN{FS="\\(@\\)\\$";RS="&\\(\\$\\)"}{print NR,NF}' f
1 3
2 3

Andere Tipps

As you are defining two values in the BEGIN{} block, you are missing a semi colon to separate them:

awk 'BEGIN{FS="(@)$"; RS="&($)"} {} END{print NR}' file
                    ^

You can also do

awk 'BEGIN{FS="(@)$"} {} END{print NR}' RS="&($)" file

Regarding the use of these separators, note as well what Kent is commenting in his answer: you need to escape them.

$ cat a
hello(@)$this(@)$is one record&($)and this another one
$ awk 'BEGIN{FS="\\(\\@\\)\\$"} {print $1, NR, NF}' RS="\\&\\(\\$\\)" a
hello 1 3
and this another one
 2 1
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top