Linux AWK:使用字段分隔符连接匹配模式
输入文件:Linux AWK:使用字段分隔符连接匹配模式,linux,bash,awk,sed,Linux,Bash,Awk,Sed,输入文件: cat /tmp/filename app_name="MysqlCluster" whatever whatever whatever whatever whatever whatever whatever whatever region="Europe" root@localhost:~# awk -F\" '/app_name|region/ {printf "%s_", $2}' /tmp/filename MysqlCluster_Europe_root@localho
cat /tmp/filename
app_name="MysqlCluster"
whatever whatever
whatever whatever
whatever whatever
whatever whatever
region="Europe"
root@localhost:~# awk -F\" '/app_name|region/ {printf "%s_", $2}' /tmp/filename
MysqlCluster_Europe_root@localhost:~#
root@localhost:~# awk -F\" '/app_name|region/ {OFS="_"; printf "%s", $2}' /tmp/filename
MysqlClusterEuroperoot@localhost:~#
root@localhost:~# awk -F\" '/app_name|region/ {printf "%s_", $2} END{print} ' /tmp/filename
MysqlCluster_Europe_
And few other attempts on similar lines but have not been successful.
root@localhost:~# awk <here goes some awk magic> /tmp/filename
MysqlCluster_Europe <<< NOTE: No Underscore at the end
预期输出:
MysqlCluster_Europe
尝试:
cat /tmp/filename
app_name="MysqlCluster"
whatever whatever
whatever whatever
whatever whatever
whatever whatever
region="Europe"
root@localhost:~# awk -F\" '/app_name|region/ {printf "%s_", $2}' /tmp/filename
MysqlCluster_Europe_root@localhost:~#
root@localhost:~# awk -F\" '/app_name|region/ {OFS="_"; printf "%s", $2}' /tmp/filename
MysqlClusterEuroperoot@localhost:~#
root@localhost:~# awk -F\" '/app_name|region/ {printf "%s_", $2} END{print} ' /tmp/filename
MysqlCluster_Europe_
And few other attempts on similar lines but have not been successful.
root@localhost:~# awk <here goes some awk magic> /tmp/filename
MysqlCluster_Europe <<< NOTE: No Underscore at the end
我正在寻找:
cat /tmp/filename
app_name="MysqlCluster"
whatever whatever
whatever whatever
whatever whatever
whatever whatever
region="Europe"
root@localhost:~# awk -F\" '/app_name|region/ {printf "%s_", $2}' /tmp/filename
MysqlCluster_Europe_root@localhost:~#
root@localhost:~# awk -F\" '/app_name|region/ {OFS="_"; printf "%s", $2}' /tmp/filename
MysqlClusterEuroperoot@localhost:~#
root@localhost:~# awk -F\" '/app_name|region/ {printf "%s_", $2} END{print} ' /tmp/filename
MysqlCluster_Europe_
And few other attempts on similar lines but have not been successful.
root@localhost:~# awk <here goes some awk magic> /tmp/filename
MysqlCluster_Europe <<< NOTE: No Underscore at the end
root@localhost:~#awk/tmp/filename
MysqlCluster_Europe以下内容适用于您的示例。(但它不会打印换行。)
在imo中,一点也不减少您的努力,但在两个单独的操作中处理app_name
和region
会更实用,这样它也将支持多个app_name-region对
awk -F\" '/^app_name/ { printf "%s_", $2 } /^region/ { print $2 }' /tmp/filename
下面是一个正则表达式,用于匹配第1列中的app_名称和区域,然后用“=”字符分割
awk '$1 ~ "app_name|region" {split($0,a,"="); printf a[2]}' /tmp/filename | sed 's/"//g'
sed删除双引号
BR使用GNU awk。即使您的文件中有更多的app_name-region对,这也应该有效: