News for hackers. Stuff that matters.
Re:Shell Script Best Practices, from a decade of scripting t... by ketanmaheshwari on Thursday October 27, 2022 @09:26AM (#33357156)
Awk is awesome but saying it literally takes 1 hour to properly learn it is a bit overselling.
Re:Shell Script Best Practices, from a decade of scripting t... by j0hnyl on Thursday October 27, 2022 @09:37AM (#33357281)
I really don't think so! If you have experience with any scripting, you can fully grok the fundamentals of awk in 1 hour. You might not memorize all the nuances, but you can establish the fundamentals to a degree that most things you would try to achieve would take just a few minutes of brushing up.

For those that haven't taken the time yet, I think this is a good place to start:

https://learnxinyminutes.com/docs/awk/

Of course, some people do very advanced things in awk and I absolutely agree that 1 hour of study isn't going to make you a ninja, but it's absolutely enough to learn the awk programming paradigm so that when the need arises you can quickly mobilize the solution you need.

For example: If you're quick to the draw, it can take less time to write an awk one liner to calculate the average of a column in a csv than it does to copy the csv into excel and highlight the column. It's a massive productivity booster.

Re:Shell Script Best Practices, from a decade of scripting t... by 2devnull on Thursday October 27, 2022 @11:51AM (#33359431)
I feel like I do this every three years then proceed to never use it. Then I read a post on hn and think about how great it could be; rinse and repeat
Re:Shell Script Best Practices, from a decade of scripting t... by boredtofears on Thursday October 27, 2022 @12:03PM (#33359596)
yeah thats exactly right. it may only take an hour to learn, but every time i need to use awk it seems like i have to spend an hour to re-learn its goofy syntax.
Re:Shell Script Best Practices, from a decade of scripting t... by tejtm on Thursday October 27, 2022 @02:36PM (#33361978)
alas this is true, I never correctly recall the order of particular function args as they are fairly random, still beats the alternative of having to continually internalize entire fragile ecosystems to achieve the same goal.
Re:Shell Script Best Practices, from a decade of scripting t... by boredtofears on Thursday October 27, 2022 @03:24PM (#33362705)
yeah you're definitely right. im sure if it was something i had to use more consistently i'd be able to commit it to memory. maybe...
Re:Shell Script Best Practices, from a decade of scripting t... by chasil on Thursday October 27, 2022 @03:50PM (#33363073)
Brian Kernighan covers the entire [new] awk language in 40 pages - chapter 2.

There are people who have asked me scripting questions for over a decade, who will not read this for some reason.

It could be read in an hour, but not fully retained.

https://archive.org/download/pdfy-MgN0H1joIoDVoIC7/

Re:Shell Script Best Practices, from a decade of scripting t... by foobarian on Thursday October 27, 2022 @10:31AM (#33358035)
All you need to do is learn that cmd | awk '{ $5 }' will print out the 5th word as delimited by one or more whitespace characters. Regexes support this easily but are cumbersome to write on the command line.
Re:Shell Script Best Practices, from a decade of scripting t... by bravetraveler on Thursday October 27, 2022 @11:37AM (#33359200)
Doing that, maybe with some inline concatenation to make a new structure, and this are about all I use:

Printing based on another field, example gets UIDs >= 1000:

    awk -F: '$3 >= 1000 {print $0}' /etc/passwd
It can do plenty of magic, but knowing how to pull fields, concat them together, and select based on them cover like 99% of the things I hope to do with it
Re:Shell Script Best Practices, from a decade of scripting t... by every on Thursday October 27, 2022 @12:00PM (#33359559)
And don't forget the invisible $0 field in awk...
Re:Shell Script Best Practices, from a decade of scripting t... by fluential on Sunday October 30, 2022 @08:24AM (#33393817)
And $NF
Re:Shell Script Best Practices, from a decade of scripting t... by metadat on Thursday October 27, 2022 @11:50AM (#33359407)
Awk is an amazingly effective tool for getting things done quickly.

Submitted yesterday:

Learn to use Awk with hundreds of examples

https://github.com/learnbyexample/Command-line-text-processi...

https://news.ycombinator.com/item?id=33349930

Re:Shell Script Best Practices, from a decade of scripting t... by mmh0000 on Thursday October 27, 2022 @02:56PM (#33362294)
What? The awk manual is only 827 highly technical pages[1]. If you can't read and interalize that in an hour, I suspect you're a much worse programmer than the OP.

[1] https://www.gnu.org/software/gawk/manual/gawk.html

For the sarcasm impared among us, everything above this, but possibly including this sentence is sarcasm.

Re:Shell Script Best Practices, from a decade of scripting t... by sargstuff on Friday October 28, 2022 @10:46AM (#33371980)
Think the more relevant script equivalent of 'Everything in this statement is false.' is 'All output must return false to have true side effects.'

The quick one ~ true ~ fix was ! or #! without the 1024k copyright.

s-expression notation avoids the issue with (."contents")

MS Windows interpretation is much more terse & colorful.