#!/usr/bin/env bash tmp=$(mktemp) while IFS= read -r line; do echo $line >> $tmp done last_line=$(cat $tmp | tail -n1) cat $tmp | awk -F"," 'BEGIN{LAST=-1;skipped=0} {if(LAST!=$3){print $1","$2","$3;skipped=0}else{skipped=1} LAST=$3} END{if(skipped){print "'$last_line'"}}' rm $tmp