When it comes to command line text processing, from an abstract point of view, there are three major pillars



Download 125.91 Kb.
Page59/60
Date09.03.2023
Size125.91 Kb.
#60849
1   ...   52   53   54   55   56   57   58   59   60
Learn GNU AWK

Forcing numeric context


Use unary operator + to force numeric conversion. A variable might have numeric operations but still not get assigned a number if there's no input to read. So, when printing a variable that should be a number, use unary + to ensure it prints 0 instead of empty string.
$ # numbers present in last column, no issues $ awk '{sum += $NF} END{print sum}' table.txt 38.14 $ # strings in first column, gets treated as 0 $ awk '{sum += $1} END{print sum}' table.txt 0 $ # no input at all, empty string is printed $ awk '{sum += $1} END{print sum}' /dev/null $ # forced conversion to number, so that 0 is printed $ awk '{sum += $1} END{print +sum}' /dev/null 0
The -N option (or --use-lc-numeric) is useful to work with floating-point numbers based on the current locale.
$ # my locale uses . for decimal point $ echo '3.14' | awk '{$0++} 1' 4.14 $ echo '3,14' | awk '{$0++} 1' 4 $ echo '3,14' | LC_NUMERIC=de_DE awk -N '{$0++} 1' 4,14

Forcing string context


Concatenate empty string to force string comparison.
$ # parentheses around first argument to print used for clarity $ # fields get compared as numbers here $ echo '5 5.0' | awk '{print ($1==$2 ? "same" : "different"), "number"}' same number $ # fields get compared as strings here $ echo '5 5.0' | awk '{print ($1""==$2 ? "same" : "different"), "string"}' different string

Negative NF


Manipulating NF sometimes leads to a negative value. Fortunately, awk throws an error instead of behaving like uninitialized variable.
$ # file with different number of fields $ cat varying.txt parrot good cool awesome blue sky 12 34 56 78 90 $ # delete last two fields $ awk '{NF -= 2} 1' varying.txt awk: cmd. line:1: (FILENAME=varying.txt FNR=1) fatal: NF set to negative value $ # add a condition to check number of fields $ # assuming that lines with less than 3 fields should be preserved $ awk 'NF>2{NF -= 2} 1' varying.txt parrot good blue sky 12 34 56
Here's another example, which needs to access third field from the end.
$ awk '{print $(NF-2)}' varying.txt awk: cmd. line:1: (FILENAME=varying.txt FNR=1) fatal: attempt to access field -1 $ # print only if there are minimum 3 fields $ awk 'NF>2{print $(NF-2)}' varying.txt good 56

Download 125.91 Kb.

Share with your friends:
1   ...   52   53   54   55   56   57   58   59   60




The database is protected by copyright ©ininet.org 2024
send message

    Main page