加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 如何使用awk提取引用字段?

发布时间:2020-12-13 19:56:15 所属栏目:Linux 来源:网络整理
导读:参见英文答案 Escaping separator within double quotes,in awk3个 我在用 awk '{ printf "%s",$3 }' 从空格分隔的行中提取一些字段.当我引用字段时,我得到部分结果.请问有谁提出解决方案吗? 解决方法 这实际上非常困难.我想出了以下awk脚本,它手动拆分行并
参见英文答案 > Escaping separator within double quotes,in awk3个
我在用
awk '{ printf "%s",$3 }'

从空格分隔的行中提取一些字段.当我引用字段时,我得到部分结果.请问有谁提出解决方案吗?

解决方法

这实际上非常困难.我想出了以下awk脚本,它手动拆分行并将所有字段存储在一个数组中.
{
    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]
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读