Intro To 'grep' Command In Linux
2024-07-22 - By Robert Elder
I use the 'grep' command to search for things in text files. For example, if I run the 'grep' command on the document 'interject.txt':
I'd just like to interject for a moment. What you're
referring to as Linux, is in fact, GNU/Linux, or as I've
recently taken to calling it, GNU plus Linux. Linux is
not an operating system unto itself, but rather another
free component of a fully functioning GNU system made
useful by the GNU corelibs, shell utilities and vital
system components comprising a full OS as defined by POSIX.
like this:
grep system interject.txt
the output will show only the lines that contain the word 'system':
not an operating system unto itself, but rather another
free component of a fully functioning GNU system made
system components comprising a full OS as defined by POSIX.
Filtering Dating Matches
One day, I was feeling lonely, so I decided to check out my favourite terminal-based online dating web site:
____ _____ _______ __ _ _____ _______ ___ _ _ _ ___ _ _ _____
/ ___|_ _|_ _\ \ / / | | / _ \ \ / / ____| / _ \| \ | | | |_ _| \ | | ____|
\___ \ | | | | \ V /____| | | | | \ \ / /| _| | | | | \| | | | || \| | _|
___) || | | | | |_____| |__| |_| |\ V / | |___ | |_| | |\ | |___ | || |\ | |___
|____/ |_| |_| |_| |_____\___/ \_/ |_____(_)___/|_| \_|_____|___|_| \_|_____|
The world's most popular site for singles who conform to the 80 column line limit.
***********************************
- Match Search -
I'm a
___________ (Select One: Vim, Emacs, Nano, Editor-fluid) user
Seeking A
___________ (Select One: Vim, Emacs, Nano, Editor-fluid) user
[ Find Matches ]
I can query the site for dating matches using the 'curl' command:
curl -s http://stty-love.online
Lily: I'm a cat gurl ;) Come and pet me, Meow!
Ava: I enjoy activism that consists of destroying priceless artwork.
Victoria: I have incredibly poor spending habits and no intentions of changing them.
Alice: I only pursue people who ignore me and reject anyone who gives me attention.
Miley: Doing my taxes and obeying the law are my favourite activites.
Sophia: I only vote for a specific political party, no matter how unqualified they are.
Emma: I project past relationship trauma on everyone around me.
Lucy: I enjoying drinking, partying, and not taking responsibility for my actions.
Nora: I am very mentally stable, and have normal social relationships.
Sally: I like to spread dangerous misinformation for my own amusement.
This web site shows me a list of potential dating matches, but unfortunately, most of the matches are not very wholesome!
I can use the 'grep' command to solve this problem, by piping the list of matches directly into grep so that I can filter the matches based on desired keywords:
curl -s http://stty-love.online | grep obey
Miley: Doing my taxes and obeying the law are my favourite activites.
Filter Using Regular Expressions
Using the '-P' flag, I can filter using regular expressions (although, this is not POSIX-compliant):
curl -s http://stty-love.online | grep -P 'obey|stable'
Miley: Doing my taxes and obeying the law are my favourite activites.
Nora: I am very mentally stable, and have normal social relationships.
Invert Matches
The '-v' flag can be used to only show lines that don't match instead:
curl -s http://stty-love.online | grep -P -v 'danger|poor|trauma|party'
Lily: I'm a cat gurl ;) Come and pet me, Meow!
Ava: I enjoy activism that consists of destroying priceless artwork.
Alice: I only pursue people who ignore me and reject anyone who gives me attention.
Miley: Doing my taxes and obeying the law are my favourite activites.
Nora: I am very mentally stable, and have normal social relationships.
Show Line Numbers
The '-n' flag can be used to also show the line numbers in the results as well:
curl -s http://stty-love.online | grep -P -n 'stable|relationship'
1:Lily: I'm a cat gurl ;) Come and pet me, Meow!
2:Ava: I enjoy activism that consists of destroying priceless artwork.
4:Alice: I only pursue people who ignore me and reject anyone who gives me attention.
5:Miley: Doing my taxes and obeying the law are my favourite activites.
9:Nora: I am very mentally stable, and have normal social relationships.
Searching Multiple Files & Folders
You can also use grep to recursively search through many files and folders at once using the '-R' flag. For example, I can use these commands to search for 'love' in the Linux kernel source code:
# Frist, clone source code for Linux kernel to ~/linux
cd ~/linux/arch
grep -R -n -P '\blove\b' *
And that's why the 'grep' command is my favourite Linux command.
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
$1.00 CAD |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
The Most Confusing Grep Mistakes I've Ever Made
Published 2020-11-02 |
Use The 'tail' Command To Monitor Everything
Published 2021-04-08 |
An Overview of How to Do Everything with Raspberry Pi Cameras
Published 2019-05-28 |
An Introduction To Data Science On The Linux Command Line
Published 2019-10-16 |
Using A Piece Of Paper As A Display Terminal - ed Vs. vim
Published 2020-10-05 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|