Comments (2)
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.
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)
- Ensure we use value of FS at the time the record was read
- Allow outputting to /dev/stderr on Windows HOT 14
- Missing support for json input files HOT 1
- CSV - loss of double quote when dataset is updated HOT 2
- After Go 1.21 is out, build with PGO enabled HOT 1
- close() on pipe command does not return exit status HOT 2
- close() on input pipe does not cleanup child process HOT 5
- system() always returns -1 if process exits due to signal
- fflush() stops on first flush error HOT 2
- Add support for new \u Unicode escape sequence
- Windows error: flag provided but not defined: -encodedCommand HOT 3
- goawk CSV mode ignores specified field separator HOT 2
- Allow line break between `}` and `while` HOT 1
- parser: Discriminate between constant and dynamic regexps HOT 3
- Support for network? HOT 3
- What's the purpose of `testdata/echo` HOT 7
- {} behaves like 1, END{} doesn't exhaust input HOT 2
- Support for %i in format strings. HOT 3
- srand() initialization without an argument. HOT 1
- Test failures on Linux/i386 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from goawk.