diff --git a/sheets/awk b/sheets/awk index b873a65..72ab572 100644 --- a/sheets/awk +++ b/sheets/awk @@ -7,6 +7,33 @@ printf '1\n2\n3\n' | awk '{sum += $1} END {print sum}' # Using specific character as separator to sum integers from a file or STDIN. printf '1:2:3' | awk -F ":" '{print $1+$2+$3}' +# Print line number 12 of file.txt +awk 'NR==12' file.txt + +# Print first field for each record in file.txt +awk '{print $1}' file.txt + +Print only lines that match regex in file.txt +awk '/regex/' file.txt + +# Print only lines that do not match regex in file.txt +awk '!/regex/' file.txt + +# Print any line where field 2 is equal to "foo" in file.txt +awk '$2 == "foo"' file.txt + +# Print lines where field 2 is NOT equal to "foo" in file.txt +awk '$2 != "foo"' file.txt + +# Print line if field 1 matches regex in file.txt +awk '$1 ~ /regex/' file.txt + +# Print line if field 1 does NOT match regex in file.txt +awk '$1 !~ /regex/' file.txt + +# Print lines with more than 80 characters in file.txt +awk 'length > 80' file.txt + # Print a multiplication table. awk -v RS='' ' { @@ -100,3 +127,9 @@ awk ' } } ' FILE + +# Remove duplicate lines +awk '!seen[$0]++' file.txt + +# Remove all empty lines +awk 'NF > 0' file.txt