views:

289

answers:

3
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?

A: 

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
ghostdog74
The first proposed solution is not a good idea - for multiple reasons. The second will work, but is overkill compared to fixing the regex to '`~!\+`'. Using '-F' is nicer than setting FS in a BEGIN block, but you should use single quotes around it rather than double quotes - especially when it contains a backslash.
Jonathan Leffler
can you then show the code and results of using '~!\+' ?
ghostdog74
"The first proposed solution is not a good idea" -- agreed indeed!!
Xolve
+1  A: 

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

Chris Dail
+1  A: 
 $ echo -n 'a001~!+rr001~!+1~!+TEST DATA 1' | awk 'BEGIN {FS="~!\\+"} {print $2}' 
rr001 

Double escaping also seems to do the job.