我正在阅读shell脚本在线课程,因为我的工作要求我学习shell脚本。
我遇到了“awk”和“nawk”命令,我的学习还没有达到目的。
简而言之,我知道awk / nawk搜索特定模式并在找到匹配项时执行操作。
尽管如此,我还是无法理解以下行是什么意思:
eval $( cat ${MMORPHYDIR}/${PWFILE} | nawk ' /^#BEGIN '${ENV_NAME}'/,/^#END '${ENV_NAME}'/ { print }' | egrep "${USEFULL_PARAM}" )
请帮助我理解这条线的作用或意图。
答案 0 :(得分:1)
... awk '/start/,/end/'
在开始和结束模式之间打印记录。 {print}
可以省略,因为它是隐含的。脚本中的^
表示行的开头。
请注意,cat和eval是不必要的,同样可以写为
$ awk '...' "${MMORPHYDIR}/${PWFILE}"
grep也可以包含在awk
脚本中。
答案 1 :(得分:1)
awk
is THE standard, general purpose tool for manipulating text on all UNIX-like systems. There are various flavors of awk, all with the same core functionality plus some differences. nawk
is the very unfortunately named new awk because it was named that about 30 years ago as the successor to 1977s old awk (e.g. /bin/awk on Solaris, aka old, broken awk which must never be used) and is now actually best avoided as it doesn't even support the minimal awk functionality required by POSIX (e.g. character classes). Important lesson there: never use the word "new" in the name of any software entity!
The best awk to use these days is GNU awk, gawk
, as it supports all POSIX functionality plus a ton of useful extensions, is generally available, is extremely well documented, and has a massive user base.
wrt:
eval $( cat ${MMORPHYDIR}/${PWFILE} | nawk ' /^#BEGIN '${ENV_NAME}'/,/^#END '${ENV_NAME}'/ { print }' | egrep "${USEFULL_PARAM}" )
That is a complete mess, doing literally about a dozen things that should never be done in shell or in awk. Trying to explain it would be like trying to explain someone mixing concrete with a screwdriver. Forget you ever saw it and move on.
To learn awk, read the book Effective Awk Programming, 4th Edition, by Arnold Robbins.