11 examples for awk

{{ score }}
  # Print only the first column of the input
awk '{print $1;}'
        
{{ score }}
  # print first 10 lines of file (emulates behavior of "head")
awk 'NR < 11'

# print first line of file (emulates "head -1")
awk 'NR>1{exit};1'

# print the last 2 lines of a file (emulates "tail -2")
awk '{y=x "\n" $0; x=$0};END{print y}'

# print the last line of a file (emulates "tail -1")
awk 'END{print}'

# print only lines which match regular expression (emulates "grep")
awk '/regex/'

# print only lines which do NOT match regex (emulates "grep -v")
awk '!/regex/'

# print the line immediately before a regex, but not the line
# containing the regex
awk '/regex/{print x};{x=$0}'
awk '/regex/{print (x=="" ? "match on line 1" : x)};{x=$0}'

# print the line immediately after a regex, but not the line
# containing the regex
awk '/regex/{getline;print}'

# grep for AAA and BBB and CCC (in any order)
awk '/AAA/; /BBB/; /CCC/'

# grep for AAA and BBB and CCC (in that order)
awk '/AAA.*BBB.*CCC/'

# print only lines of 65 characters or longer
awk 'length > 64'

# print only lines of less than 65 characters
awk 'length < 64'

# print section of file from regular expression to end of file
awk '/regex/,0'
awk '/regex/,EOF'

# print section of file based on line numbers (lines 8-12, inclusive)
awk 'NR==8,NR==12'

# delete leading whitespace (spaces, tabs) from front of each line
# aligns all text flush left
awk '{sub(/^[ \t]+/, ""); print}'

# delete trailing whitespace (spaces, tabs) from end of each line
awk '{sub(/[ \t]+$/, "");print}'

# delete BOTH leading and trailing whitespace from each line
awk '{gsub(/^[ \t]+|[ \t]+$/,"");print}'
awk '{$1=$1;print}' 

# insert 5 blank spaces at beginning of each line (make page offset)
awk '{sub(/^/, "     ");print}'

# align all text flush right on a 79-column width
awk '{printf "%79s\n", $0}' file*

# center all text on a 79-character width
awk '{l=length();s=int((79-l)/2); printf "%"(s+l)"s\n",$0}' file*

# substitute "foo" with "bar" ONLY for lines which contain "baz"
awk '/baz/{gsub(/foo/, "bar")};{print}'

# substitute "foo" with "bar" EXCEPT for lines which contain "baz"
awk '!/baz/{gsub(/foo/, "bar")};{print}'

# change "scarlet" or "ruby" or "puce" to "red"
awk '{gsub(/scarlet|ruby|puce/, "red"); print}'

# reverse order of lines (emulates "tac")
awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' file*

# if a line ends with a backslash, append the next line to it
# (fails if there are multiple lines ending with backslash...)
awk '/\\$/ {sub(/\\$/,""); getline t; print $0 t; next}; 1' file*

# print and sort the login names of all users
awk -F ":" '{ print $1 | "sort" }' /etc/passwd

# print the first 2 fields, in opposite order, of every line
awk '{print $2, $1}' file

# switch the first 2 fields of every line
awk '{temp = $1; $1 = $2; $2 = temp}' file

# print every line, deleting the second field of that line
awk '{ $2 = ""; print }'

# print in reverse order the fields of every line
awk '{for (i=NF; i>0; i--) printf("%s ",i);printf ("\n")}' file

# remove duplicate, consecutive lines (emulates "uniq")
awk 'a !~ $0; {a=$0}'

# remove duplicate, nonconsecutive lines
awk '! a[$0]++'                     # most concise script
awk '!($0 in a) {a[$0];print}'      # most efficient script

# concatenate every 5 lines of input, using a comma separator
# between fields
awk 'ORS=%NR%5?",":"\n"' file
        
{{ score }}
  # Print only the last column of the input
awk '{print $NF}'
        
{{ score }}
  # Print each line of input, with line numbers
awk '{print NR":",$0;}'
        
{{ score }}
  # Remove duplicate line entries in a file without sorting 
awk '!x[$0]++' 
        
{{ score }}
  # removes lines that exist in file2 from file1
awk 'NR==FNR{a[$0]++;next} !a[$0]' file2.txt file1.txt > output.txt
        
{{ score }}
  # Prints one line for every second field in my_server.log
# 'Fields' are separated by whitespace.
cat my_server.log | awk '{ print $2 }'
        
{{ score }}
  # de-deplicate lines in a text file
awk '!x[$0]++' input.txt > output.txt
        
{{ score }}
  # Calculate the average of a file full of line-separated numbers
awk '{ sum += $2; n++ } END { if (n > 0) print sum / n; }'
        
{{ score }}
  # Edit column/value in CSV-file
# append the string '000' to the third column
awk 'BEGIN{FS=OFS=","} {$3=$3"000"; print}' file1.csv

# add 1000 to the existing value of the third column/value (for numbers, otherwise it will be replaced)
awk 'BEGIN{FS=OFS=","} {$3=$3+"1000"; print}' file1.csv
# or
awk 'BEGIN{FS=OFS=","} {$3+="1000"; print}' file1.csv

# if column 1 is the same as column 2, add/edit column 3 and make it read 'These are the same'
awk 'BEGIN{FS=OFS=","} {if ($1 == $2); $3="These are the same"; print}' file1.csv
        
{{ score }}
  # Print lines between and including the two patterns each time they are found in the input
awk '/Some pattern/,/Some other pattern/'