echo -n 'a001~!+rr001~!+1~!+TEST DATA 1' | awk 'BEGIN {FS="~!+"} {print $2}'
I have the field separator set to "~!+" and want to print the second field. AWK prints an extraneous + with rr001 as +rr001 .
What am I doing wrong?
echo -n 'a001~!+rr001~!+1~!+TEST DATA 1' | awk 'BEGIN {FS="~!+"} {print $2}'
I have the field separator set to "~!+" and want to print the second field. AWK prints an extraneous + with rr001 as +rr001 .
What am I doing wrong?
do it another way
$ echo 'a001~!+rr001~!+1~!+TEST DATA 1' | awk -F"+" '{gsub(/~!$/,"",$2);print $2}'
rr001
or this
$ echo 'a001~!+rr001~!+1~!+TEST DATA 1' | awk -F"[~][!][+]" '{print $2}'
rr001
or
$ echo 'a001~!+rr001~!+1~!+TEST DATA 1' | awk -F'~!\\+' '{print $2}'
rr001
Your problem is that your match criteria '~!+' is a regular expression.
From the documentation: "+ This symbol is similar to ‘*’, except that the preceding expression must be matched at least once. This means that ‘wh+y’ would match ‘why’ and ‘whhy’, but not ‘wy’, whereas ‘wh*y’ would match all three of these strings."
So essentially you are asking to match ~! or ~!!, etc. So you are not matching on the + at all. This is why you see the + in the output. You should be able to use '~!\\+
' to get your expression to work