Differences

This shows you the differences between two versions of the page.


Previous revision
Next revision
fuss:awk [2018/01/02 01:47] – [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
 +</code>
 +
 +====== Perform Floating Point Calculations ======
 +
 +''awk'' can be used for complex calculations, for example:
 +
 +<code bash>
 +awk 'BEGIN {printf "%.3f\n", sin(30 * atan2(0, -1)/180)}'
 +</code>
 +
 +will calculate $\sin(30^\circ)=sin(30rad * \frac{\pi}{180})$ and return:
 +
 +<code>
 +0.500
 +</code>
 +
 +Since ''awk'' does not have a constant for $\pi$, we use ''atan2(0, -1)'' instead.
 +
 +====== Delimiter-Style ======
 +
 +Suppose you have a file ''input'' containing data separated by delimiters such as:
 +<code>
 +variable = value
 +</code>
 +or:
 +<code>
 +variable = value
 +</code>
 +
 +The following command can be used to obtain ''value'':
 +<code bash>
 +awk -F"[ \t]*[=][ \t]*" '{ print $2 }' input
 +</code>
 +
 +====== Remove Duplicate Entries in File Without Sorting ======
 +
 +To remove all duplicates from ''file.txt'' without sorting, issue:
 +<code bash>
 +awk '!x[$0]++' file.txt
 +</code>
 +
 +====== Merge Lines by Two in File ======
 +
 +Given a file with the contents:
 +<code>
 +Q: What is this?
 +A: This is a FAQ!
 +</code>
 +
 +and you would like to achieve:
 +<code>
 +Q: What is this? A: This is a FAQ!
 +</code>
 +
 +then the following ''awk'' one liner:
 +<code bash>
 +awk 'NR%2{printf "%s ",$0;next;}1' yourFile
 +</code>
 +
 +will merge all the lines in a file by two.
 +
 +====== Convert Key-Value Map to JSON ======
 +
 +Given an input such as:
 +<code>
 +a1 : "abc
 +c2  : def
 +e3 : "ghi "
 +f5 :" a     b"
 +</code>
 +
 +The following awk script:
 +<code bash>
 +awk -F: '
 +    function trim(s) { 
 +        return gensub(/^[ \t"]*|[ \t"]*$/, "", "g", s) 
 +    }
 +    BEGIN {
 +        printf "{"
 +    } 
 +    NR > 1 { 
 +        printf(", "
 +    } 
 +    { 
 +        printf("\"%s\":\"%s\"", trim($1), trim(substr($0, index($0,":") + 1)));
 +    }
 +    END {
 +        print "}"
 +    }
 +'
 +</code>
 +
 +will return the JSON object:
 +<code>
 +{"a1":"abc","c2":"def","e3":"ghi","f5":"    b"}
 +</code>
 +
 +where keys and values are trimmed for spaces and quotes from the start and end.
 +
 +[[iot/openhab2/templates/apc_ups|One example application]] would be creating a JSON object out of the status of an APC UPS:
 +<code bash>
 +apcaccess | awk -F: '
 +    function trim(s) { 
 +        return gensub(/^[ \t"]*|[ \t"]*$/, "", "g", s) 
 +    }
 +    BEGIN {
 +        printf "{"
 +    } 
 +    NR > 1 { 
 +        printf(", "
 +    } 
 +    { 
 +        printf("\"%s\":\"%s\"", trim($1), trim(substr($0, index($0,":") + 1)));
 +    }
 +    END {
 +        print "}"
 +    }
 +'
 +</code>
  

fuss/awk.txt · Last modified: 2022/04/19 08:28 by 127.0.0.1

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.