linux - awk command issue to recognize delimiter -


experts, thoughts why delimiter not working in case? '^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 valid points made ed morton in comments area, have updated answer provide more insight on different behavior of awk variants regarding escaping.


my understanding want use ^a delimiter.

you have escape ^ character, messes awk's regex*. way this, prepending double escape sequence \\ ^


- in linux (awk symlinked mawk or gawk, see note):

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

now, mawk has more relaxed behavior on this, possible achieve same results using \ (single escape):

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

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


- in windows (cygwin, mingw, gnutils provide gawk):

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

- in osx (awk 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 find variant of awk available in system, first have locate awk command , use ls follow link chain actual binary, this:

$ 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 system, xubuntu 14.04)


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -