Bash awk/gsub-以每行多次出现的方式打印双引号之间的所有内容

Bash awk/gsub-以每行多次出现的方式打印双引号之间的所有内容,bash,sed,awk,Bash,Sed,Awk,我试图打印双引号(sampleField=“sampleValue”)之间的所有数据,但无法获取awk和/或sub/gsub以返回双引号之间的所有数据实例。然后,我希望将所有实例打印在各自的行上,以将数据保存在一起 以下是input.txt文件的示例: deviceId="1300", deviceName="router 13", deviceLocation="Corp" deviceId="2000", deviceName="router 20", deviceLocation="DC1

我试图打印双引号(
sampleField=“sampleValue”
)之间的所有数据,但无法获取
awk
和/或
sub
/
gsub
以返回双引号之间的所有数据实例。然后,我希望将所有实例打印在各自的行上,以将数据保存在一起

以下是
input.txt
文件的示例:

deviceId="1300", deviceName="router 13", deviceLocation="Corp"
deviceId="2000", deviceName="router 20", deviceLocation="DC1"
我想要的输出是:

"1300", "router 13", "Corp"
"2000", "router 20", "DC1"
使用gsub删除
=
之间的所有数据时遇到问题。每次我尝试不同的方法时,它总是返回第一个字段并移动到下一行

更新:

我忘了提到我不知道每行有多少双引号封装的字段。可能是1、3或5000。不确定这是否会影响解决方案,但希望确保它存在

awk -F '"' '{printf(" %c%s%c, %c%s%c, %c%s%c\n", 34,$2, 34, 34, $4,34, $6, 34) } ' \
    input file > newfile
是另一种更简单的方法,使用引号作为字段分隔符

awk 'BEGIN{ t=sprintf("%c", 34)}
     { for(i=1; i<=NF; i++){
        if(index($i,t) ){print $i}  }; printf("\n")}'  infile > outfile
awk'BEGIN{t=sprintf(“%c”,34)}

{对于(i=1;iA
sed
解决方案:

sed -r 's/[^\"]*([\"][^\"]*[\"][,]?)[^\"]*/\1 /g'
    <<< 'deviceId="1300", deviceName="router 13", deviceLocation="Corp"'
或对于文件:

sed -r 's/[^\"]*([\"][^\"]*[\"][,]?)[^\"]*/\1 /g' input.txt
试试这个


awk-F\”{for(i=2;iawk/sub/gsub/可能既不是最直接的方法,也不是最简单的方法。我喜欢有意义的一行程序:

(1) 在Perl中:

(2) 在bash中:

在这种情况下,我们指示sed在一行中运行三条替换指令,其中“deviceId”、“deviceName”和“deviceLocation”均替换为空字符串“”


不幸的是,sed(以及sub和gsub)对正则表达式的支持比Perl弱得多,Perl是支持完全正则表达式的黄金标准。特别是,sed和sub/gsub都不支持非贪婪指令“?”,而这次失败使我的生活变得相当复杂。

虽然为时已晚,但一个可能的简单解决方案是:

 $ awk -F"=|," '{print $2,$4,$6}' input.txt
"1300" "router 13" "Corp"
"2000" "router 20" "DC1"

这是不是因为不知道每行可以有多少字段?不,这是基于您的示例输入。我将发布一个更通用的解决方案,因为您需要一个。您可以在文件之前添加这样的OFS=“,”input.txt,以获得逗号和空格。
awk -F \" '
    {
        sep=""
        for (i=2; i<=NF; i+=2) {
            printf "%s\"%s\"", sep, $i
            sep=", "
        }
        print ""
    }
' << END
deviceId="1300", deviceName="router 13", deviceLocation="Corp", foo="bar"
deviceId="2000", deviceName="router 20", deviceLocation="DC1"
END
"1300", "router 13", "Corp", "bar"
"2000", "router 20", "DC1"
"1300",  "router 13",     "Corp"
"2000",  "router 20",     "DC1"
172-30-3-163:ajax vphuvan$ perl -pe 's/device.*?=//g' input.txt
"1300", "router 13", "Corp"
"2000", "router 20", "DC1"

where 
-p means "print to screen"
-e means execute the statement between the single quotes
s is a regular expression command which gives the instruction to substitute
g is the switch for the regular expression. /g instructs the program to carry out the substitution /device.*?=// wherever applicable
/device.*?=// is an instruction to replace with an empty string '' any expression that starts with the prefix "device" and that ends just before the closest "=" sign. Note that "deviceId", "deviceName"  and "deviceLocation" all start with the prefix "device" and each of them ends just before the "=" sign
172-30-3-163:ajax vphuvan$ sed "s/deviceId=//; s/deviceName=//; s/deviceLocation=//" input.txt
"1300", "router 13", "Corp"
"2000", "router 20", "DC1"
 $ awk -F"=|," '{print $2,$4,$6}' input.txt
"1300" "router 13" "Corp"
"2000" "router 20" "DC1"