Search For Patterns Within Files With Grep

Ғылым және технология

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/software/grep/ - GNU Grep
WANT TO SUPPORT THE CHANNEL?
💰 Patreon: / distrotube
💳 Paypal: www.paypal.com/cgi-bin/webscr...
🛍️ Amazon: amzn.to/2RotFFi
👕 Teespring: teespring.com/stores/distrotube
SOCIAL PLATFORMS:
🗨️ Mastodon: mastodon.technology/@distrotube
💬 IRC: irc://freenode #distrotube
👫 Reddit: / distrotube
📽️ LBRY: lbry.tv/$/invite/@DistroTube:2
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.audacityteam.org/
💻 VirtualBox: www.virtualbox.org/
Your support is very much appreciated. Thanks, guys!

Пікірлер: 62

  • @dustinmorse8497
    @dustinmorse84974 жыл бұрын

    cat into grep *Luke Smith enters the chat

  • @Mega_Casual
    @Mega_Casual4 жыл бұрын

    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.

  • @kellingc
    @kellingc4 жыл бұрын

    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.

  • @HeikoRehm
    @HeikoRehm4 жыл бұрын

    DT - The Human Manpage! Love it!

  • @birkner510
    @birkner5104 жыл бұрын

    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.

  • @zizzu549
    @zizzu5494 жыл бұрын

    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

  • @AnzanHoshinRoshi
    @AnzanHoshinRoshi4 жыл бұрын

    Thank you, Derek.

  • @AndersJackson
    @AndersJackson14 күн бұрын

    -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).

  • @MrZombieSwamp
    @MrZombieSwamp4 жыл бұрын

    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
    @enderger53084 жыл бұрын

    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.

  • @letsplayonlinux4383
    @letsplayonlinux43834 жыл бұрын

    Great topic

  • @mrmiyagi5
    @mrmiyagi54 жыл бұрын

    I use the The Silver Searcher at work almost daily.

  • @w01dnick

    @w01dnick

    4 жыл бұрын

    Same, switched to ag in almost all tasks. Still grepping logs.

  • @zeocamo
    @zeocamo4 жыл бұрын

    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
    @w01dnick4 жыл бұрын

    3:05 why not to use recursive grep? "-R ." ?

  • @nacthewatcher
    @nacthewatcher4 жыл бұрын

    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.

  • @jahnuchoudhury6677
    @jahnuchoudhury66773 жыл бұрын

    It's really great .. I really liked the you teach ..thanks ... Could you make some video on TCL ?

  • @cattmampbellvideo
    @cattmampbellvideo4 жыл бұрын

    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.

  • @jeetadityachatterjee6995
    @jeetadityachatterjee69954 жыл бұрын

    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

  • @crwmike0
    @crwmike04 жыл бұрын

    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

  • @UnlucksMcGee
    @UnlucksMcGee4 жыл бұрын

    cat myfile | grep rickroll | wc - l Or just: grep -c rickroll myfile :D

  • @dlgang
    @dlgang4 жыл бұрын

    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...

  • @PestisNonSapien_GMO_exHuman
    @PestisNonSapien_GMO_exHuman4 жыл бұрын

    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.*?(?=/)"

  • @mramosch
    @mramosch2 жыл бұрын

    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?

  • @user-ql6kk9cz6o
    @user-ql6kk9cz6o10 ай бұрын

    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
    @enoughmuskspam24904 жыл бұрын

    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 :)

  • @EdHatesNoobTubers
    @EdHatesNoobTubers Жыл бұрын

    Great video, but man it bugs me the audio is not synced

  • @rohitmeharcse9772
    @rohitmeharcse97722 жыл бұрын

    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

  • @zacharycarbon4312
    @zacharycarbon43124 жыл бұрын

    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. ;)

  • @davidgomez79
    @davidgomez794 жыл бұрын

    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.

  • @shiftyworks5630
    @shiftyworks56304 жыл бұрын

    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. ;)

  • @GreyDeathVaccine
    @GreyDeathVaccine4 жыл бұрын

    grep -r "phrase" some dir - search recursively :-)

  • @luaumoal
    @luaumoal4 жыл бұрын

    Take a look at ripgrep

  • @eherrmann01
    @eherrmann013 жыл бұрын

    I'm not sure if I'm amused or disturbed that he has rickroll on his .bashrc.

  • @zweitlander874
    @zweitlander8744 жыл бұрын

    :)

  • @justawfulgamer7738
    @justawfulgamer77384 жыл бұрын

    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
    @asswhole41954 жыл бұрын

    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

  • @FranksThemen
    @FranksThemen4 жыл бұрын

    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.

Келесі