Giter Site home page Giter Site logo

Comments (2)

benhoyt avatar benhoyt commented on May 27, 2024 1

Yes, this is slightly tricky, isn't it? I'd rather not introduce new syntax and range pattern types above and beyond POSIX here, so I'd suggest not using a range pattern for this, but two patterns with a flag. Similar to your endcond solution but a bit simpler (and one line :-).

You have to be careful with the order of the patterns, putting the /^ts:/ { e=0 } pattern-action first, so that the /Got Excep/ { e=1 } sets e to 1 for that first line before the e { print } pattern is evaluated, and the "Got Exception" line is printed:

$ goawk '/^ts:/ { e=0 }  /Got Excep/ { e=1 }  e { print }' data.txt
ts:Jan 11 12:16:33 ERROR Got Exception in module foo
                         1. Traceback (most recent call last):
                         1.   File "/tmp/teste.py", line 9, in <module>
                         1.     run_my_stuff()
                         1. NameError: name 'run_my_stufff' is not defined
ts:Jan 11 12:17:33 ERROR Got Exception in module foo
                         2. Traceback (most recent call last):
                         2.   File "/tmp/teste.py", line 9, in <module>
                         2.     run_my_stuff()
                         2. NameError: name 'run_my_stufff' is not defined

You can even shorten it slightly more by dropping the { print } on the last pattern, as that's the default:

$ goawk '/^ts:/ { e=0 }  /Got Excep/ { e=1 }  e' data.txt

The Gawk manual also has a couple of examples for range patterns that might be useful (though they don't quite fit what you're doing here).

Hope that helps!

from goawk.

balki avatar balki commented on May 27, 2024

Thanks! Though not obvious at first glance, yet concise and clear.

$ goawk '/^ts:/ { e=0 }  /Got Excep/ { e=1 }  e' data.txt

from goawk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.