#!/usr/bin/env bash outfile="$1.filtered" # Only print the lines we want filter_dat() { awk 'BEGIN { RS = "" ; FS = "\n" } { if ( $2 !~ /(text|error|note|title)/) { print $1 print $2 print $3 if ($4 !~ /UTF-8/) print $4 if ($5 !~ /UTF-8/) print $5 if ($6 !~ /UTF-8/) print $6 if ($7 !~ /UTF-8/) print $7 } }' $1 } # Run filter_dat then pipe output to sed to strip all empty # lines, then add one newline above Name and save it to a file filter_dat $1 | sed '/^$/d; s/Name/\n&/g' > "$outfile" printf '%s%s\n' 'Processing complete! The filtered file is: ' "$outfile"