gawk -F[][] '{ print $0" -> "$1"\t"$2; }'
$ gawk -F[][] '{ print $0" -> "$1"\t"$2; }'
titi[toto]tutu
titi[toto]tutu -> titi toto
1) You must set the FS
before entering the main parsing loop. You could do:
awk 'BEGIN { FS="[\\[\\]]"; } { print $0" -> "$1"\t"$2; }'
Which executes the BEGIN
clause before parsing the file.
I have to escape the [
character twice: one because it is inside a quoted string. And another once because gawk mandate it inside a bracket expression.
I personnaly prefer to use the -F
flag which is less verbose.
2) FS="[\[\]]"
is wrong, because you are inside a quoted string, this escape the character inside the string. Awk will see: [[]]
which is an invalid bracket expression.
3) FS="[]"
is wrong because it is an empty bracket expression trying to match nothing
4) FS="\[\]"
is wrong again because it is error 2) and 3) together :)
gawk manual says: The regular expressions in awk are a superset of the POSIX specification
. This is why you can use either: [\\[\\]]
or [][]
. The later being the posix way.
To include a literal ']' in the list, make it the first character
See:
Posix Regex specification: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html#tag_09_04
Posix awk specification: http://pubs.opengroup.org/onlinepubs/009695399/utilities/awk.html
Gnu Awk manual: http://www.gnu.org/software/gawk/manual/gawk.html#Bracket-Expressions