### Commands tagged grep (371) the last day the last week the last month all time sorted by date votes

• 0
grep -i '[^script\$]' 1.txt
· 2010-10-20 12:17:04
• We can put this inside a function: fxray() { curl -s http://urlxray.com/display.php?url="\$1" | grep -o '<title>.*</title>' | sed 's/<title>.*--> \(.*\)<\/title>/\1/g'; }; fxray http://tinyurl.com/demo-xray Show Sample Output

-3
curl -s http://urlxray.com/display.php?url=http://tinyurl.com/demo-xray | grep -o '<title>.*</title>' | sed 's/<title>.*--> \(.*\)<\/title>/\1/g'
· 2010-09-30 10:25:18

• 0
pgrep -c cat
· 2010-09-24 22:42:12

• -2
find . -name "*noticia*" -name "*jhtm*" -name "*.tpl" -exec grep -li "id=\"col-direita\"" '{}' \; | xargs -n1 mate
· 2010-09-18 02:55:40
• recursive find and replace. important stuff are grep -Z and zargs -0 which add zero byte after file name so sed can work even with file names with spaces.

0
· 2010-08-30 22:12:57
• Suppose you have 11 marbles, 4 of which are red, the rest being blue. The marbles are indistinguishable, apart from colour. How many different ways are there to arrange the marbles in a line? And how many ways are there to arrange them so that no two red marbles are adjacent? There are simple mathematical solutions to these questions, but it's also possible to generate and count all possibilities directly on the command line, using little more than brace expansion, grep and wc! The answer to the question posed above is that there are 330 ways of arranging the marbles in a line, 70 of which have no two red marbles adjacent. See the sample output. To follow the call to marbles 11 4: after c=''; for i in \$(seq \$1); do c+='{b,r}'; done;, \$c equals {b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r}{b,r} After x=\$(eval echo \$c), and brace expansion, \$x equals bbbbbbbbbbb bbbbbbbbbbr ... rrrrrrrrrrb rrrrrrrrrrr, which is all 2^11 = 2048 strings of 11 b's and r's. After p=''; for i in \$(seq \$2); do p+='b*r'; done;, \$p equals b*rb*rb*rb*r Next, after y=\$(grep -wo "\${p}b*" Finally, grep -vc 'rr' Show Sample Output

-4
marbles () { c=''; for i in \$(seq \$1); do c+='{b,r}'; done; x=\$(eval echo \$c); p=''; for i in \$(seq \$2); do p+='b*r'; done; y=\$(grep -wo "\${p}b*" <<< \$x); wc -l <<< "\$y"; grep -vc 'rr' <<< "\$y"; }
· 2010-08-27 23:04:33
• Based on: http://stackoverflow.com/questions/746684/how-to-search-through-all-commits-in-the-repository It would be good if anyone can shorten this to eliminate the duplicate query string. Show Sample Output

-1
git grep "search for something" \$(git log -g --pretty=format:%h -S"search for something")
· 2010-08-26 12:05:45
• grep searches through a file and prints out all the lines that match some pattern. Here, the pattern is some string that is known to be in the deleted file. The more specific this string can be, the better. The file being searched by grep (/dev/sda1) is the partition of the hard drive the deleted file used to reside in. The ?-a? flag tells grep to treat the hard drive partition, which is actually a binary file, as text. Since recovering the entire file would be nice instead of just the lines that are already known, context control is used. The flags ?-B 25 -A 100? tell grep to print out 25 lines before a match and 100 lines after a match. Be conservative with estimates on these numbers to ensure the entire file is included (when in doubt, guess bigger numbers). Excess data is easy to trim out of results, but if you find yourself with a truncated or incomplete file, you need to do this all over again. Finally, the ?> results.txt? instructs the computer to store the output of grep in a file called results.txt. Source: http://spin.atomicobject.com/2010/08/18/undelete?utm_source=y-combinator&utm_medium=social-media&utm_campaign=technical

22
grep -a -B 25 -A 100 'some string in the file' /dev/sda1 > results.txt
· 2010-08-19 20:07:42
• When working on a big proeject with SVN, you create quite much files, for now! Can just sit here and type svn add for all of them! svn status will return a list of all of file which get ?(not add), "M"(Modified), "D"(Deleted)! This code just grep "?" flag, then add it into SVN again!

1
svn status | grep "^\?" | awk '{print \$2}' | xargs svn add
· 2010-08-14 18:56:15
• If we want files with more than one extension, like .tar.gz, only appear the latest, .gz: ls -Xp /path/to/dir | grep -Eo "\.[^./]+\$" | uniq Show Sample Output

0
ls -Xp /path/to/dir | grep -Eo "\.[^/]+\$" | uniq
· 2010-08-12 16:32:54
• Just a little simplification.

1
find /path/to/dir -type f | grep -o '\.[^./]*\$' | sort | uniq
· 2010-08-12 14:32:48

• 5
svn st | grep -e '^M' | awk '{print \$2}' | xargs svn revert
· 2010-08-11 14:24:05

• 1
netstat -rn | grep UG | tr -s " " | cut -d" " -f2
· 2010-08-09 15:25:18
• This will deal nicely with filenames containing newlines and will run one lzma process per CPU core. It requires GNU Parallel http://www.youtube.com/watch?v=OpaiGYxkSuQ

2
find . -name '*.txt' -print0 | parallel -0 -j+0 lzma
· 2010-07-28 21:01:12

• -2
find . -name '*.txt' | grep -v '\.lzma\$' | xargs -n 1 lzma -f -v -3
· 2010-07-21 16:58:41
• Really, you deserve whatever happens if you have a whitespace character in a file name, but this has a small safety net. The truly paranoid will use '-i'.

-1
rm \$( ls | egrep -v 'abc|\s' )
· 2010-07-18 10:59:15
• Essentially the same as funky's alias, but will not traverse filesystems and has nicer formatting. Show Sample Output

-1
alias dush="du -xsm * | sort -n | awk '{ printf(\"%4s MB ./\",\\$1) ; for (i=1;i<=NF;i++) { if (i>1) printf(\"%s \",\\$i) } ; printf(\"\n\") }' | tail"
· 2010-07-15 10:38:27
• Gnu grep allows to restrict the search to files only matching a given pattern. It also allows to exclude files.

0
grep -R --include=*.cpp --include=*.h --exclude=*.inl.h "string" .
· 2010-07-14 16:32:28

• 1
find . -name '*.?pp' -exec grep -H "string" {} \;
· 2010-07-14 15:10:23
• I like this better than some of the alternatives using -exec, because if I want to change the string, it's right there at the end of the command line. That means less editing effort and more time to drink coffee. Show Sample Output

2
find . -name '*.?pp' | xargs grep -H "string"
· 2010-07-14 14:41:07

• -1
grep -i '^DocumentRoot' /etc/httpd/conf/httpd.conf | cut -f2 -d'"'
· 2010-07-14 13:30:36
• The above output is for a custom compiled version of Vim on Arch Linux. Just a quick shell one liner, and presents a list of all the enabled and disabled (those prefixed with a '-') features. Show Sample Output

2
vim --version | grep -P '^(\+|\-)' | sed 's/\s/\n/g' | grep -Pv '^ ?\$'
· 2010-07-02 02:57:19
• Query the Socrata Open Data API being used by the White House to find any employee's salary using curl, grep and awk. Change the value of the search parameter (example uses Axelrod) to the name of any White House staffer to see their annual salary. Show Sample Output

2
curl -s "http://www.socrata.com/api/views/vedg-c5sb/rows.json?search=Axelrod" | grep "data\" :" | awk '{ print \$17 }'
· 2010-07-01 23:54:54
• Quicker way to search man pages of command for key word Show Sample Output

0
function mg(){ man \${1} | egrep \${2} | more; }
· 2010-07-01 21:14:24

• 0
ifconfig eth0 | grep -o "inet [^ ]*" | cut -d: -f2
· 2010-07-01 20:25:55
• ‹ First  < 8 9 10 11 12 >  Last ›

### What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.