Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

    $ grep somestring file1 > /tmp/a
    $ grep somestring file2 > /tmp/b
    $ diff /tmp/a /tmp/b
You shouldn't do that, but not because it's not neat enough. /tmp is world-writable, so you might be writing to somebody else's file, or over a symlink that was set up by someone else. Use mktemp¹ for creating temporary files.

¹ http://man7.org/linux/man-pages/man1/mktemp.1.html



sort by: page size:

Could you not do that with pipes instead, something like:

    $ diff <(grep somestring file1) <(grep somestring file2)

Thanks for the detailed comparisons and writeup.

I find this simple wrapper around grep(1) very fast and useful:

http://www.pixelbeat.org/scripts/findrepo



    $ file /sbin/* | grep "dynamically linked" | wc -l
    325

    $ file /sbin/* | grep -i "static" | wc -l
    0
On Ubuntu focal, but Red Hat is similar.

... although that is a canonical Useless Use of grep. (-:

* http://porkmail.org/era/unix/award.html#grep


http://www.redballoon.net/humor/unix1.txt

A is for Awk, which runs like a snail, and B is for Biff, which reads all your mail. ...

but the grep entry here is different from what you remember.


   <file grep xyz
works in bash for one.

does your grep have an -f option?

   printf linkedintrouble |sha1sum |sed 's/ .*//' |grep -f - combo_not.txt

FWIW redirection is not positional:

    ~$ <frongle.txt grep fringle >frungle.txt

Somewhat tangential, but the author calls out using `grep` in conjunction with `awk`. Anytime I find myself doing this, it usually turns out that I can just throw the pattern into AWK itself.

    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.

[1] https://porkmail.org/era/unix/award


> grep filename.txt “thing I want”

> grep “thing I want” filename.txt.

…every time


I prefer keeping it all in one file, less messy since you are using grep anyway:

alias note='(date; cat; echo) >> ~/notes.txt'


I think

    grep ^ *
would fix that and is just as short.


One of my favorite moreutils tools is combine[1]. It allows you to compare the contents of text files with boolean operations. For example, want to know what lines are in file1 but not file2? Use:

    combine file1 not file2
1. http://manpages.ubuntu.com/manpages/zesty/en/man1/combine.1....

fwiw

    $ <filename grep <pattern>
no shell I'm aware of restricts you to placing redirections at the end, you can throw them on the beginning no problem.

uh...

$ grep remote files/* | grep "machine learning"

you're welcome?


I use

   ***TEMP***, 
   ***NEEDS WORK***, etc.  
Those you can find with grep.

(HN needs less broken Markdown.)


Thanks for sharing. I feel less silly for suggesting this now (as root):

    unalias grep && unalias egrep && cp "$(command -v grep)" "$(dirname "$(command -v grep)")/egrep
next

Legal | privacy