Search For Patterns Within Files With Grep

  Рет қаралды 27,821

DistroTube

DistroTube

Күн бұрын

One of the most important command line utilities that every Linux user should know is Grep. Grep searches one or more input files for lines containing a match to a specified pattern. By default, Grep outputs the matching lines but it has a ton of flags and options available.
ERRATA:
I mentioned that Grep doesn't like binaries. That's not entirely true. There is a way to grep a binary as if it were a text file by using the "-a" flag. One thing to note though is many binaries are just one gigantic line so a lot of the time, all you are going to get is the equivalent of a "cat binary-file".
REFERENCED:
► www.gnu.org/so... - GNU Grep
WANT TO SUPPORT THE CHANNEL?
💰 Patreon: / distrotube
💳 Paypal: www.paypal.com...
🛍️ Amazon: amzn.to/2RotFFi
👕 Teespring: teespring.com/...
SOCIAL PLATFORMS:
🗨️ Mastodon: mastodon.techn...
💬 IRC: irc://freenode #distrotube
👫 Reddit: / distrotube
📽️ LBRY: lbry.tv/$/invi...
DT ON THE WEB:
🕸️ Website: distrotube.com/
🐿️ Gopherhole: gopher://distro.tube
📁 GitLab: gitlab.com/dwt1
FREE AND OPEN SOURCE SOFTWARE THAT I USE:
🌐 Brave Browser - brave.com/dis872
📽️ Open Broadcaster Software: obsproject.com/
🎬 Kdenlive: kdenlive.org
🎨 GIMP: www.gimp.org/
🔊 Audacity: www.audacityte...
💻 VirtualBox: www.virtualbox...
Your support is very much appreciated. Thanks, guys!

