AWK One Liners

# 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. I found no way to do this with awk in the shell. Instead, I came up with this:

awk '{printf("+%s+\n", $i)}' | tr + "'"

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! 

More AWK help

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

A Tool to Shrink pngs: Pngquant

Sometimes it is needed or wanted to shrink pngs in file size. There is a fine tool for doing this and it is called pngquant. It is available for Windows, Mac, Linux and even Android, as commandline utility or with GUI or as library.

With this tool, we could reduce our pngs of technical drawings to around one third of their former size with no visible difference.
Compare: above before shrinking, 149 kB, below after shrinking, 36 kB.

Markdown, a Tool for Writing Programmers

Markdown is a simple markup language with plain text formatting and a lot less use of angle brackets than HTML or XML.

  • Pandoc can be used to create HTML or nearly any other format you’d like out of markdown.
  • It can be written easily in a text editor like vi or notepad.
  • Unlike HTML, Markdown is well readable in the text editor.
  • So for programmers, it feels very natural to write stuff in markdown. In fact, I’m writing this blog in markdown, using the JP Markdown plugin for WordPress.
  • Here’s a link to a fine Markdown Cheatsheet, handling the Github-variation of Markdown.
  • This complete post including links, italics, bold and unordered list does not contain a single angle bracket.

Finish