Search For Patterns Within Files With Grep

Sdílet
Vložit
  • čas přidán 20. 07. 2020
  • 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!
  • Věda a technologie

Komentáře • 61

  • @dustinmorse8497
    @dustinmorse8497 Před 4 lety +38

    cat into grep
    *Luke Smith enters the chat

  • @Mega_Casual
    @Mega_Casual Před 4 lety +8

    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 Před 4 lety +1

      You can also use pgrep for that.

    • @___9136
      @___9136 Před 4 lety

      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 Před 4 lety +23

    Now 40hours tutorial on advanced regular expressions thanks.

    • @DistroTube
      @DistroTube  Před 4 lety +12

      Regular expressions shouldn't require a 40-hour series of videos.

    • @zizzu549
      @zizzu549 Před 4 lety +4

      ​@@DistroTube Regular expressions for turtles!

    • @theemacsen1518
      @theemacsen1518 Před 4 lety +3

      @@DistroTube I think he was joking XD

  • @kellingc
    @kellingc Před 4 lety +2

    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 Před 4 lety +6

    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.

  • @HeikoRehm
    @HeikoRehm Před 4 lety +3

    DT - The Human Manpage! Love it!

  • @AnzanHoshinRoshi
    @AnzanHoshinRoshi Před 4 lety +2

    Thank you, Derek.

  • @MrZombieSwamp
    @MrZombieSwamp Před 4 lety +15

    you could've used grep --color=auto for better visualization

  • @anirbankopty9418
    @anirbankopty9418 Před rokem +1

    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.

  • @mrmiyagi5
    @mrmiyagi5 Před 4 lety +5

    I use the The Silver Searcher at work almost daily.

    • @w01dnick
      @w01dnick Před 4 lety +1

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

  • @zeocamo
    @zeocamo Před 4 lety +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

  • @letsplayonlinux4383
    @letsplayonlinux4383 Před 4 lety +1

    Great topic

  • @w01dnick
    @w01dnick Před 4 lety +6

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

  • @enderger5308
    @enderger5308 Před 4 lety +7

    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.

  • @nacthewatcher
    @nacthewatcher Před 4 lety

    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 Před 4 lety +6

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

  • @jahnuchoudhury6677
    @jahnuchoudhury6677 Před 3 lety

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

  • @cattmampbellvideo
    @cattmampbellvideo Před 3 lety +2

    This was so helpful! Would you consider making a video on Regular Expressions? Thanks!

    • @genkiferal7178
      @genkiferal7178 Před rokem

      I'd like o see the top 10 **useful** RegEx that the average person would use - not what a dev would use.

  • @jeetadityachatterjee6995
    @jeetadityachatterjee6995 Před 4 lety +1

    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
    @crwmike0 Před 4 lety +3

    The "command line snobs" would also tell you that cat is not for displaying files.

  • @dlgang
    @dlgang Před 4 lety +1

    cool, I was thinking about grep the other day, no joke

    • @DistroTube
      @DistroTube  Před 4 lety +3

      Yea, sometimes I'm just sitting around and all of a sudden "grep" pops into my head. ;)

    • @hakumaou5929
      @hakumaou5929 Před 4 lety +1

      google reads minds...creepy...

  • @enoughmuskspam2490
    @enoughmuskspam2490 Před 4 lety +3

    A piece of advance ta anyone watching this video just use ripgrep instead it's better in every possible way.

  • @mramosch
    @mramosch Před 2 lety

    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-ql6kk9cz6o Před 9 měsíci

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

  • @rohitmeharcse9772
    @rohitmeharcse9772 Před 2 lety

    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

  • @davidgomez79
    @davidgomez79 Před 4 lety +2

    command line snobs.. now I know how to describe them.

    • @folksurvival
      @folksurvival Před 3 lety

      He was talking about Luke Smith.

    • @davidgomez79
      @davidgomez79 Před 3 lety

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

  • @EdHatesNoobTubers
    @EdHatesNoobTubers Před rokem

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

  • @PestisNonSapien_GMO_exHuman

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

  • @GreyDeathVaccine
    @GreyDeathVaccine Před 4 lety

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

  • @shiftyworks5630
    @shiftyworks5630 Před 4 lety +4

    why not awk?

    • @DistroTube
      @DistroTube  Před 4 lety +13

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

  • @zacharycarbon4312
    @zacharycarbon4312 Před 4 lety +2

    did you do what i think you did? was that a masterfully subtle rickrolling @1:06 ... was this whole thing a rickroll ?

    • @DistroTube
      @DistroTube  Před 4 lety +2

      Well, if you are using my bashrc, all you need to do is type "rr" for the terminal rickroll. ;)

  • @luaumoal
    @luaumoal Před 4 lety +2

    Take a look at ripgrep

  • @eherrmann01
    @eherrmann01 Před 3 lety +1

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

  • @zweitlander874
    @zweitlander874 Před 4 lety +3

    :)

  • @justawfulgamer7738
    @justawfulgamer7738 Před 4 lety

    Am I the only one that finds it odd that he doesn't look at the camera?

    • @stephenjones8645
      @stephenjones8645 Před 4 lety +1

      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 Před 4 lety +3

    First

    • @ikichullo
      @ikichullo Před 4 lety +9

      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 Před 4 lety +6

      @@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 Před 4 lety +10

      Nice, don't forget to put this in your resume, it really helps :)

    • @asswhole4195
      @asswhole4195 Před 4 lety +6

      @@a_maxed_out_handle_of_30_chars Already got that covered.

    • @DistroTube
      @DistroTube  Před 4 lety +16

      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
    @FranksThemen Před 4 lety +1

    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.