unix - Get the last non-empty string from a file using shellscript -
i wrote following code:
success="traffic engineering tunnel validated successfully" text=$(tail -2 /apps/pofitb/logs/tunnels.log) echo $executed_date if [[ $text = $success ]] text=$(tail -2 /apps/pofitb/logs/tunnels.log) echo "tunnel script execution successful" | mailx -s "tunnel script executed succefsfully on $executed_date" abc@gmail.com else echo "tunnel script lastly executed on $executed_date" | mailx -s "tunnel script failed!!!" abc@gmail.com fi exit currently tunnel.log file has blank line while being updated. so, text=$(tail -2 /apps/pofitb/logs/tunnels.log) extracts last non-blank line end of file. works if number of blank line inserted @ end of file 1.
how can modify script such script searches last last non-blank line file tunnel.log, irrespective of number of blank lines inserted ?
awk rescue!
tac log | awk 'nf{print;exit}' if log long, start generous tail first
tail -5 log | tac | awk 'nf{print;exit}' will print last non-empty line.
Comments
Post a Comment