Пікірлер: 62
@dustinmorse8497
@dustinmorse8497 4 жыл бұрын
cat into grep *Luke Smith enters the chat
@Mega_Casual
@Mega_Casual 4 жыл бұрын
Another very cool use for grep is when using ps -ef to list all processes. You can grep to filter specific info like a process name, or user name. Pretty much any process. EG-You can use that to determine what the process ID number is of a specific process that isn't working properly in order to use kill -9 the process ID if it were giving you trouble and needed to be shutdown. For example: ps -ef | grep bash ps -ef | grep username etc.....
@birkner510
@birkner510 4 жыл бұрын
You can also use pgrep for that.
@___9136
@___9136 4 жыл бұрын
Yeah, pgrep is generally easier to use for this purpose. Did you notice that `ps | grep foo` returns at least two results, one of which is the grep process itself? pgrep doesn't have that problem.
@zizzu549
@zizzu549 4 жыл бұрын
Now 40hours tutorial on advanced regular expressions thanks.
@DistroTube
@DistroTube 4 жыл бұрын
Regular expressions shouldn't require a 40-hour series of videos.
@zizzu549
@zizzu549 4 жыл бұрын
​@@DistroTube Regular expressions for turtles!
@theemacsen1518
@theemacsen1518 4 жыл бұрын
@@DistroTube I think he was joking XD
@kellingc
@kellingc 4 жыл бұрын
Thanks for posting this. Something a little different than tiling window managers et al. cat is actually for taking 2 or more files and catinating the output. So, sticking grep at the end of cat is perfectly fine. Or if you are looking to edit specific lines you can cat the file, use grep, and then use sed. Stick awk in there to the search ny a specific colum, then send it to lpr to print it out. The whole point of UNIX and Linux is streaming information, which is how the command line is set up. MS-DOS is the same way, with less utilities already available. You'd have to write a program to do things like grep, awk, and sed.
@birkner510
@birkner510 4 жыл бұрын
Not using more commands or programs than necessary is not really important for use on the command line. But it is useful in scripts, as it potentially reduces dependencies and runtime.
@AndersJackson
@AndersJackson Ай бұрын
-A After -B Before -C Context To see a full expansion will make it easier to remember the shot options. "cat | grep" make it hard to see which lines in which file that are matched with grep. If you do grep on many files. That is why it usually are better to not concatenate (cat is short for concatenate) file and then pipe it into global regular expression print command (and grep is short for Global Regular Expression Print, which is what grep do).
@HeikoRehm
@HeikoRehm 4 жыл бұрын
DT - The Human Manpage! Love it!
@MrZombieSwamp
@MrZombieSwamp 4 жыл бұрын
you could've used grep --color=auto for better visualization
@anirbankopty9418
@anirbankopty9418 Жыл бұрын
It would have been really nice if grep output had the matching part colored in the video, I do have that for some reason which helps me.
@enderger5308
@enderger5308 4 жыл бұрын
Can you do a video/stream where you let your viewers send in links to their dotfiles to be critiqued by you? It would help highlight good and bad practices found in such files.
@zeocamo
@zeocamo 4 жыл бұрын
you should use ripgrep, it is a FAST grep, so much faster ... and used with fzf ,,, it give you a fuzzy search in term or VIM
@w01dnick
@w01dnick 4 жыл бұрын
3:05 why not to use recursive grep? "-R ." ?
@mrmiyagi5
@mrmiyagi5 4 жыл бұрын
I use the The Silver Searcher at work almost daily.
@w01dnick
@w01dnick 4 жыл бұрын
Same, switched to ag in almost all tasks. Still grepping logs.
@AnzanHoshinRoshi
@AnzanHoshinRoshi 4 жыл бұрын
Thank you, Derek.
@nacthewatcher
@nacthewatcher 4 жыл бұрын
Hey DT, regarding streaming cat into grep because you just used it, the better way is writing the grep command and pressing alt-. To bring the argument from the previous command.
@UnlucksMcGee
@UnlucksMcGee 4 жыл бұрын
cat myfile | grep rickroll | wc - l Or just: grep -c rickroll myfile :D
@cattmampbellvideo
@cattmampbellvideo 4 жыл бұрын
This was so helpful! Would you consider making a video on Regular Expressions? Thanks!
@genkiferal7178
@genkiferal7178 Жыл бұрын
I'd like o see the top 10 **useful** RegEx that the average person would use - not what a dev would use.
@crwmike0
@crwmike0 4 жыл бұрын
The "command line snobs" would also tell you that cat is not for displaying files.
@FreeMan-wz3hj
@FreeMan-wz3hj 4 жыл бұрын
It's not, use less
@PestisNonSapien_GMO_exHuman
@PestisNonSapien_GMO_exHuman 4 жыл бұрын
vim is for newbs. cat is my editor cat
@jeetadityachatterjee6995
@jeetadityachatterjee6995 4 жыл бұрын
Do you plan on doing a video on text manipulation in the command line? I have touched on it using sed but I realise it's the tip of the iceberg
@dlgang
@dlgang 4 жыл бұрын
cool, I was thinking about grep the other day, no joke
@DistroTube
@DistroTube 4 жыл бұрын
Yea, sometimes I'm just sitting around and all of a sudden "grep" pops into my head. ;)
@hakumaou5929
@hakumaou5929 4 жыл бұрын
google reads minds...creepy...
@user-ql6kk9cz6o
@user-ql6kk9cz6o 11 ай бұрын
can you help me with this: You have not completed this task. You can use the cd command to navigate to the /home/analyst/logs directory and the grep command to return all lines containing the text string. To return all lines containing the text string, you must enter grep, then the string you are searching for, and then server_logs.txt. ?
@enoughmuskspam2490
@enoughmuskspam2490 4 жыл бұрын
A piece of advance ta anyone watching this video just use ripgrep instead it's better in every possible way.
@williamdroz6890
@williamdroz6890 4 жыл бұрын
Hello fellow Rustacean :)
@rohitmeharcse9772
@rohitmeharcse9772 2 жыл бұрын
Count the number of words in the file which contain all the letters a,e, i, o, u. These letters may occur more than once & other letters are also permitted. hellow you are so good can you solve this question pls
@letsplayonlinux4383
@letsplayonlinux4383 4 жыл бұрын
Great topic
@jahnuchoudhury6677
@jahnuchoudhury6677 3 жыл бұрын
It's really great .. I really liked the you teach ..thanks ... Could you make some video on TCL ?
@zacharycarbon4312
@zacharycarbon4312 4 жыл бұрын
did you do what i think you did? was that a masterfully subtle rickrolling @1:06 ... was this whole thing a rickroll ?
@DistroTube
@DistroTube 4 жыл бұрын
Well, if you are using my bashrc, all you need to do is type "rr" for the terminal rickroll. ;)
@PestisNonSapien_GMO_exHuman
@PestisNonSapien_GMO_exHuman 4 жыл бұрын
My favorite grep function acts like sed. Extract a substring from a line that contains a string that doesn't have to be part of that substring. echo -e "now is the time for all good"|grep -Po "the \K.*?(?= for)" grep -Po "wlan0.*inet \K.*?(?=/)"
@EdHatesNoobTubers
@EdHatesNoobTubers Жыл бұрын
Great video, but man it bugs me the audio is not synced
@mramosch
@mramosch 2 жыл бұрын
When you wc -w some text of exactly 1000 words you get 1000 as a result. However when you split the same text with grep into individual words you get 1040 words because grep is splitting off the ‘s and ‘t from words like • he’s • there’s • don’t • won’t • etc. when you do • cat input_file | grep -oE '\w{1,} ' Is there a way to make grep behave like wc, or do we have to use a different strategy for getting the right (desired) result? Any ideas?
@davidgomez79
@davidgomez79 4 жыл бұрын
command line snobs.. now I know how to describe them.
@folksurvival
@folksurvival 3 жыл бұрын
He was talking about Luke Smith.
@davidgomez79
@davidgomez79 3 жыл бұрын
@@folksurvival I figured as much. I feel the same way about Luke and a lot more. Way overrated. Pretty arrogant for a such a non advanced user.
@GreyDeathVaccine
@GreyDeathVaccine 4 жыл бұрын
grep -r "phrase" some dir - search recursively :-)
@eherrmann01
@eherrmann01 3 жыл бұрын
I'm not sure if I'm amused or disturbed that he has rickroll on his .bashrc.
@shiftyworks5630
@shiftyworks5630 4 жыл бұрын
why not awk?
@DistroTube
@DistroTube 4 жыл бұрын
There is a progression to these things. First, a video on grep, then a video on sed, then a video on awk. Gotta walk before we run. ;)
@luaumoal
@luaumoal 4 жыл бұрын
Take a look at ripgrep
@zweitlander874
@zweitlander874 4 жыл бұрын
:)
@FranksThemen
@FranksThemen 4 жыл бұрын
Grep is full of bloat and clearly violates the Unix philosophy. Instead of doing only one thing right, it does also read files and count lines. Worse feature creep than SystemD.
@justawfulgamer7738
@justawfulgamer7738 4 жыл бұрын
Am I the only one that finds it odd that he doesn't look at the camera?
@stephenjones8645
@stephenjones8645 4 жыл бұрын
He’s discussed that before. The room is too small to accommodate putting the dslr camera/tripod in front of him so he has to put it on the side.
@asswhole4195
@asswhole4195 4 жыл бұрын
First
@ikichullo
@ikichullo 4 жыл бұрын
Woah you're so cool i wish i could be like you wow you're so hip man i'm jealous you're so cool
@asswhole4195
@asswhole4195 4 жыл бұрын
@@ikichullo haha, thanks man. It gets lonely at the top sometimes though.
@a_maxed_out_handle_of_30_chars
@a_maxed_out_handle_of_30_chars 4 жыл бұрын
Nice, don't forget to put this in your resume, it really helps :)
@asswhole4195
@asswhole4195 4 жыл бұрын
@@a_maxed_out_handle_of_30_chars Already got that covered.
@DistroTube
@DistroTube 4 жыл бұрын
You guys are being really mean to Mr. Whole. He was just excited. The last 15 times he claimed "first" he was really the second. :D
Turn Bash Into A Game Of Chance With $RANDOM
11:05
DistroTube
Рет қаралды 13 М.
Xargs Should Be In Your Command Line Toolbag
16:24
DistroTube
Рет қаралды 98 М.
Nurse's Mission: Bringing Joy to Young Lives #shorts
00:17
Fabiosa Stories
Рет қаралды 15 МЛН
GTA 5 vs GTA San Andreas Doctors🥼🚑
00:57
Xzit Thamer
Рет қаралды 25 МЛН
Where GREP Came From - Computerphile
10:07
Computerphile
Рет қаралды 937 М.
Learning Awk Is Essential For Linux Users
20:02
DistroTube
Рет қаралды 296 М.
Essential Linux Commands - Cat, Tac and Tee
21:07
DistroTube
Рет қаралды 24 М.
Find And Locate Your Files
10:55
DistroTube
Рет қаралды 15 М.
Generate Random Manpages To Learn Linux
17:28
DistroTube
Рет қаралды 16 М.
Two Powerful Command Line Utilities 'cut' And 'tr'
15:18
DistroTube
Рет қаралды 33 М.
How The 'awk' Command Made Me A 10x Engineer
10:40
RobertElderSoftware
Рет қаралды 172 М.
grep: A Practical Guide
13:55
Navek
Рет қаралды 72 М.
Chown, Chmod and Shell Aliases
22:07
DistroTube
Рет қаралды 13 М.
Nurse's Mission: Bringing Joy to Young Lives #shorts
00:17
Fabiosa Stories
Рет қаралды 15 МЛН