Learn AWK tweet by tweet

Joined November 2020
😎 TIL: ECMAScript / Javascript is influenced by AWK You're welcome #javascript 🤪
2
4
😎 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
2
2
8
🤓 How to print the first and the last line in a file? 🤓 awk 'NR==1; END{print}
2
2
16
🤔 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
awk is the coolest tool you don’t know portal.drewdevault.com/2021/…
2
20
2
78
🤔 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}'
1
16
🤔 Is there an AWK script you don't understand? Post it here ⬇️
1
2
4
😎 While AWK does support if statements, patterns are often easier 💡
1
2
11
🤓 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 😉
1
5
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
1
3
A third of the participants has known AWK for more than ten years 🤯
For how long have you known about AWK? #Linux #DevOps
17%< 1 year
33%< 5 years
11%< 10 years
39%>= 10 years
36 votes • Final results
For how long have you known about AWK? #Linux #DevOps
17%< 1 year
33%< 5 years
11%< 10 years
39%>= 10 years
36 votes • Final results
1
2
🤔 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 🐧
2
9
😍 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…
1
9
🤔 How to print the container id of a running postgres docker container? 🤔 > docker ps | awk '/postgres/{print $1}' 😎 Easy, right?
1
7
😎 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 😉
1
7
🤔 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
3
12
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
1
3
10
😎 How to count non-empty lines in AWK? 😎 > awk 'NF{cnt++}END{print cnt}' file.txt 💡 NF is the number of fields ➡️ true if there is at least one field (NF>0) 💡 END is only run at the end, so we print the variable 'cnt' when the entire file is processed #Linux #AWK
1
10
🤓 Another popular AWK question from StackOverflow: How to print all lines starting from the n-th? 🤔 > awk 'NR>=2' file.txt 💡 remember that the first line has NR=1 ➡️ the above prints from the 2nd line onwards
1
1
9