Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux AWK:使用字段分隔符连接匹配模式_Linux_Bash_Awk_Sed - Fatal编程技术网

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对,这也应该有效: