Linux for Programmers #10 - Grep Command & Regular Expressions

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·5y ago

Key Takeaways

The video covers the grep command and regular expressions in Linux, demonstrating how to use grep to search for patterns in files and console output, and explaining various options and techniques for filtering and searching.

Full Transcript

[Music] hello everybody and welcome to another linux or programmers tutorial video in this video i'll be covering the grep command now the grep command is extremely useful it's used very commonly and it actually exists in pretty much all linux distributions now what grep allows you to do is search for a pattern of specific text in a file in console output and in a bunch of other things as well now it's very useful there's a ton of different uses for the grep command and as like all the other videos this is just going to be kind of a brief introduction to it you will have to do a little bit of your own research and and find out a bit more about the grep command because i really just can't cover everything in this one video so with that said let's dive in after a quick word from the sponsor of this video and this series which is lenode now lenode is the best company to use when you're hosting a website app or some service in the cloud if your app runs on linux it will run on lenode and lenovo has great support 24 hours a day seven days a week 365 days a year as i've mentioned i have been a customer of lenod for over two years i love their service i use them for all kinds of different projects and i just love that they have cheap and affordable servers that i can use for some basic hobby projects too and i don't have to kind of break the bank if i just want to get something spun up and use it for a few hours or a few days so anyways claim your free 100 credit towards the node by clicking the link in the description and one last reminder to make sure that you sign up to get access to the last five videos in this tutorial series this is video 10 if you want to see the other five videos and you don't want to have to wait for potentially a long time then make sure you sign up at this platform again there will be a link in the description now lastly there is a text guide for this tutorial video this comes from lenode this is a very helpful guide talks about a lot of the things i'm going to say in this video and just as kind of a cheat sheet for some of the syntax and regular expression syntax and all that so anyways make sure you check this out there will be a link in the description to that so with that said let's talk about the grep command and actually before i do that i want to quickly show you what i've set up here on my system because i've created a bunch of files and a large text file that we're going to use to kind of test out this command so i'm in this folder called files and if i ls you can see that i have a bunch of files created here now i've also created another file this one is called grep.txt and inside of here you can see that we have a bunch of text and you don't have to worry about the contents of this text or anything like that you don't have to recreate this just understand that i've generated a ton of text and a bunch of empty files i just wrote a really basic script to do this so we had some kind of meaningful output for our grab command in this video so anyways let me show you grep so the syntax for the grep command is simply grep a pattern and then a file that you want to search for this pattern in now this is kind of the most basic usage of the grep command you can also use this to filter console output which i will show you in a second you can do a lot of other things with it as well but specifically if we wanted to search for a pattern in a file then what we would do is we would type the pattern so let's say i want to look for the pattern which is a and i want to find that in the grep.txt file i would type grep a grep.txt hit enter and this is going to show me all of the lines that contain this pattern on it so let me go back here let me just clear the screen and i'll explain this more in depth so this is the command that we just ran so what this is going to do is it's going to find the pattern a in this grep.txt file and whenever it finds this pattern is going to print out the line in which this pattern appears on now notice i keep saying pattern the reason i'm saying pattern is because we're not just searching for this absolute text we're searching for a pattern we're going to match whatever pattern we type here which can be a lot more advanced than this with all of the text in this file and if we ever find a match we will output the entire line in the file that contains that match even if only part of that line was the actual match so for example you could see here that only a is what's actually matching and that's highlighted in red but we're showing all of the lines in which we actually find a right and you'll see that some of the lines we have two matches because there was two a's so just keep that in mind it will show you the line in which these matches occur now as i said at the beginning of the video what grep will take or what it will look for is a regular expression pattern now i'm not going to get into depth about regular expressions because they're very complicated and there's a lot of stuff that you can show but i will show you some very basic regular expression syntax or patterns that you can write so you have an idea of what i mean so if i type grep a and then period what this period means when this occurs in a regular expression pattern is that we're going to match any character or any symbol with this period so when we talk about the pattern a and then period what that means is we're looking for anything that starts with a and then has any other character afterwards so you'll see when i run this that we get zed and then a and then any other character right and we have all other letters and then a and then any other character because we're looking for a followed by any character right now if i change this and instead i say a dot a what this means is we're looking for any pattern that starts with a has any character and then ends with it so now when i do this you can see that these are all of the patterns that we get so a dot matches any character or any symbol just keep that in mind because that's probably the most useful one or the most common one that you will use now another one that you can use is these square brackets now what these square brackets will do is look for one symbol or the other so if i type a comma b and then dot a what this means is look for anything that starts with a or starts with b that's what it means when you're inside of these square brackets then has any symbol and then ends in a so if i run this notice that we get a bunch of stuff that starts with a that starts with b and that has any other symbol and then ends with a so that's what that pattern did so i just need to take a quick pause and thank the other sponsor of this video which is algo expert i'll go expert is the best platform to use to prepare for your software engineering coding interviews and i actually work there as an algorithms instructor so if you want to check out the questions i've created on algo expert head to the link in the description and use the code temp with tim for a discount on the platform so now i'll show you a few other things that you can do with grep so let's go back to our command here so let me remove this now usually when you do a regular expression pattern you're going to put it inside of quotation marks the reason you do this is because there's some symbols that you type that will just kind of be messed up or interpreted incorrectly if they're not inside of quotation marks so keep that in mind if you're getting some types of issues when you're doing your regular expressions just just put the expression or the pattern inside of quotation marks anyways the next thing i wanted to show you though is the or operator so if you want to search for one pattern or another pattern then what you do is use the or operator so the or operator looks like this it's a forward slash and then a pipe at least i think that's a forward slash i don't know if you call that one the forward slash of the backslash someone can correct me in the comments but regardless you put a pattern on the left hand side and a pattern on the right hand side and it will look for either of those patterns so if i want to look for a dot b or i wanted to look for b dot a then i would do this and it's going to show me all of the patterns that match either of these so we're looking for a pattern that is this one or the other one hopefully that's clear but that's the or operator there's a few more things that you can do as well i don't really want to get into it a ton you can match the beginning and the end of lines so for example say i wanted to look for a b and then i wanted this a b to actually be at the end of a line i would type a b and then the dollar sign and what this means is match this pattern here so a b with the end of the line so when i do this it's going to find all the a b's that are at the end of the line not at the beginning of the line and of course if a line is just a b it would be at the beginning and the end of the line but you get the idea right so it's just showing me once at the end if i only want to match the front of the line or the beginning of the line i use this little hat so that's what's above the six or shift six so grab hat and then a b and what that does is show me all the a b's at the beginning of the line so you get the idea these can be really complicated you can make a very advanced regular expression patterns again not going to get into those too much that's all i wanted to cover so now let me show you a few more options that we can use with the grep command so first of all sometimes you don't just want to know the line that this pattern is on like the contents of the line you want to know the line number so if you want to know the line number what you do is you type grep hyphen n standing for the line number that's pretty much what it means and then you are going to type your pattern so maybe we'll do abc and then the file in this case we do grep.txt and we see that line 32 is what has the pattern abc now even if we did say a b and then dot this will show me all of the uh all of the line numbers for all of the different patterns so that's pretty helpful now there's another thing you can do which is hyphen o now what hyphen o will do and i believe this is a lowercase o not a uppercase o is it is just going to show you the matching pattern not the entire line now sometimes like i don't know why you would exactly want to do this but sometimes you don't want to see the entire line you only want to see the pattern that matched so for example if i were to type a b and we didn't have this hyphen o notice how it's showing me all all these other characters before it if i didn't want to see that and i just wanted to say c a b then what i would do is i would put hyphen o here so grep hyphen o a b grep.txt and now it's only going to show me the pattern it's not going to show me all of the letters before that now this is kind of a poor example because you know why would you want to do this but for example if we did a dot now we'll see just the pattern right we won't see all of the uh all of the characters that are not a part of the pattern but are on the same line so hopefully that's clear that's what hyphen o does just shows you the matching pattern then if you type hyphen c and this is uppercase c and then you type some number this is actually going to show you lines after the matched pattern so say i wanted to see like four lines after the matched pattern what i would type is grep hyphen c four and then we'll go a and then dot and then a and now it's going to show me four lines above and four lines below the matching pattern right so it shows it to me on both sides and so i had mis-explained that previously but it does show you on both sides of the pattern so this way you can kind of see what's around it now you can imagine that would be useful if you're searching for something in like a python file or in some you know programming related file because you could see maybe the block it was nested in and stuff like that so that's kind of the basic commands and options for the grep command of course there's more that you can do with it and the more you know about regular expressions the better you're going to be with grep but what you can also do with grep which is really useful is you can search for files you can look in directories and you can filter the output of a command so the most useful one is filtering the output of the command so i'll show you that first so if i type the ls command and then i type the pipe and then graph and then the pattern that i want to search for what this will do is it will find this pattern in this output so ls has some output right so ls command will run and then grep will search through the output of ls and show me all of the matching patterns so if i say ls and then grep and then a b it's going to show me all of the file names that match that pattern now it's kind of confusing because my grep.txt and my file names have the exact same thing but remember if i go ls i have a bunch of empty files so what i did was i did ls and then pipe and then grep and then let's just go lh and now it only shows me files that have lh in them right and i can use the same pattern i would use previously i could match the ending of the line so i would do lh like that and now it only shows me files that end in lh so you can use this on any command you don't just have to use it on ls for example we could say like cat and you know grep.txt and then bar and then grep and let's say a b and then dot and notice is going to give me the same thing right it's going to filter the cat command the output of the cat command and just give me all the matching patterns so another thing that we can do with the grep command is we can actually search a bunch of files at the same time so for example i just have changed my directories so now i'm inside this directory my home directory notice we have abc.txt a dot txt ab.txt abcde right so let's just nano into abc.txt let's add and hello and let's escape let's nano into a.txt and let's add and hello and escape and now what i'm going to do is i'm going to cd dot dot and i'm going to look inside of this entire directory so inside of my entire home directory and find all of the strings that are hello so what i'll do now is i'll just type grep hyphen r hyphen r stands for recursive so if you want to look in an entire directory and search all of the files in a directory you do hyphen r then you do the pattern you want to look for so in this case i want to look for hello and then i want to look in what directory i want to look in the home directory now i think this is just called root actually let's do grep hyphen r hello root and now it's going to show me all of the strings hello in any of the files that are inside of this root directory so notice inside of test2 we have low a bunch of times inside of abc we have hello inside of biminfo we have hello right so you get the idea that's what it's showing us so that is kind of the idea behind how grep works you can use it to search for text inside of a file you can use it to search for text inside of a directory so it looks through all of the files you can then output commands or you can then filter commands using grep so you saw when i typed cat i could filter that when i typed ls i could filter that there's a whole bunch more uses for grep as well but super useful and again definitely something that you need to know and be comfortable using so anyways i hope this video was helpful to you if it was make sure to leave a like subscribe to the channel and i hope to see you in the next linux for programmers tutorial [Music] video you

Original Description

Welcome back to another Linux for programmers tutorial video. In this video, I'll be covering the grep command and regular expressions. The grep command is an extremely useful and common command within Linux, and exists in almost all Linux distributions. The grep command allows you to find a pattern of specific text in a file, console output, etc. 💰 Get a FREE $100 Credit on Linode: https://promo.linode.com/twt/ 📚 Get early access to later videos in the series: https://event.on24.com/wcc/r/2952121/381131E9ADEA3203AEC3413E5A212643/1958924?partnerref=partneryoutube 📖 Grep & Regular Expressions (Guide): https://www.linode.com//docs/guides/how-to-grep-for-text-in-files/ 💻 AlgoExpert is the coding interview prep platform that I used to ace my Microsoft and Shopify interviews. Check it out and get a discount on the platform using the code "techwithtim" https://algoexpert.io/techwithtim ⭐️ Timestamps ⭐️ 00:00 | Intro 02:08 | grep command syntax 04:21 | Regular expressions 08:27 | grep options 11:02 | Filtering output with grep 12:22 | grepping directories 📒 Playlist: https://www.youtube.com/watch?v=ebHX9c75H8I&list=PLzMcBGfZo4-nUIIMsz040W_X-03QH5c5h ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️ 💰 Courses & Merch 💰 💻 The Fundamentals of Programming w/ Python: https://tech-with-tim.teachable.com/p/the-fundamentals-of-programming-with-python 👕 Merchandise: https://teespring.com/stores/tech-with-tim-merch-shop 🔗 Social Medias 🔗 📸 Instagram: https://www.instagram.com/tech_with_tim 📱 Twitter: https://twitter.com/TechWithTimm ⭐ Discord: https://discord.gg/twt 📝 LinkedIn: https://www.linkedin.com/in/tim-ruscica-82631b179/ 🌎 Website: https://techwithtim.net 📂 GitHub: https://github.com/techwithtim 🔊 Podcast: https://anchor.fm/tech-with-tim 🎬 My YouTube Gear 🎬 🎥 Main Camera (EOS Canon 90D): https://amzn.to/3cY23y9 🎥 Secondary Camera (Panasonic Lumix G7): https://amzn.to/3fl2iEV 📹 Main Lens (EFS 24mm f/2.8): https://amzn.to/2Yuol5r 🕹 Tripod: https://amzn.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video teaches the basics of the grep command and regular expressions in Linux, covering how to search for patterns in files and console output, and how to use various options and techniques for filtering and searching. By the end of this video, viewers will be able to use the grep command to search for patterns in files and console output, and understand how to use regular expressions to match specific patterns.

Key Takeaways
  1. Type the pattern to search for in a file
  2. Type the file to search for the pattern in
  3. Run the grep command
  4. Use quotation marks around regular expression patterns
  5. Use the or operator to search for one pattern or another
  6. Use the dollar sign to match the end of a line
  7. Use the caret to match the beginning of a line
  8. Use grep -n to show line numbers with matching patterns
💡 The grep command is a powerful tool for searching for patterns in files and console output, and regular expressions can be used to match complex patterns.

Related Reads

📰
The Silent Costs of AI APIs Nobody Warns You About
Understand the hidden costs of AI APIs to avoid unexpected expenses and vendor lock-in
Dev.to · Shaw Sha
📰
The Only AI Tools a Small Business Actually Needs in 2026
Discover the essential AI tools for small businesses in 2026 to boost efficiency and customer engagement
Dev.to AI
📰
I Built an AI Life Planner the Month I Graduated and Switched to Linux Halfway Through
Learn how to build a personal AI life planner by following the author's journey, from initial development to switching to Linux halfway through, and apply these skills to your own projects
Dev.to · Hilal
📰
Your Second Brain Is a Graveyard. Your AI Has Amnesia. (Part 1)
Learn why your second brain and AI tools are failing you and how to address these issues
Medium · AI

Chapters (6)

| Intro
2:08 | grep command syntax
4:21 | Regular expressions
8:27 | grep options
11:02 | Filtering output with grep
12:22 | grepping directories
Up next
How AI Is Transforming Analytics in Tableau Cloud & Server
Salesforce Product Center
Watch →