2020-02-22 03:38:15 +00:00
|
|
|
# sum integers from a file or STDIN, one integer per line:
|
2018-01-12 10:08:02 +00:00
|
|
|
printf '1\n2\n3\n' | awk '{ sum += $1} END {print sum}'
|
|
|
|
|
2020-02-22 03:38:15 +00:00
|
|
|
# using specific character as separator to sum integers from a file or STDIN
|
2018-01-12 10:08:02 +00:00
|
|
|
printf '1:2:3' | awk -F ":" '{print $1+$2+$3}'
|
|
|
|
|
|
|
|
# print a multiplication table
|
|
|
|
seq 9 | sed 'H;g' | awk -v RS='' '{for(i=1;i<=NF;i++)printf("%dx%d=%d%s", i, NR, i*NR, i==NR?"\n":"\t")}'
|
|
|
|
|
|
|
|
# Specify output separator character
|
|
|
|
printf '1 2 3' | awk 'BEGIN {OFS=":"}; {print $1,$2,$3}'
|
|
|
|
|
|
|
|
# search for a paragraph containing string
|
|
|
|
awk -v RS='' '/42B/' file
|
2019-03-15 10:03:07 +00:00
|
|
|
|
|
|
|
# display only first column from multi-column text
|
|
|
|
echo "first-column second-column third-column" | awk '{print $1}'
|
2019-11-06 00:11:43 +00:00
|
|
|
|
2020-02-22 03:38:15 +00:00
|
|
|
# Use AWK solo; without the need for something via STDIN.
|
2019-11-06 00:11:43 +00:00
|
|
|
awk BEGIN'{printf("Example text.\n")}'
|
2019-11-06 00:18:59 +00:00
|
|
|
|
2020-02-22 03:38:15 +00:00
|
|
|
# Accessing environment variables from within AWK.
|
2019-11-06 00:18:59 +00:00
|
|
|
awk 'BEGIN{print ENVIRON["LS_COLORS"]}'
|
2019-11-06 00:28:10 +00:00
|
|
|
|
|
|
|
# One method to count the number of lines; in this case, read from STDIN.
|
|
|
|
free | awk '{i++} END{print i}'
|
2019-11-22 02:03:22 +00:00
|
|
|
|
2020-02-22 03:38:15 +00:00
|
|
|
# Output unique list of available sections under which to create a DEB package.
|
2019-11-22 02:03:22 +00:00
|
|
|
awk '!A[$1]++{print($1)}' <<< "$(dpkg-query --show -f='${Section}\n')"
|
2019-11-26 23:49:20 +00:00
|
|
|
|
2020-02-22 03:38:15 +00:00
|
|
|
# Using process substitution (`<()` is NOT command substitution), with AWK and
|
|
|
|
# its associative array variables, we can print just column 2 for lines whose
|
|
|
|
# first column is equal to what's between the double-quotes.
|
2019-11-26 23:49:20 +00:00
|
|
|
awk '{NR!=1&&A[$1]=$2} END{print(A["Mem:"])}' <(free -h)
|
2020-02-22 03:38:15 +00:00
|
|
|
# While below is an easier and simpler solution to the above, it's not at all
|
|
|
|
# the same, and in other cases, the above is definitely preferable.
|
2019-11-26 23:49:20 +00:00
|
|
|
awk '/^Mem:/{print($2)}' <(free -h)
|
Fetch unique list of uppercase-only variables
Just wrote this because I'm wanting to convert numerous uppercase
variables in lots of shell programs to lowercase, except the initial
letter of a word. Manually, doing this would take forever.
First, however, I need to compile a list of variable names so I know
for what I want to search and replace, then omit certain ones typically
named in all uppercase, like `UID`, `USER`, `HOSTNAME`, etc, which I
will probably do by parsing `env` and various other special parameters
used by Bash.
Fun times.
2019-11-27 16:44:28 +00:00
|
|
|
|
2020-02-22 03:38:15 +00:00
|
|
|
# Output list of unique uppercase-only, sigil-omitted variables used in [FILE].
|
Fetch unique list of uppercase-only variables
Just wrote this because I'm wanting to convert numerous uppercase
variables in lots of shell programs to lowercase, except the initial
letter of a word. Manually, doing this would take forever.
First, however, I need to compile a list of variable names so I know
for what I want to search and replace, then omit certain ones typically
named in all uppercase, like `UID`, `USER`, `HOSTNAME`, etc, which I
will probably do by parsing `env` and various other special parameters
used by Bash.
Fun times.
2019-11-27 16:44:28 +00:00
|
|
|
awk '{for(F=0; F<NF; F++){if($F~/^\$[A-Z_]+$/){A[$F]++}}} END{for(I in A){X=substr(I, 2, length(I)); printf("%s\n", X)}}' [FILE]
|
2019-12-01 13:00:13 +00:00
|
|
|
|
2020-02-22 03:38:15 +00:00
|
|
|
# Output only lines from FILE between PATTERN_1 and PATTERN_2. Good for logs.
|
2019-12-01 13:00:13 +00:00
|
|
|
awk '/PATTERN_1/,/PATTERN_2/{print}' [FILE]
|
2019-12-05 01:01:13 +00:00
|
|
|
|
|
|
|
# Pretty-print a table of an overview of the non-system users on the system.
|
|
|
|
awk -SPF ':' 'BEGIN {printf("%-17s %-4s %-4s %-s\n", "NAME", "UID", "GID", "SHELL")} $3>=1000 && $1!="nobody" {printf("%-17s %-d %-d %-s\n", $1, $3, $4, $7)}' /etc/passwd
|
2020-02-29 23:43:42 +00:00
|
|
|
|
|
|
|
# Display the total amount of MiB of RAM available in the machine. This is also
|
|
|
|
# a painful but useful workaround to get the units comma-separated, as would be
|
|
|
|
# doable with Bash's own `printf` built-in.
|
|
|
|
awk '/^MemTotal:/ {printf("%'"'"'dMiB\n", $2 / 1024)}'
|