Issue
while [ ! -f /home/stopPing.txt ] ; do
cat /home/ipListforPing.txt | while read output
do
timestamp=$(date +%d-%m-%Y_%H-%M-%S)
timeout 2s ping -qc1 $output 2>&1 | awk -F/ '/^rtt/ { printf "OK %.2f ms\n", $5; ok = 1 } END { if (!ok) print "FAIL" }' >> $output.txt
#echo "----------------"
done
sleep 1.85;
done
Hello, how can I add a timestamp variabl to this output. wherever I type the timestamp variable I get an error. This is the output I want to see,
19-01-2021_03-11-48 OK 0.02 ms
Solution
You can add the timestamp within awk using the strftime function (GNU awk) without the need to use date outside:
timeout 2s ping -qc1 $output 2>&1 | awk -F/ '/^rtt/ { printf strftime("%d-%m-%Y_%H-%M-%S")" OK %.2f ms\n", $5; ok = 1 } END { if (!ok) print "FAIL" }' >> $output.txt
Without GNU awk:
timeout 2s ping -qc1 $output 2>&1 | awk -v timestamp="$(date +%d-%m-%Y_%H-%M-%S)" -F/ '/^rtt/ { printf timestamp" OK %.2f ms\n", $5; ok = 1 } END { if (!ok) print "FAIL" }' >> $output.txt
Pass the result of the date command into awk with -v as a variable timestamp.
Answered By - Raman Sailopal