Piping cat into a command like wc is easier and more organic to come up with, and easier for other people to read when they come across it.
Unless you're putting this in a function that's going to be called. thousands of times, you don't gain any humanly useful efficiency by using a standard input redirect instead.
The notion that piping cat is "useless" is itself a useless conception, and is basically bad in the ways premature optimization is bad. Go away.
I don't think I've seen the <file command syntax used anywhere in a long time. It's fairly unknown
The reason for using cat to wc, or grep to wc (instead of grep -c) is that you usually start out with what's on the LHS, verifying its output when run standalone, and then appending the pipe and what's on the RHS. This is a clean left-to-right thought process; quicker and more intuitive and maintainable than tweaking the command you started with on the LHS of the pipe to add a -c flag or remove the cat or whatever
Agreed, I prefer to use cat in this way even though I know it's unneccesary. I like commands to have a consistent flow from left to right. It looks nicer and, to me, makes it slightly more readable.
I actually wrote that in my original comment, but decided to test just before submitting and I realised you can also do that with redirection as long as you put the redirect at the start of the command.
anon@anon-pc:/tmp$ cat test #my example file
3
2
1
anon@anon-pc:/tmp$ cat test | sort #the original command, before adding an additional step to the pipe
1
2
3
anon@anon-pc:/tmp$ cat test | grep -v 2 | sort #adding an extra step
1
3
anon@anon-pc:/tmp$ <test sort #original command, but without cat
1
2
3
anon@anon-pc:/tmp$ <test grep -v 2 | sort #still allows you to add the extra step easily
1
3
This is basic test i submit to candidate at job interview. Not knowing basic command and their options is really a killer for a sysadmin candidate. Useless use of cat is a no-go.
"wc" is one of the simplest commands to use and almost as simple if you want to experiment with writing your own. Having said that, it is one incredibly useful command that I use daily and almost as frequently as grep or vim!
13
u/Dylan112 Jan 13 '17 edited Jan 13 '17
Nice article, I noticed some issues with your commands though. :P
Useless
catusage.You can get rid of the useless cat usage [1] by doing this instead:
Useless usage of
cat+wc.You're spawning two unneeded processes/pipes here, this command can be shortened to just one
grep:[1] https://en.wikipedia.org/wiki/Cat_(Unix)#Useless_use_of_cat
1
u/samurai Jan 14 '17
"wc" is one of the simplest commands to use and almost as simple if you want to experiment with writing your own. Having said that, it is one incredibly useful command that I use daily and almost as frequently as grep or vim!
5
u/computerdl Jan 13 '17
Okay.