Learn AWK tweet by tweet

Joined November 2020
3 Photos and videos
23 May 2021
😎 TIL: ECMAScript / Javascript is influenced by AWK You're welcome #javascript πŸ€ͺ
2
4
18 May 2021
😎 You agree, don't you? 😎
17 May 2021
"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
17 May 2021
πŸ€“ How to print the first and the last line in a file? πŸ€“ awk 'NR==1; END{print}
2
2
17
7 May 2021
πŸ€” 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
2
6
Bitsized AWK retweeted
awk is the coolest tool you don’t know portal.drewdevault.com/2021/…

2
22
79
4 May 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}'
1
16
30 Apr 2021
πŸ€” Is there an AWK script you don't understand? Post it here ⬇️
1
2
4
28 Apr 2021
😎 While AWK does support if statements, patterns are often easier πŸ’‘
1
2
11
26 Apr 2021
πŸ€“ 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
23 Apr 2021
How to print only odd lines πŸ€“
24 Dec 2020
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
21 Apr 2021
A third of the participants has known AWK for more than ten years 🀯
20 Apr 2021
For how long have you known about AWK? #Linux #DevOps
20 Apr 2021
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
2
13 Apr 2021
πŸ€” 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
8 Apr 2021
😍 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
5 Apr 2021
πŸ€” How to print the container id of a running postgres docker container? πŸ€” > docker ps | awk '/postgres/{print $1}' 😎 Easy, right?
1
7
1 Apr 2021
😎 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
29 Mar 2021
πŸ€” 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
12 Feb 2021
πŸ§‘β€πŸŽ“ 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
22 Mar 2021
😎 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
11
19 Mar 2021
πŸ€“ 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