如何使用awk来提取引述场? [英] How to use awk to extract a quoted field?
本文介绍了如何使用awk来提取引述场?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用
awk '{ printf "%s", $3 }'
从空格分隔行提取一些领域。当然,我得到的部分结果时域里面的自由空间引用。可能任何机构提出一个解决方案吗?
to extract some field from a space delimited line. Of course I get partial results when the field is quoted with free spaces inside. May any body suggest a solution please?
推荐答案
这实际上是相当困难的。我想出了以下的手动和专卖店分割线的所有领域中的数组AWK
脚本。
This is actually quite difficult. I came up with the following awk
script that splits the line manually and stores all fields in an array.
{
s = $0
i = 0
split("", a)
while ((m = match(s, /"[^"]*"/)) > 0) {
# Add all unquoted fields before this field
n = split(substr(s, 1, m - 1), t)
for (j = 1; j <= n; j++)
a[++i] = t[j]
# Add this quoted field
a[++i] = substr(s, RSTART + 1, RLENGTH - 2)
s = substr(s, RSTART + RLENGTH)
if (i >= 3) # We can stop once we have field 3
break
}
# Process the remaining unquoted fields after the last quoted field
n = split(s, t)
for (j = 1; j <= n; j++)
a[++i] = t[j]
print a[3]
}
这篇关于如何使用awk来提取引述场?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文