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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -