Reputation: 43
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!
Upvotes: 4
Views: 1194
Reputation: 84561
You can also use tr
to your advantage here:
tr ':' ' ' < /home/timestamp.log
It will provide the translation of characters you need. E.g.:
$ tr ':' ' ' <dat/timestamp.log
00 00 01
00 00 02
00 00 03
Upvotes: 2
Reputation: 332836
read line
is consuming the first line of the input, and assigning it to the Bash variable $line
, which you then ignore. Then the body of the loop is processing the rest of the file using awk.
You can just use awk
directly without the loop:
awk -F: '{ print $1, $2, $3 }' /home/timestamp.log > /home/time.log
Or you can use a loop and not awk:
while IFS=: read h m s; do echo $h $m $s >> /home/time.log; done < /home/timestamp.log
Upvotes: 3
Reputation: 121387
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.
Upvotes: 6