Search For Patterns Within Files With Grep

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ส.ค. 2024
  • 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 ปีที่แล้ว +38

    cat into grep
    *Luke Smith enters the chat

  • @Mega_Casual
    @Mega_Casual 4 ปีที่แล้ว +9

    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 ปีที่แล้ว +1

      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
    @kellingc 4 ปีที่แล้ว +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.

  • @HeikoRehm
    @HeikoRehm 4 ปีที่แล้ว +3

    DT - The Human Manpage! Love it!

  • @zizzu549
    @zizzu549 4 ปีที่แล้ว +23

    Now 40hours tutorial on advanced regular expressions thanks.

    • @DistroTube
      @DistroTube  4 ปีที่แล้ว +12

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

    • @zizzu549
      @zizzu549 4 ปีที่แล้ว +4

      ​@@DistroTube Regular expressions for turtles!

    • @theemacsen1518
      @theemacsen1518 4 ปีที่แล้ว +3

      @@DistroTube I think he was joking XD

  • @birkner510
    @birkner510 4 ปีที่แล้ว +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.

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

  • @AnzanHoshinRoshi
    @AnzanHoshinRoshi 4 ปีที่แล้ว +2

    Thank you, Derek.

  • @MrZombieSwamp
    @MrZombieSwamp 4 ปีที่แล้ว +15

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

  • @enderger5308
    @enderger5308 4 ปีที่แล้ว +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.

  • @w01dnick
    @w01dnick 4 ปีที่แล้ว +6

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

  • @mrmiyagi5
    @mrmiyagi5 4 ปีที่แล้ว +5

    I use the The Silver Searcher at work almost daily.

    • @w01dnick
      @w01dnick 4 ปีที่แล้ว +1

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

  • @anirbankopty9418
    @anirbankopty9418 ปีที่แล้ว +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.

  • @zeocamo
    @zeocamo 4 ปีที่แล้ว +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

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

  • @cattmampbellvideo
    @cattmampbellvideo 4 ปีที่แล้ว +2

    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.

  • @UnlucksMcGee
    @UnlucksMcGee 4 ปีที่แล้ว +6

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

  • @jeetadityachatterjee6995
    @jeetadityachatterjee6995 4 ปีที่แล้ว +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 4 ปีที่แล้ว +3

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

  • @letsplayonlinux4383
    @letsplayonlinux4383 4 ปีที่แล้ว +1

    Great topic

  • @dlgang
    @dlgang 4 ปีที่แล้ว +1

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

    • @DistroTube
      @DistroTube  4 ปีที่แล้ว +3

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

    • @hakumaou5929
      @hakumaou5929 4 ปีที่แล้ว +1

      google reads minds...creepy...

  • @user-ql6kk9cz6o
    @user-ql6kk9cz6o 10 หลายเดือนก่อน

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

  • @jahnuchoudhury6677
    @jahnuchoudhury6677 3 ปีที่แล้ว

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

  • @enoughmuskspam2490
    @enoughmuskspam2490 4 ปีที่แล้ว +3

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

  • @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?

  • @zacharycarbon4312
    @zacharycarbon4312 4 ปีที่แล้ว +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  4 ปีที่แล้ว +2

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

  • @davidgomez79
    @davidgomez79 4 ปีที่แล้ว +2

    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.

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

  • @shiftyworks5630
    @shiftyworks5630 4 ปีที่แล้ว +4

    why not awk?

    • @DistroTube
      @DistroTube  4 ปีที่แล้ว +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. ;)

  • @eherrmann01
    @eherrmann01 3 ปีที่แล้ว +1

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

  • @luaumoal
    @luaumoal 4 ปีที่แล้ว +2

    Take a look at ripgrep

  • @GreyDeathVaccine
    @GreyDeathVaccine 4 ปีที่แล้ว

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

  • @zweitlander874
    @zweitlander874 4 ปีที่แล้ว +3

    :)

  • @FranksThemen
    @FranksThemen 4 ปีที่แล้ว +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.

  • @justawfulgamer7738
    @justawfulgamer7738 4 ปีที่แล้ว

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

    • @stephenjones8645
      @stephenjones8645 4 ปีที่แล้ว +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 4 ปีที่แล้ว +3

    First

    • @ikichullo
      @ikichullo 4 ปีที่แล้ว +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 4 ปีที่แล้ว +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 4 ปีที่แล้ว +10

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

    • @asswhole4195
      @asswhole4195 4 ปีที่แล้ว +6

      @@a_maxed_out_handle_of_30_chars Already got that covered.

    • @DistroTube
      @DistroTube  4 ปีที่แล้ว +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