Live data from Hacker News

Skip grep, use awk

blog.jpalardy.com

111–120 of 136 posts

Re: Skip grep, use awk

#111
post #66
post #52

Earlier quoted context omitted.

Even though it's more characters, I usually use perl -pe 's/search/replace/' instead of sed in pipelines because it understands /n (and other escape characters I don't remember). Because all it takes is to get burned a couple times for it to be worth sticking with what you know will work.

"/n"? What's that? My `man perlre' doesn't say anything about this regexp flag.

Probably not what rflrob meant, but /n prevents parentheses from capturing.

https://perldoc.perl.org/perlre.html#%2an%2a

Re: Skip grep, use awk

#113
post #93

I'd like to share a recent experience on a related note, but in the opposite spirit - rather than reduce the number of command invocations on a command line, it may make sense to increase it. I had a loop operating on a text file like this: while read line do echo "$line" | sed -e "s/A/X/" -e "s/B/Y/" -e "s/C/Z/" ... Gradually, as I added more things to replace, I noticed severe slowdown. Things got fast again when I…

why do you need `while read` part at all? this is the main slow down here. shell is extremely time consuming compared to sed/awk/whatever tool.

The script is currently a filter - the input comes from stdin.

Re: Skip grep, use awk

#114

Skip awk, use perl.... The alias below sets perl to loop over STDIN splitting each line on more than one whitespace character and populate an array F. The -nE will then Evaluate an expression from the command line looping over the input line-by-line. alias glorp='perl -aF"/\s+/" -nE' So now we have the command `glorp` to play with which has more familiar syntax than awk and all of CPAN available to play with! $ [data…

There was an awk/perl thread yesterday too.

Re: Skip grep, use awk

#115
post #111
post #66

Earlier quoted context omitted.

"/n"? What's that? My `man perlre' doesn't say anything about this regexp flag.

Probably not what rflrob meant, but /n prevents parentheses from capturing. https://perldoc.perl.org/perlre.html#%2an%2a

This is somewhat surprising. A relatively new feature, and not present in my current Perl.

Re: Skip grep, use awk

#116
post #82

Earlier quoted context omitted.

Small note: For GNU grep on single regexes at least, the -F flag should not impact performance. It is smart enough to see through a pattern as a literal and avoid the regex engine.

Thanks, I didn't know this.

Of course you might still want to use fgrep to make sure + and . match with the characters and are not interpreted like regular expression operators.

Re: Skip grep, use awk

#117
I'm sure the author is aware, but awk has at least three implementations: nawk (the one true), gawk (what most are using), and mawk (performance-oriented, unmaintained). Plus busybox-awk.

When benchmarking gawk, I've found using LANG=C and avoiding UTF-8 to make a substantial difference for pattern matching.

Re: Skip grep, use awk

#119

I'm sure the author is aware, but awk has at least three implementations: nawk (the one true), gawk (what most are using), and mawk (performance-oriented, unmaintained). Plus busybox-awk. When benchmarking gawk, I've found using LANG=C and avoiding UTF-8 to make a substantial difference for pattern matching.

> I've found using LANG=C

This is also true for grep and tr and sort - the unicode handling does impact speed quite a bit and I still don't understand why this here https://stackoverflow.com/q/20226851/772013 is not treated as a bug

Re: Skip grep, use awk

#120
post #96

Skip awk, use perl.... The alias below sets perl to loop over STDIN splitting each line on more than one whitespace character and populate an array F. The -nE will then Evaluate an expression from the command line looping over the input line-by-line. alias glorp='perl -aF"/\s+/" -nE' So now we have the command `glorp` to play with which has more familiar syntax than awk and all of CPAN available to play with! $ [data…

Here's the equivalent for Ruby: alias glorp='ruby -ane ' $ [data is generated] | glorp ' ~ /Something/ and puts $F[2]' Or: $ echo -e '{"hello":"world"}\n{"hello":"cat"}' | glorp 'puts JSON.load($_)["hello"] ' -rjson (Of course Ruby got the -a autosplit-mode and the -n assumed 'while gets(); ... end' loop from Perl along with $_ and $F, so it's very intentional that they're similar)

And Ruby regexes are amazing.
Post reply on HN