I thought I’ll make this thread for all of you out there who have questions but are afraid to ask them. This is your chance!

I’ll try my best to answer any questions here, but I hope others in the community will contribute too!

  • Godort@lemm.ee
    link
    fedilink
    arrow-up
    16
    arrow-down
    1
    ·
    9 months ago

    Maybe not a super beginner question, but what do awk and sed do and how do I use them?

    • mumblerfish@lemmy.world
      link
      fedilink
      arrow-up
      15
      ·
      9 months ago

      This is 80% of my usage of awk and sed:

      “ugh, I need the 4th column of this print out”: command | awk '{print $4}'

      Useful for getting pids out of a ps command you applied a bunch of greps to.

      ”hm, if I change all ‘this’ to ‘that’ in the print out, I get what I want": command | sed "s/this/that/g"

      Useful for a lot of things, like “I need to change the urls in this to that” or whatever.

      Basically the rest I have to look up.

    • harsh3466@lemmy.ml
      link
      fedilink
      arrow-up
      4
      ·
      edit-2
      9 months ago

      If you’re gonna dive into sed and awk, I’d also highly recommend learning at least the basics of regular expressions. The book Mastering Regular Expressions has been tremendously helpful for me.

      Edit: a letter. Stupid autocorrect.

    • Ramin Honary@lemmy.ml
      link
      fedilink
      English
      arrow-up
      4
      ·
      edit-2
      9 months ago

      Awk is a programming language designed for reading files line by line. It finds lines by a pattern and then runs an action on that line if the pattern matches. You can easily write a 1-line program on the command line and ask Awk to run that 1-line program on a file. Here is a program to count the number of “comment” lines in a script:

      awk 'BEGIN{comment_count=0;} /^[[:space:]]*[#]/{comment_count++;} END{print(comment_count);}' file.sh
      

      It is a good way to inspect the content of files, espcially log files or CSV files. But Awk can do some fairly complex file editing operations as well, like collating multiple files. It is a complete programming language.

      Sed works similar to Awk, but it is much simplified, and designed mostly around CLI usage. The pattern language is similar to Awk, but the commands are usually just one or two letters representing actions like “print the line” or “copy the line to the in-memory buffer” or “dump the in-memory buffer to output.”

    • neidu2@feddit.nl
      link
      fedilink
      arrow-up
      2
      ·
      edit-2
      9 months ago

      Probably a bit narrow, but my usecases:

      • awk: modify STDIN before it goes to STDOUT. Example: only print the 3rd word for each line
      • sed: run a regex on every line.