Search code examples
bashawksedcut

Remove string after match along with the word/string after that


I have a file containing lines of following pattern.

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

I'd like to remove type, subtype and service alongwith the value of those fields(value after =).

Desired output:

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

I have been trying, with the little know, using cut, awk, sed but still not any near to solution. I've searched online for hours but that's gone in vain too. Could anyone please help?


Solution

  • Something you might want to reuse or build on later:

    $ 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
    

    The above uses GNU awk for FPAT and gensub().