grep 'foo' file.txt | awk '{ print $1 }'
Becomes: awk '/foo/ { print $1 }' file.txt
There may be times when `grep` is preferable, but this is ubiquitous enough that it's mentioned in the "Useless Use of Cat" [1] awards. grep 'foo' file.txt | awk '{ print $1 }'
Becomes: awk '/foo/ { print $1 }' file.txt
There may be times when `grep` is preferable, but this is ubiquitous enough that it's mentioned in the "Useless Use of Cat" [1] awards.I was curious how performance would be impacted, and for a large file `grep` may not be so useless.
$ du -h /tmp/file.json
161M /tmp/file.json
$ time awk -F: '/"id"/ { print $1 }' /tmp/file.json >/dev/null
real 0m17.810s
user 0m17.690s
sys 0m0.083s
$ time (grep '"id"' /tmp/file.json | awk -F: '{ print $1 }' >/dev/null)
real 0m3.617s
user 0m3.641s
sys 0m0.037s
While pushing the filter into AWK is a bit easier on the eyes, it appears there is an incurred performance cost.[1] http://www.catb.org/~esr/writings/taoup/html/ch01s06.html
Personally I don't think either approach is "better". Sometimes it's easier to deal with some complex logic if it's all in a single awk program, rather than smeared across combinations of grep, awk, and other things. If that outweighs the perf drop in some specific situation, then I'll do it.
It was essentially a large list of pretty-printed objects with about 20 attributes each. A few of which had multi-paragraph string values.
You're totally right that commands like `cat *.txt | grep foo` look right and feel intuitive, but the problem is that it's not actually equivalent to `grep foo *.txt`. With a single input file, it's probably not an issue, but with multiple files the downstream command can't actually see what file it's reading from, so in this example grep can't report filenames with matches and you can't use flags like --include/--exclude based on filenames.
I keep wishing for a Unix-like shell and utilities where `cat *.txt | grep foo` actually works the same as `grep foo *.txt`. PowerShell has pulled this off to some extent, which is cool, but it doesn't quite feel right to me compared to Bash.