Context
grep is a powerful linux command utility which can be used to search strings in text files. Here are some advanced ways of using grep.
Practical Uses
Find a string in files recursively under a directory
Let’s create some files first.
1
2
3
4
5
6
7
8
9
10
11
|
mkdir /tmp/test
echo "password:DaoL^hk#" > /tmp/test/file1.txt
echo "password:PaIl^&ae" > /tmp/test/file2.txt
cat<<END >> /tmp/test/file3.txt
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nam a dolor laoreet, porta sem eget, congue ante.
Mauris et ligula elementum, porta nibh eget, ultrices lacus.
Aliquam a elit ut purus pulvinar consectetur.
Ut pharetra arcu eget mauris faucibus, ac tristique arcu facilisis.
password:helloWorld
END
|
This fetches lines with “password” keyword, along with file names.
1
|
grep -r "password" test
|
To print out line numbers, pass in -n param.
1
|
grep -rn "password" test
|
This is how it looks like in console.
1
2
3
4
|
demo:$ grep -rn "password" test
test/file2.txt:password:PaIl^&ae
test/file3.txt:password:helloWorld
test/file1.txt:password:DaoL^hk#
|
Scrape URLs from a web page
We use -E param for extended regex. Note that we are piping the contents of the webpage to grep command. curl is also on silent mode (-s param).
1
|
curl -s https://kaung.dev | grep -E "href=\"http(s{0,1}):\/{2}[a-zA-Z0-9\/\.]+\""
|
To print out only the matched items, we use -o param.
1
|
curl -s https://kaung.dev | grep -Eo "href=\"http(s{0,1}):\/{2}[a-zA-Z0-9\/\.]+\""
|
The result looks like below.
1
2
3
4
5
6
7
|
demo:$ curl -s https://kaung.dev | grep -Eo "href=\"http(s{0,1}):\/{2}[a-zA-Z0-9\/\.]+\""
href="http://kaung.dev/index.xml"
href="http://kaung.dev/"
href="https://github.com/kglns"
href="https://github.com/kglns"
href="http://kaung.dev/index.xml"
href="https://gohugo.io/"
|
Fetch N lines before or after a keyword
We use -B param and specify N, for “before” a keyword.
1
|
grep -B 3 "[P|p]assword" /tmp/test/file3.txt
|
The result
1
2
3
4
5
|
demo:$ grep -B 3 "[P|p]assword" /tmp/test/file3.txt
Mauris et ligula elementum, porta nibh eget, ultrices lacus.
Aliquam a elit ut purus pulvinar consectetur.
Ut pharetra arcu eget mauris faucibus, ac tristique arcu facilisis.
password:helloWorld
|
We use -A param and specify the number of lines we want to see after.
1
|
grep -A 3 "Lorem" /tmp/test/file3.txt
|
The result
1
2
3
4
5
|
demo:$ grep -A 3 "Lorem" /tmp/test/file3.txt
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nam a dolor laoreet, porta sem eget, congue ante.
Mauris et ligula elementum, porta nibh eget, ultrices lacus.
Aliquam a elit ut purus pulvinar consectetur.
|