Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash script fails to read first line

Tags:

bash

awk

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!

like image 517
Lewp1973 Avatar asked Dec 19 '22 21:12

Lewp1973


1 Answers

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.

like image 50
P.P Avatar answered Dec 30 '22 23:12

P.P