Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

awk: warning: escape sequence `]' treated as plain `]'

echo "A: B: C: D:" | awk -F'[:]' '{print $1}'

Output:

awk: warning: escape sequence `]' treated as plain `]'

A

I am getting the above warning message, I have tried multiple options to escape the ] but that didn't resolve.

I want to use both the delimiters : and print exactly the alphabets A, B, C, D by printing $1, $2, $3, $4 respectively.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this is what you're trying to do:

$ echo 'A: B: C: D:' | awk -F':\\[[:space:]]*' '{print $2}'
B

Note that I added [[:space:]]* as I don't think you want leading spaces in the fields after $1.

String literals as used in your FS setting get parsed twice, once when the script is read and again when executed so escapes need to be doubled up (google it and/or read the awk man page or book). In this case on the first pass \\ gets converted to \ and on the second pass \ gets converted to which is what you want.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...