This shows you the differences between two versions of the page.
Previous revisionLast revision | |||
— | fuss:awk [2021/03/23 02:38] – [Convert Key-Value Map to JSON] office | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Find Longest Line in a File ====== | ||
+ | |||
+ | <code bash> | ||
+ | awk ' { if ( length > x ) { x = length } }END{ print x }' file.txt | ||
+ | </ | ||
+ | |||
+ | ====== Perform Floating Point Calculations ====== | ||
+ | |||
+ | '' | ||
+ | |||
+ | <code bash> | ||
+ | awk 'BEGIN {printf " | ||
+ | </ | ||
+ | |||
+ | will calculate $\sin(30^\circ)=sin(30rad * \frac{\pi}{180})$ and return: | ||
+ | |||
+ | < | ||
+ | 0.500 | ||
+ | </ | ||
+ | |||
+ | Since '' | ||
+ | |||
+ | ====== Delimiter-Style ====== | ||
+ | |||
+ | Suppose you have a file '' | ||
+ | < | ||
+ | variable = value | ||
+ | </ | ||
+ | or: | ||
+ | < | ||
+ | variable = value | ||
+ | </ | ||
+ | |||
+ | The following command can be used to obtain '' | ||
+ | <code bash> | ||
+ | awk -F"[ \t]*[=][ \t]*" '{ print $2 }' input | ||
+ | </ | ||
+ | |||
+ | ====== Remove Duplicate Entries in File Without Sorting ====== | ||
+ | |||
+ | To remove all duplicates from '' | ||
+ | <code bash> | ||
+ | awk ' | ||
+ | </ | ||
+ | |||
+ | ====== Merge Lines by Two in File ====== | ||
+ | |||
+ | Given a file with the contents: | ||
+ | < | ||
+ | Q: What is this? | ||
+ | A: This is a FAQ! | ||
+ | </ | ||
+ | |||
+ | and you would like to achieve: | ||
+ | < | ||
+ | Q: What is this? A: This is a FAQ! | ||
+ | </ | ||
+ | |||
+ | then the following '' | ||
+ | <code bash> | ||
+ | awk ' | ||
+ | </ | ||
+ | |||
+ | will merge all the lines in a file by two. | ||
+ | |||
+ | ====== Convert Key-Value Map to JSON ====== | ||
+ | |||
+ | Given an input such as: | ||
+ | < | ||
+ | a1 : "abc | ||
+ | c2 : def | ||
+ | e3 : "ghi " | ||
+ | f5 :" a | ||
+ | </ | ||
+ | |||
+ | The following awk script: | ||
+ | <code bash> | ||
+ | awk -F: ' | ||
+ | function trim(s) { | ||
+ | return gensub(/^[ \t" | ||
+ | } | ||
+ | BEGIN { | ||
+ | printf " | ||
+ | } | ||
+ | NR > 1 { | ||
+ | printf(", | ||
+ | } | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | END { | ||
+ | print " | ||
+ | } | ||
+ | ' | ||
+ | </ | ||
+ | |||
+ | will return the JSON object: | ||
+ | < | ||
+ | {" | ||
+ | </ | ||
+ | |||
+ | where keys and values are trimmed for spaces and quotes from the start and end. | ||
+ | |||
+ | [[iot/ | ||
+ | <code bash> | ||
+ | apcaccess | awk -F: ' | ||
+ | function trim(s) { | ||
+ | return gensub(/^[ \t" | ||
+ | } | ||
+ | BEGIN { | ||
+ | printf " | ||
+ | } | ||
+ | NR > 1 { | ||
+ | printf(", | ||
+ | } | ||
+ | { | ||
+ | printf(" | ||
+ | } | ||
+ | END { | ||
+ | print " | ||
+ | } | ||
+ | ' | ||
+ | </ | ||
+ | |||
+ | ====== Netstat with Awk ====== | ||
+ | |||
+ | From [[https:// | ||
+ | |||
+ | <code bash> | ||
+ | # Based on gist https:// | ||
+ | # - added TCP states | ||
+ | |||
+ | awk ' | ||
+ | states[" | ||
+ | states[" | ||
+ | states[" | ||
+ | states[" | ||
+ | states[" | ||
+ | states[" | ||
+ | states[" | ||
+ | states[" | ||
+ | states[" | ||
+ | states[" | ||
+ | states[" | ||
+ | } | ||
+ | function hextodec(str, | ||
+ | ret = 0 | ||
+ | n = length(str) | ||
+ | for (i = 1; i <= n; i++) { | ||
+ | c = tolower(substr(str, | ||
+ | k = index(" | ||
+ | ret = ret * 16 + k | ||
+ | } | ||
+ | return ret | ||
+ | } | ||
+ | function getIP(str, | ||
+ | ret=hextodec(substr(str, | ||
+ | for (i=5; i>0; i-=2) { | ||
+ | ret = ret" | ||
+ | } | ||
+ | ret = ret":" | ||
+ | return ret | ||
+ | } | ||
+ | NR > 1 {{if(NR==2)print "Local - Remote"; | ||
+ | |||
+ | </ | ||