匹配后删除字符串,然后删除单词/字符串

时间:2020-03-30 19:36:22

标签: bash awk sed cut

我有一个包含以下模式行的文件。

date=2020-02-22 time=13:32:41 type=text subtype=text ip=1.2.3.4 country="China" service="foo"  id=47291 msg="foo: bar.baz," value=50
date=2020-03-17 time=11:49:54 type=text subtype=anothertext ip=1.2.3.5 country="Russian Federation" service="bar"  id=47324 msg="foo: bar.baz," value=30
date=2020-03-30 time=16:29:24 type=text subtype=someothertext ip=1.2.3.6 country="Korea, Republic of" service="grault, garply"  id=47448 msg="foo: bar.baz," value=60

我想删除类型,子类型和服务以及这些字段的值(=之后的值)。

所需的输出:

date=2020-02-22 time=13:32:41 ip=1.2.3.4 country="China" id=47291 msg="foo: bar.baz," value=50
date=2020-03-17 time=11:49:54 ip=1.2.3.5 country="Russian Federation" id=47324 msg="foo: bar.baz," value=30
date=2020-03-30 time=16:29:24 ip=1.2.3.6 country="Korea, Republic of" id=47448 msg="foo: bar.baz," value=60

我鲜为人知地尝试使用cutawksed,但仍然没有解决的办法。我已经在网上搜索了几个小时,但这也没有用。谁能帮忙吗?

3 个答案:

答案 0 :(得分:1)

您可以使用此sed

sed -E 's/(^|[[:blank:]]+)(subtype|type|service)=[^[:blank:]]+//g' file

date=2020-02-22 time=13:32:41 ip=1.2.3.4 country="China"  id=47291 msg="foo: bar.baz," value=50
date=2020-03-17 time=11:49:54 ip=1.2.3.5 country="Russian Federation"  id=47324 msg="foo: bar.baz," value=30
date=2020-03-30 time=16:29:24 ip=1.2.3.6 country="Korea, Republic of" garply"  id=47448 msg="foo: bar.baz," value=60

答案 1 :(得分:1)

您可能想重用或以后建立的东西:

$ cat tst.awk
BEGIN {
    split(s,tmp)
    for (i in tmp) {
        skip[tmp[i]]
    }
    FPAT = "[^ ]+(=\"[^\"]+\")?"
}
{
    c=0
    for (i=1; i<=NF; i++) {
        tag = gensub(/=.*/,"",1,$i)
        if ( !(tag in skip) ) {
            printf "%s%s", (c++ ? OFS : ""), $i
        }
    }
    print ""
}

$ awk -v s='type subtype service' -f tst.awk file
date=2020-02-22 time=13:32:41 ip=1.2.3.4 country="China" id=47291 msg="foo: bar.baz," value=50
date=2020-03-17 time=11:49:54 ip=1.2.3.5 country="Russian Federation" id=47324 msg="foo: bar.baz," value=30
date=2020-03-30 time=16:29:24 ip=1.2.3.6 country="Korea, Republic of" id=47448 msg="foo: bar.baz," value=60

上面的代码将GNU awk用于FPAT和gensub()。

答案 2 :(得分:0)

您可以尝试以下操作:

awk -F " " '{ $3=""; $4=""; $5="";  print}' file

您基本上是将列设置为空字符串。

相关问题