简体   繁体   中英

awk command issue to recognize delimiter

Experts, any thoughts why delimiter not working in my case? The '^A' is a real '^A' string, not ASCII value 1.

cat 2.txt
123^A9343784^A2207983400
45^A1270843^A66789439
67^A188285^A28075164
8^A91183^A27049564
9^A128589^A7283486
100^A84325^A7043462

cat 2.txt | awk -F'^A' '{print $1 }'
123^A9343784^A2207983400
45^A1270843^A66789439
67^A188285^A28075164
8^A91183^A27049564
9^A128589^A7283486
100^A84325^A7043462

BTW, working on Mac OSX/Linux.

thanks in advance, Lin

EDIT

After some valid points made by Ed Morton in the comments area, I have updated my answer to provide slightly more insight on the different behavior of awk variants regarding escaping.


My understanding is that you want to use ^A as delimiter.

You have to escape the ^ character, as it messes with awk's regex*. The way to do this, is by prepending the double escape sequence \\\\ to ^


- In Linux ( awk is usually symlinked to mawk or gawk , see NOTE):

$ cat 2.txt | awk -F'\\^A' '{print $1 }' # mawk, gawk

Now, mawk has a slightly more relaxed behavior on this, so it is possible to achieve the same results using only \\ (single escape):

$ cat 2.txt | awk -F'\^A' '{print $1 }' # mawk (note the single backslash here)

however, in general, this should be avoided (especially if used in a script or as a passe partout one-liner -portability comes to mind-), since other awk variants will treat this differently and a variety of unwanted outcomes will occur (some even well-disguised as legitimate ones in complex situations)


- In Windows ( cygwin , MinGW , gnutils provide gawk ):

$ cat 2.txt | awk -F'\\^A' '{print $1 }' # gawk

- In OSX ( awk is by default nawk ):

$ cat 2.txt | awk -F'\\^A' '{print $1 }' # nawk

All these yield:

123
45
67
8
9
100

* You can find more information on awk's Regular Expressions here .


NOTE

In order to find which variant of awk is available in your system, first you have to locate the awk command itself and then use ls to follow the link chain up to the actual binary, like this:

$ which awk
/usr/bin/awk
$ ls -l /usr/bin/awk
lrwxrwxrwx 1 root root ... /usr/bin/awk -> /etc/alternatives/awk
$ ls -l /etc/alternatives/awk
lrwxrwxrwx 1 root root ... /etc/alternatives/awk -> /usr/bin/mawk

(example taken from my system, Xubuntu 14.04)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM