😎 TIL: ECMAScript / Javascript is influenced by AWK
You're welcome #javascript 🤪
😎 You agree, don't you? 😎
"In terms of programming languages you get the most bang for the buck by learning AWK."
- Brian Kernighan
podquotes.io/quote/21
🤔 Do you want to write better and portable AWK scripts?
Of course you want 😄
💡 GAWK (GNU AWK) supports --lint:
> awk --lint 'and($1,$2)' file.txt
awk: cmd. line:1: warning: `and' is a gawk extension
Bitsized AWK retweeted
awk is the coolest tool you don’t know
portal.drewdevault.com/2021/…
🤔 Do you find your scripts with all these $1, $2 etc unreadable? 🤔
💡 Try giving them names:
> docker ps | awk 'BEGIN{cont_id=1;image=2;}$image=="postgres"{print $cont_id}'
🤓 New week new trick! 🤓
🤔 Replace a string in AWK:
> awk '{gsub("bob", "anna")}1' file.txt
➡️ replace "bob" with "anna"
➡️ 1st param can be a regex (eg. /bob/)
➡️ 3rd param is optional (default $0 / line)
💡sed might be more suitable for such tasks 😉
How to print only odd lines 🤓
Replying to @mawkic
> awk 'NR%2 {print $1}' file.txt
anna
chad
👨🎨 This prints odd lines, because %2 (modulo 2) returns 0 if the NR is divisible by 2. Since 0 is false, those lines are not printed.
➡️ The block is easy: just print the first column
🤔 Did you know you can write a self-contained AWK script? 🤔
➡️ Given a file:
> cat script.awk
#!/usr/bin/awk -f
{ print $0 }
➡️ Make it executable
> chmod +x script.awk
➡️ And run it
> ./script.awk file.txt
🐧 If only everything was so easy #Linux 🐧
😍 Last July, @lexfridman interviewed Brian Kernighan, one of the authors of AWK. Listen to Brian explaining why AWK is still very useful today.
youtube.com/watch?v=W5kr7X7E…
🤔 How to print the container id of a running postgres docker container? 🤔
> docker ps | awk '/postgres/{print $1}'
😎 Easy, right?
😎 How to print all columns but the first? 😎
> awk '{$1="";$0=$0} NF=NF' file.txt
💡 Change to $1=$2="" to omit the second column as well
💡 The $0=$0 and NF=NF gymnastic is needed to make AWK recalculate what to print. If you don't do it, you might see leading spaces 😉
🤔 Did you ever pipe grep into AWK? 🤔
> grep x file.txt | awk '{print $1}'
💡 No need to grep first, AWK can do it:
> awk '/x/{print $1}' file.txt
➡️ Before the command ({..}) is the pattern. The pattern can be a regex (between slashes) and matches on anything in the line
Bitsized AWK retweeted
🧑🎓 How to copy/paste CSV (or other data) into a google spreadsheet? 🧑🎓
> awk -F';' 'BEGIN{OFS="\t"} $1=$1' file.txt | xclip -sel clip
➡️ Make the data tab-separated and put it into the clipboard
➡️ Now you can use Ctrl-V in a spreadsheet
#Linux #AWK #codingisfun





