使用 bash shell 脚本和 awk 提取子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2573009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Substring extraction using bash shell scripting and awk
提问by GobiasKoffi
So, I have a file called 'dummy' which contains the string:
所以,我有一个名为“dummy”的文件,其中包含以下字符串:
"There is 100% packet loss at node 1".
I also have a small script that I want to use to grab the percentage from this file. The script is below.
我还有一个小脚本,我想用它来从这个文件中获取百分比。脚本如下。
result=`grep 'packet loss' dummy` |
awk '{ first=match(result=$( grep 'packet loss' dummy |
awk '{ first=match(result=$( grep 'packet loss' | grep -o "[0-9]\+%" )
,"[0-9]+%")
last=match(awk '{print }'
," packet loss")
s=substr($ results=$(awk '/packet loss/{for(i=1;i<=NF;i++)if($i~/[0-9]+%$/)print $i}' file)
$ echo $results
100%
,first,last-first)
print s}' )
echo $result
,"[0-9]+%")
last=match(i=`expr "There is 98.76% packet loss at node 1" : '[^0-9.]*\([0-9.]*%\)[^0-9.]*'`; echo $i;
," packet loss")
s=substr(awk '/packet loss/ { print }' dummy
,first,last-first)
print s}'
echo $result
I want the value of $result to basically be 100% in this case. But for some reason, it just prints out a blank string. Can anyone help me?
在这种情况下,我希望 $result 的值基本上是 100%。但出于某种原因,它只是打印出一个空白字符串。谁能帮我?
回答by Paused until further notice.
You would need to put the closing backtick after the end of the awkcommand, but it's preferable to use $()instead:
您需要在awk命令结束后放置结束反引号,但最好使用$():
but you could just do:
但你可以这样做:
##代码##回答by Steve Emmerson
Try
尝试
##代码##instead.
反而。
回答by ghostdog74
the solution below can be used when you don't know where the percentage numbers are( and there's no need to use awk with greps)
当您不知道百分比数字在哪里时,可以使用下面的解决方案(并且不需要将 awk 与 grep 一起使用)
##代码##回答by Arvind Pai
You could do this with bash alone using expr.
您可以单独使用 bash 使用expr.
This extracts the substring matching the regex within \( \).
这将提取与\( \).
回答by janks
Here I'm assuming that the output lines you're interested in adhere strictly to your example, with the percentage value being the only variation.
在这里,我假设您感兴趣的输出行严格遵守您的示例,百分比值是唯一的变化。
With that assumption, you really don't need anything more complicated than:
有了这个假设,你真的不需要比以下更复杂的东西了:
##代码##This quite literally means "print the 3rd field of any lines containing 'packet loss' in them". By default awk treats whitespace as field delimiters, which is perfect for you.
这很字面意思是“打印其中包含'数据包丢失'的任何行的第三个字段”。默认情况下,awk 将空格视为字段分隔符,这非常适合您。
If you are doing more than simply printing the percentage, you could save the results to a shell variable using backticks, or redirect the output to a file. But your sample code simply echoes the percentages to stdout, and then exits. The one-liner does the exact same thing. No need for backticks or $() or any other shell machinations whatsoever.
如果您要做的不仅仅是打印百分比,您可以使用反引号将结果保存到 shell 变量,或将输出重定向到文件。但是您的示例代码只是将百分比回显到标准输出,然后退出。one-liner 做同样的事情。不需要反引号或 $() 或任何其他外壳程序。
NB: In my experience, piping the output of grep to awk is usually doing something that awk can do all by itself.
注意:根据我的经验,将 grep 的输出通过管道传递给 awk 通常是在做一些 awk 可以自己完成的事情。

