awk
Table of Contents
- 1. Match every lines starting with
Link:or[number]except[1] - 2. Print the total amount from a list of numbers found on the first field (
$1) - 3. Print value in matching
FIELD=from each lines - 4. Print the last 2 fields, separated by
., returnfoobar - 5. Return first field with the highest number
- 6. References
1. Match every lines starting with Link: or [number] except [1]
Link: https://example.com/foobar1 Links: [1]: https://example.com/foobar1 [2]: https://example.com/foobar4
awk '/^Link:/ || (/^\[[0-9]+\]/ && ! /^\[1\]/) {print $2}' foo.txt
1.1. Print unique links without changing the order of the list
awk '/^Link:/ || /^\[[0-9]+\]/ { if (!seen[$2]++) print $2}' foo.txt
2. Print the total amount from a list of numbers found on the first field ($1)
awk '{sum+=$1} END {print sum}'
3. Print value in matching FIELD= from each lines
awk '{for (i=1; i<=NF; i++) if ($i ~ /FIELD=/) print $i}'
4. Print the last 2 fields, separated by ., return foobar
echo "buz.foo.bar" | awk -F '.' '{print $(NF-1)$NF}'
5. Return first field with the highest number
printf '8,buz\n42,foo\n27,bar' | awk -F, 'NR == 1 { var = $1 } { if ($1 > var ) var = $1 } END { print var }' # \__________________/ \_________________________/ \_______________/ # | | | # while we are on the run this on every lines run this after # first line, set the every lines have # variable "var" to been processed # the value of the # first field $1 # delimited by ","