AWK One Liners

Predefined Variables in AWK

Variable Contains Variable Contains
NF Number of Fields in current input line/row NR Number of current Row / input line
FNR Like NR but relative to the current file
FILENAME current input file name without path IGNORECASE if ==1 in gawk, ignore case
FS input Field Separator OFS Output Field Separator
RS input Record Separator ORS Output Record Separator
ARGC Count of ARGuments ARGV array of ARGuments
ENVIRON associative array of environment others There are some more. https://www.tutorialspoint.com/awk/awk_built_in_variables.htm
# Print field number 4, 5, and all the rest
awk '{$1=$2=$3=""; print $0}' 

# Split every line of the file x.txt at each comma
awk -F, '{for (i=1; i<=NF; i++) print $i}' x.txt

# Print only filenames of logfiles which contain a line matching /xyz/.
awk '/xyz/{print FILENAME; nextfile}' *.log

Trim leading and ending whitespace of field number two and print result. Attention, gsub does its work in place and returns the number of replacements. So you cannot use print gsub(/^[ \t]+|[ \t]+$/, "", $2);

awk '{gsub(/^[ \t]+|[ \t]+$/, "", $2); print $2}'

Put apostrophes around a field where awk is called from a bash.

awk '{a="\047"; printf("%s%s%s\n", a, $1, a);}'  # \047 for the apostrophe
awk '{printf("+%s+\n", $1)}' | tr + "'"      # with the help of tr

Take first field of each line of file 2.txt, put single apostrophes around it and connect all by commas.

awk '{printf("+%s+, ", $1)}' 2.txt | tr + \'

Pass variable into awk.

awk -v xyz=abc '{print xyz}'  # Print abc. 
awk -v col=3 '{print $col}'   # Print 3rd column.

Split comma separated lines of texts from file 1.txt, trim leading and ending whitespace of fields and convert all into the middle part of an SQL-statement with LIKE.

awk -F, '{for (i=1; i <= NF; ++i) { gsub(/^[ \t]+|[ \t]+$/, "", $i); printf("+%s%s+ or artikel like \n", $i, "%")} }' 1.txt  | tr + "'"

Print filename, line number and line matching xyz in each file (instead of a total line nr). Didn't find quickly a solution in awk, using find -exec:

find . -type f -exec  awk '/xyz/{print FILENAME, NR, $0}' {} \; 
# Use multiple characters -+. and space as field separators.   
awk -F'[-+ .]'     # The delimiter is a regular expression! 

# Print lines from match1 to match2.
awk '/match1/,/match2/'

# ... lines from mat1 to mat2, ignoring case in match expression.
awk 'tolower($0)~"mat1",tolower($0)~"mat2"'  # OR 
awk 'BEGIN{IGNORECASE=1}/mat1,mat2/'         # Only with gawk! 
# Print number of occurences of xy in several log-files
awk 'BEGINFILE{n=0}/xy/{n++} ENDFILE{print FILENAME, n}' *.log  #BEGINFILE and ENDFILE only in gawk!

More AWK help

Perment's AWK One Liners
Tuxgraphics' AWK One Liners
AWK One Liners Explained
Gnu AWK User's Guide