3 ms·
> When we’re satisfied with our Perl script, it’s not unreasonable to think that we might want to run it across the entire access log rather than just the firs
by cryptonector 3y ago
> When we’re satisfied with our Perl script, it’s not unreasonable to think that we might want to run it across the entire access log rather than just the first 500 records. If we then delete only the head processing step we’re left without a step that transforms the string access.log into the contents of the access log. We can move that responsibility into the grep call, but this would mean we hade to change some existing component in order to remove another – no good!
What? Who cares, but also, if you do, then try this:
<the_file the_pipeline_here
taking his example:
<access.log head -n 500 | grep mail | perl -e ...
<access.log grep mail | perl -e ...
<access.log perl -e ...
Yes, you have a lot of freedom over where you put the redirections. I often write
nroff -man >/dev/null blah.1
to check for errors in a man page I'm editing, and you can see that intersperses I/O redirection with command-line words.
So if you're really concerned about what edits you might have to make to
head -n 500 access.log | grep mail | perl -e ...
to remove the head(1) and/or the grep(1) and so have to move where the `access.log` goes, well, just write
<access.log head -n 500 | grep mail | perl -e ...
and now you don't have to move `access.log`.
> The natural solution is a useless use of cat.
The problem with useless uses of cat(1) is mainly that it betrays a misunderstanding of how the shell works, so "that's a useless use of cat" is a way to teach someone something they're missing. (Useless uses of cat are also useless uses of CPU cycles and energy, but the vast majority of the time those will be in the noise, so it's not a huge deal.)