you are viewing a single comment's thread
view the rest of the comments
[–] 4 points 10 months ago* (17 children)

What do y'all use awk for really? 20 of using Linux, I've never had to use awk. And I've done a looot of scripting in my days. Anything from building my own clone of polybar using eww (with loads of scripts underneath), to automated systems for bulk handling of student assignments back at uni when I used to help out with grading and such.

What's awk good for that other standard utilities can't do?

  • source
  • parent
  • hideshow 17 child comments
  • [–] 7 points 10 months ago (2 children)

    I've been using Linux for 25 years, awk is a more recent addition to my arsenal, but rapidly becoming more and more useful.

    For example, awk is extremely helpful if you want to rearrange columns, do math on columns, essentially do things that would take multiple lines of bash with cut and read.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 4 points 10 months ago* (3 children)

    I usually use something like awk '{print $2}' to get a bit of some output

    Ex: list processes, grep for the line I want, then awk out the chunk I want from that line (the pid):

    ps aux | grep myprogram | awk '{print $2}'

  • source
  • parent
  • hideshow 3 child comments
  • [–] 4 points 10 months ago (2 children)

    That's the only think I know how to do with awk, and I reach for it a lot! cut is purpose-built for that function, and is supposedly easier to understand; but it doesn't seem to just work like awk does.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 2 points 10 months ago (1 child)

    I think cut is a little bit finicky because two consecutive occurrences of the cell delimiter counts, and gives an empty cell when selecting the index between them.

    choose is a bit better at this from what I remember, which is like the modern cut, I believe, of course written in Rust.

    Otherwise Nushell excels at this sort of thing, although I don't really use it.

  • source
  • parent
  • hideshow 1 child comment
  • [–] 2 points 10 months ago

    Oh, I hadn't heard about choose!

    I have been using Nushell, and you're right, it is great at parsing input. Commands like detect columns and parse are very nice, and have been supplanting awk for me.

  • source
  • parent
  • [–] 3 points 10 months ago (2 children)

    I both love and hate awk -- on the one hand, it provides the same/similar functionality of similar tools (sed, grep, cut, etc); but it is a bit of a bear and can be pretty slow.

    If you need more "complex" tasks done what would be cumbersome with the rest of the standard tooling, and performance is a non-issue, awk/gawk can probably get it done.

    Though, I too am trying to use it as little as possible in scripts. I think multiple subshells/pipes is still better than awk in some cases. Syntax also leaves a lot to be desired...

  • source
  • parent
  • hideshow 2 child comments
  • [–] 2 points 10 months ago (1 child)

    My experience exactly. I'd rather use a specific tool designed for the task than invoke a whole new language. It just feels... icky for some reason.

  • source
  • parent
  • hideshow 1 child comment
  • [–] 2 points 10 months ago (6 children)

    I use awk instead of cut cause I can remember the syntax better.

    ps aux | grep zoom | grep -v grep | awk '{print $2}' | xargs kill -9

  • source
  • parent
  • hideshow 6 child comments
  • [–] 2 points 10 months ago (2 children)

    You can get rid of those greps, btw:

    ps aux | awk '/zoom/ && $0 !~ /awk/ {print $2}'
    

    Or just use pgrep.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 10 months ago (2 children)

    May I interest you in the command killall? 😁

  • source
  • parent
  • hideshow 2 child comments