Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'useless use of cat!' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
useless use of cat!
Authored by: merlyn on Jul 26, '05 10:00:59AM
You said:

cat file.txt|grep stringToSearchFor
This is a "useless use of cat". "cat" is for concatenating. You're not concatenating. You could have, should have, might have, written:

grep stringToSearchFor file.txt


[ Reply to This | # ]
useless use of cat!
Authored by: bendybendy on Jul 26, '05 10:49:12AM

I use cat like this all the time. You can try different search strings at the end of the command, using the up-arrow to recall the last bash command. Doesn't make sense for one search, but if you are going to try a bunch of differnet search strings, it's quick.



[ Reply to This | # ]
useless use of cat!
Authored by: googoo on Jul 26, '05 12:28:15PM
Merlyn's point is that you do not need to send the file(s) through cat in order to grep them. Just leave out the cat part and grep the files with

$ grep search_string file_name(s)

It even works with wildcards and other shell patterns. Try it (and check out man grep).

-Mark

[ Reply to This | # ]

useless use of cat!
Authored by: lullabud on Jul 26, '05 02:21:57PM
Both ways do the same thing and of course omitting a "useless" `cat` would be more efficient, but assuming you will be repeating the command several times with different search strings, it's much easier to arrow back from the end of the line only a few characters, rather than all the way through the filename, especially if the filename is a hundred or so characters long when the path is included. eg:

cat some_long_file_name.txt | grep 'somethingtosearch'
cat some_long_file_name.txt | grep 'somethingtosearchfor'
In that example, the second line only requires pressing the up arrow key once, the left arrow key once, then typing the string 'for'. The other way, `grep 'somethingtosearch' some_long_file_name.txt` would require arrowing all the way back through the file name, or else doing ctrl-a and arrowing all the way forward through the search string. So, really, the usage of `cat` in the original example does have a good use under certain circumstances.

[ Reply to This | # ]
useless use of cat!
Authored by: kps on Jul 26, '05 07:01:54PM

<some_long_file_name.txt grep 'somethingtosearch'
<some_long_file_name.txt grep 'somethingtosearchfor'


[ Reply to This | # ]
better use of history
Authored by: muddmatt on Jul 26, '05 03:12:54PM
if you are going to try a bunch of differnet search strings, it's quick

A better idiom:

% grep foo /path/to/file.txt
% grep bar !$
"!$" means "last argument of the previous command". See the bash manpage for other (highly useful) history tokens.

[ Reply to This | # ]
better use of history
Authored by: rootpoot on Jul 26, '05 09:13:35PM
You can also use
<esc> .
to access the last argument.

[ Reply to This | # ]