从多行输出中复制特定字符串并将其分配给变量

时间:2016-12-13 17:10:38

标签: linux bash shell scripting

当我为serverstatus.sh运行WebSphere命令时,我得到以下输出

ADMU0116I: Tool information is being logged in file
           /opt/IBM/WebSphere/AppServer/profiles/Server1/logs/serverStatus.log
ADMU0128I: Starting tool with the Server1 profile
ADMU0503I: Retrieving server status for all servers
ADMU0505I: Servers found in configuration:
ADMU0506I: Server name: server1
ADMU0508I: The Application Server "server1" is STARTED

我想复制一个名为" server1"的特定字符串。并使用shell脚本将其分配给变量。

1 个答案:

答案 0 :(得分:0)

您可以像这样使用直线GNU awk

serverName=$( ./serverstatus.sh | awk -F':[[:blank:]]*' '/Server name/{print $3; exit}')
echo "$serverName"
server1