I have a script to convert timestamps into seconds so I can eventually work out the averages. I noticed that the number of output lines did not equal the amount of input lines. In fact it does not seem to read the first line. I simplified the code to check it but it is the same every time.
Input file looks like this:
00:00:01
00:00:02
00:00:03
Output like this:
00 00 02
00 00 03
This is the script:
#!/bin/bash
while read line
do
awk -F: '{ print $1, $2, $3 }' > /home/time.log
done < /home/timestamp.log
I'm sure it is something stupid but I cannot see it!
You don't need a loop. You can simply use awk:
awk -F: '{ print $1, $2, $3 }' /home/timestamp.log > /home/time.log
The reason for the behaviour you see is that you don't pass the line
to awk. So the first line is read by read the loop and rest by awk inside the loop as awk gets it stdin from the loop. Hence, you don't see the first line.
If it's still not clear, just add a echo "Hello"
after the awk
call inside your loop and see how many times it gets printed :)
You probably intended to do:
echo $line | awk -F: '{ print $1, $2, $3 }' > /home/time.log
But that's just unnecessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With