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
615 views
in Technique[技术] by (71.8m points)

bash - Specify other flags in awk script header

I want to write an awk script file using the #!/bin/awk -f header, but I want this script to always use : as a field separator. But for some reason writing #!/bin.awk -F: -f gives me a syntax error. I also want this script to always run on the same file, so I'd like to hardcode that as well. Basically, what I want to work is this:

#!/bin/awk -F: -f -- /etc/passwd

followed by some awk code

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Never use a shebang to call awk as that has no worthwhile benefit over simply calling awk within your shell script but robs you of the ability to separate arguments passed to your shell script into values for the shell to process, values for awk to process use -v, values for awk to process using assignments at the end of the script and file names for awk to run on.

Just write:

#!/usr/bin/env bash
awk -F':' '
whatever
' /etc/passwd

so that if you had to you could trivially tweak it to:

#!/usr/bin/env bash
sort "$1" |
awk -F':' -v foo="$2" '
whatever
' - FS="$3" "$4"

or whatever else you need to do to use the arguments passed to your shell script most appropriately.


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

...