awk匹配如何传递第3个参数

时间:2022-01-15 20:08:15

Am trying to add a 3rd column/argument to achieve my output. In the below data/string I would like to add the "type" to my existing output

我试图添加第3列/参数来实现我的输出。在下面的数据/字符串中,我想在现有输出中添加“type”

Data:
<field name="AVERAGE_TIME" type="float" id="0xDZZ" sequence="1"/>


Present working script
FILE="$1"
awk -F[=\ ] 'BEGIN{OFS="|" }
/context/{cn=$3}
/field/{match($0,"id=[^ ]+"); idstart = RSTART+3; idlen=RLENGTH-3;
match($0,"name=[^ ]+"); namestart=RSTART+5; namelen=RLENGTH-5;
print substr($0,namestart, namelen), substr($0,idstart, idlen),cn
}' "../$FILE" |  sed 's/\"//g' 


Present Output
AVERAGE_TIME|0xDZZ|temp


What I would like to see (type added)
 AVERAGE_TIME|0xDZZ|temp|float

2 个解决方案

#1


0  

Here's how you'd modify your original script to do what you need. Here's the unchanged part:

以下是您修改原始脚本以执行所需操作的方法。这是未改变的部分:

FILE="$1"
awk -F[=\ ] 'BEGIN{OFS="|" }
/context/{cn=$3}
/field/{match($0,"id=[^ ]+"); idstart = RSTART+3; idlen=RLENGTH-3;
match($0,"name=[^ ]+"); namestart=RSTART+5; namelen=RLENGTH-5;

Now, we add an addditional line and modify the print:

现在,我们添加一个附加行并修改print:

match($0,"type=[^ ]+"); typestart=RSTART+5; typelen=RLENGTH-5
print substr($0,namestart, namelen), substr($0,idstart, idlen),cn,substr($0,typestart,typelen)
}' "../$FILE" |  sed 's/\"//g' 

Just a note, awk is not a good solution for xml-parsing and your awk script isn't the best way of doing it either. Here's a slightly-cleaner solution, if you really need to use awk here (I just copied your context stuff verbatim here):

只是注意,awk不是一个很好的xml解析解决方案,你的awk脚本也不是最好的方法。这里有一个稍微清洁的解决方案,如果你真的需要在这里使用awk(我只是在这里逐字复制你的上下文):

cat $FILE | 
awk  'BEGIN{OFS="|" } 
     /context/{cn=$3} ## i just copied this verbatim from your script
     /^<field/ && NF>3 {delete x; 
                       for (i=1; i<=NF; i++) {  
                          match($i,  /^(.*?)=\"(.*?)\"$/, arr); 
                          if (1 in arr && 2 in arr) { x[arr[1]] = arr[2];}
                        }; 
                        print x["name"], x["id"], cn, x["type"]}'

#2


2  

$ awk -F'"' -v OFS='|' '{print $2, $6, "temp", $4}' file
AVERAGE_TIME|0xDZZ|temp|float

If that doesn't do what you want then please edit your question to clarify your requirements and add some more truly representative sample input and expected output.

如果这不符合您的要求,那么请编辑您的问题以阐明您的要求,并添加一些更真实的代表性样本输入和预期输出。

#1


0  

Here's how you'd modify your original script to do what you need. Here's the unchanged part:

以下是您修改原始脚本以执行所需操作的方法。这是未改变的部分:

FILE="$1"
awk -F[=\ ] 'BEGIN{OFS="|" }
/context/{cn=$3}
/field/{match($0,"id=[^ ]+"); idstart = RSTART+3; idlen=RLENGTH-3;
match($0,"name=[^ ]+"); namestart=RSTART+5; namelen=RLENGTH-5;

Now, we add an addditional line and modify the print:

现在,我们添加一个附加行并修改print:

match($0,"type=[^ ]+"); typestart=RSTART+5; typelen=RLENGTH-5
print substr($0,namestart, namelen), substr($0,idstart, idlen),cn,substr($0,typestart,typelen)
}' "../$FILE" |  sed 's/\"//g' 

Just a note, awk is not a good solution for xml-parsing and your awk script isn't the best way of doing it either. Here's a slightly-cleaner solution, if you really need to use awk here (I just copied your context stuff verbatim here):

只是注意,awk不是一个很好的xml解析解决方案,你的awk脚本也不是最好的方法。这里有一个稍微清洁的解决方案,如果你真的需要在这里使用awk(我只是在这里逐字复制你的上下文):

cat $FILE | 
awk  'BEGIN{OFS="|" } 
     /context/{cn=$3} ## i just copied this verbatim from your script
     /^<field/ && NF>3 {delete x; 
                       for (i=1; i<=NF; i++) {  
                          match($i,  /^(.*?)=\"(.*?)\"$/, arr); 
                          if (1 in arr && 2 in arr) { x[arr[1]] = arr[2];}
                        }; 
                        print x["name"], x["id"], cn, x["type"]}'

#2


2  

$ awk -F'"' -v OFS='|' '{print $2, $6, "temp", $4}' file
AVERAGE_TIME|0xDZZ|temp|float

If that doesn't do what you want then please edit your question to clarify your requirements and add some more truly representative sample input and expected output.

如果这不符合您的要求,那么请编辑您的问题以阐明您的要求,并添加一些更真实的代表性样本输入和预期输出。