Bash:从命令行输出中获取部分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12223615/
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
Bash: Grab part of string from a command line output
提问by Michael Le
I am running a command in CentOS that gives me an output of a string and I want to grab a certain part of that output and set it to a variable.
我在 CentOS 中运行一个命令,它给我一个字符串的输出,我想获取该输出的某个部分并将其设置为一个变量。
I run the command ebi-describe-env.
我运行命令 ebi-describe-env。
My output as follows:
我的输出如下:
ApplicationName | CNAME | DATECreated | DateUpdated | Description | EndpointURL |
EnvironmentID | EnvironmentName | Health | Stack | Status | TemplateName |
Version Label --------------------------
Web App | domain.com | 2012-02-23 | 2012-08-31 | |
anotherdomain.com | e-8sgkf3eqbj | Web-App-Name | Status |
Linux | Ready | N/A | 20120831 - daily
I want to grab the '20120831 - daily' part of the string (this string will always change but stays in the same place) and set it to a variable.
我想获取20120831 - daily字符串的 ' ' 部分(此字符串将始终更改但保持在同一位置)并将其设置为变量。
Originally I thought I could use grep or sed and print a line after each '|' and set the 13th line to a variable.
最初我以为我可以使用 grep 或 sed 并在每个“|”后打印一行 并将第 13 行设置为变量。
I'm very new to bash scripting, so any help would be great. Thank you.
我对 bash 脚本很陌生,所以任何帮助都会很棒。谢谢你。
回答by Levon
Using awk:
使用awk:
awk -F"|" '{print $NF}'
this will work:
这将起作用:
echo " Web App | domain.com | 2012-02-23 | 2012-08-31 | | anotherdomain.com |
e-8sgkf3eqbj | Web-App-Name | Status | Linux | Ready | N/A |
20120831 - daily" | awk -F"|" '{print $NF}'
and yield:
和产量:
20120831 - daily
To assign to a variable(data.txtcontains your string just for simplicity, it also works the echoabove):
分配给一个变量(data.txt为了简单起见包含你的字符串,它也适用于echo上面):
$ myvar=$(awk -F"|" '{print $NF}' data.txt)
$ echo $myvar
20120831 - daily
Explanation
解释
the -Fsets the input field separator, in this case to |. NFis a built-in awkvariable that denotes the number of input fields, and the $in front of the variable accesses that element, i.e., in this case the last field in the line ($NF).
的-F设置输入字段分隔符,在这种情况下|。NF是一个内置awk变量,表示输入字段的数量,$变量前面的 访问该元素,即在这种情况下,行 ( $NF) 中的最后一个字段。
Alternatively: You could grab each of the last three fields separated by white space (the awkdefault) with this:
或者:您可以使用以下方法获取由空格(awk默认)分隔的最后三个字段中的每一个:
awk '{print $(NF-2), $(NF-1), $NF}'
回答by Jeremy J Starcher
Levon's answer works great, but I just had to show there are always other ways with shell scripting.
Levon 的回答很有效,但我只需要说明 shell 脚本总是有其他方法。
This one uses the basic tool called cut
这个使用称为的基本工具 cut
echo "Web App | domain.com | 2012-02-23 | 2012-08-31 | | anotherdomain.com | e-8sgkf3eqbj | Web-App-Name | Status | Linux | Ready | N/A | 20120831 - daily" | cut -d"|" -f13
回答by Neil
I know that this has been accepted already, but here's how to do it in pure bash:
我知道这已经被接受了,但这里是如何在纯 bash 中做到这一点:
string="Web App | domain.com | 2012-02-23 | 2012-08-31 | | anotherdomain.com | e-8sgkf3eqbj | Web-App-Name | Status | Linux | Ready | N/A | 20120831 - daily"
myvar="${string##*| }"
echo "$myvar"

