使用Awk搜索包含空格的字符串 [英] Using Awk to search for a string that has spaces
本文介绍了使用Awk搜索包含空格的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法使用awk在文件中搜索字符串的最后一次出现.我正在将字符串传递给脚本示例主机名Ping失败".我不断收到awk:^未终止的字符串. #!/bin/sh
I'm having trouble searching for the last occurrence of a string in a file using awk. I'm passing a string to the script example "Ping has failed on hostname". I keep getting awk: ^ unterminated string. #!/bin/sh
LOG=/opt/netcool/omnibus/log/mttrapd.log
TMP_FILE=sitescope.$$
args="$*"
#ruby sitescope.rb
echo "looking for $1 "
tail -1000 $LOG > $TMP_FILE
echo "WORD = $args"
awk '"/'$args'/" {f=$0} END{print f}' $TMP_FILE > data.out
rm -f $TMP_FILE
推荐答案
awk字符串周围的单引号的要点是将所有内容保留在第一个参数中(并防止shell替换).将参数作为
The point of the single-quotes around the awk string is to keep everything in the first argument (and prevent shell substitution). You can be a bit more flexable with how you put that argument together as
awk "/$args/"' {f=$0} END{print f}' $TMP_FILE > data.out
这篇关于使用Awk搜索包含空格的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文