What I usually do is cat the file to inspect it, hit Control+C, then up arrow for previous command, then further pipe and head/tail/grep the file.
Starting a grep command is fine if you know that's all you're going to be doing.
grep something !$
`!$` will be replaced with the last parameter of the previous command. Crude example cat /etc/hosts
grep 12.34.56.78 $! # will be executed as grep 12.34.56.78 /etc/hosts
I find this extremely useful for when I'm cat'ing a file to get a sense of it's output then wanting to do something more meaningful with it in the next command.That all said, I have absent-mindedly done my fair share of stuff like this too:
!! | less
# eg will run as `cat /etc/hosts | less`
# if `cat /etc/hosts` was the previous command run
so I certainly wouldn't look down on people who have "abused" cat due to muscle memory.That's one of the features I miss the most when using terminals on a Mac.
Or you can check “Profiles → Keyboard → Use Option as Meta” for Terminal.app [or just press ⌥⌘O]. And then use option as meta.
Thanks, I wasn't aware of that. However it's a different hotkey and only pulls the last parameter. The shell I use, you could hit <alt>+<number> and you would get a parameter of that number completed - not just the last parameter. It was very handy for rebuilding long command lines with different arguments.
> Or you can check “Profiles → Keyboard → Use Option as Meta” for Terminal.app [or just press ⌥⌘O]. And then use option as meta.
I use iTerm2 - which does seem to have similar options but I'm yet to get it working. I know that's down to user error but it's still a real pity that it isn't just the default behaviour (in either iTerm or Terminal).
Also, isn't that special variable $_ and not $! ?
Doesn't work for me. Maybe I've broken something on my build? Or maybe you've redefined your keys to emulate the [alt] key?
> Also, isn't that special variable $_ and not $! ?
Sorry I meant `!$` not `$!` (updated my post accordingly).
Yes, $_ does the same thing too.
(->
¨log.txt¨
read-to-string
´(prepend ¨\n¨)
´(prepend ¨piping¨)
lines-to-strings
first
´(= ¨piping´)
)
To think of writing (read-to-string ¨log.txt¨) in the above pipe is wrong. Might as well load on more functions on the input line: (->
(prepend ¨piping¨ (prepend ¨\n¨ (read-to-string ¨log.txt¨)))
lines-to-strings
first
´(= ¨piping´)
)
The simple principle that emerges is that the first line is for input, not for applying functions. You might guess that someone who does this even in a pipe as simple as cat x | grep y has this muscle memory too, but someone who criticizes it, certainly doesn´t. Maybe that´s not a bad thing, but you do feel a large divide between each other."As na linux/unix sysadmin": typo ('na') aside it is 'As a linux/unix sysadmin'
"...my eyes are bleeding everytime I see" I think you meant 'my eyes bleed', otherwise it means that, coincidentally, you eyes were already bleeding every time you happen to see someone use cat in that way.
"everytime" is wrong, the correct form is "every time"
My point isn't to insult you, it is to show you that everyone has blind spots and we shouldn't give each other such a hard time.
Neither does useless use of cat. Or am I missing something that I couldn't read because of a deleted comment?
<filename grep foo1 - you want to add more filtering/processing before the grep
2 - grep's command line options are confusing (+ globbing + whatever), easier to just use it to grep stdin
3 - It works. Sure 'grep pattern file' works, but here that is inconsequential. I'm not in an 80s machine to worry if I'm opening one more process or pipe than needed, especially in simple cases like this
grep [flags] search_pattern [filename]
In GNU grep (eg Linux) it's even easier because your flags can appear wherever in the command you want (even at the end). Though I'd recommend sticking to the habit of having them after the executable name for readability and portability with non-GNU coreutils.It's really not that hard. There's plenty worse CLI tools to use which we're now stuck with because of historic reasons.
Take a look at the man page (linux)
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] -e PATTERN ... [FILE...]
grep [OPTIONS] -f FILE ... [FILE...]
Now -f does not specify the file to grep, it specifies a file where to read patterns from (and they have the same "variable" name there, confusing)Not to mention globbing and other shell escapes (which is not grep's fault, of course, but you might end up hitting in some situations)
Let's actually take a look at the man page shall we:
Synopsis
grep [OPTIONS] PATTERN [FILE...]
grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]
That first line is more or less exactly what I posted, is your typical usage of grep, and is very easy to learn.Sure, you can list of esoteric examples of grep usage but that's besides the point if it's not how people would typically use grep (in my ~25 years of command line usage, I can't even remember one occasion when I've needed `-f` - not saying it hasn't happened but it certainly isn't something I've needed regularly)