altering a file inplace. As a result, anything that is not explicitly printed by awk will be missing in the output file.
cat << EOF > test.txtline1 hello world line3EOF awk -i inplace '/hello/ { print "hello everybody"}' test.txt; cat test.txt
hello everybody
$0 variable$0 is the value of the previous line, i.e. the last line of input.{ print $0 } is the default action)1
0
found=1 (use whatever name and value you like )0, we negate the flag value with !\n) between the patterns to match\n) between the patterns to match\n) between the patterns to match(some text before)EXPECTED_TAG1(some text after) here : 0 line in-between (some other text before)EXPECTED_TAG2(some other text after)
\n) between the patterns to match(unknown number of lines before) (some text before)EXPECTED_TAG1(some text after) (unknown number of lines between) (some other text before)EXPECTED_TAG2(some other text after) (unknown number of lines after)
1;unique1 2;duplicate1 3;unique2 4;duplicate1 5;unique3 6;duplicate2 7;duplicate2 8;unique4 9;duplicate1
duplicate1 duplicate2 duplicate1
4;unique_1 3;duplicate 2;duplicate 1;unique_2
4;unique_1 3;duplicate 1;unique_2
$2 is the 2nd field of each processed line (here : the one having duplicates)seen[n] is the item with index n within the seen array (the array name is arbitrary and was chosen for readability)++ is performed after ! (source)!) previously set.
;' -uk2 | sort -nrk1cat -n << EOF | sort -t ';' -uk2 | sort -nrk1 | cut -f 2-
keepMe;unique_1
keepMe;duplicate
keepMe;duplicate
keepMe;unique_2
EOF;' -uk2 | sort -nrk1 | cut -f 2-; rm "$tmpFile"| exclude | ||
|---|---|---|
| 2 fields out of several |
echo {1..9} | awk '{ print $3" "$4 }'
3 4 |
echo {1..9} | awk '{ $3=$4=""; print }'
1 2 5 6 7 8 9 |
| several fields out of many |
echo {1..999} | awk '{ for (i=123; i<=127; i++) printf $i" "; print ""}'
123 124 125 126 127 |
1 2 3 4 5 6 7 8 9 20 21 22 23 24 25 26 27 28 29 30 |
awk '/a regular expression/ {deal with it}' myFile
Since the regular expression is/-delimited, we have to escape those found in the regular expression itself :
echo -e '/a/\n/1/\n/b/\n/2/\n/c/\n/3/' | awk '/\/[a-z]\// {print}'
It it sometimes possible to workaround this :
echo -e '/foo/foo/\n/foo/bar/\n/bar/bar/\n/foo/baz/' | awk 'BEGIN {myRegex = "/foo/.a./"} $0 ~ myRegex {print}'
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Architecture Description
+++===================================================================================================
ii lsb-base 9.20161125 all Linux Standard Base init script functionality
un lsb-core <none> <none> (no description available) not installed
ii lsb-release 9.20161125 all Linux Standard Base version reporting utility
ii|\|\|)/ {print $0}'
||/ Name Version Architecture Description ii lsb-base 9.20161125 all Linux Standard Base init script functionality ii lsb-release 9.20161125 all Linux Standard Base version reporting utility
ii|\|\|)/ { for (i=2; i<=4; i++) printf $i"'$fieldSeparator'"; for (i=5; i<=NF; i++) printf $i" "; print ""}' | column -s "$fieldSeparator" -t
Name Version Architecture Description lsb-base 9.20161125 all Linux Standard Base init script functionality lsb-release 9.20161125 all Linux Standard Base version reporting utilityHow it works :
for (i=2; i<=4; i++) printf $i"'$fieldSeparator'" command prints name + version + architecture (with a separator)for (i=5; i<=NF; i++) printf $i" " command prints the description as a single data field (no separator added)awk '/pattern/ { x = NR + n } NR == x' someFile
{ print $0 } is the default action.for i in {0..9}; do echo "line $i"; done | awk '/line 4/ {lineToDisplay = NR + 3} NR == lineToDisplay'
line 7
for i in {0..9}; do echo "line $i"; done | grep -B3 'line 4' | head -1