Read logline line by line with awk/sed

Hello,

I have a logfile which is in this format:

1211667249500#3265
1211667266687#2875
1211667270781#1828

Is there a way to read the logfile line by line every time I execute the code and put the two numbers in the line in two separate variables?

Something like:

1211667249500#3265

Into:

var1=1211667249500 var2=3265

Thanks

You mean you execute the script, and you get two variables? You can try this.

#!/bin/bash
#GetVarOnceALine.ksh <logfilename>

line=`head -1 $1`
if [ $line -eq "" ]; then
echo "Logfile is empty now"
exit 1;
fi

sed '2,$ p' <$1 >/tmp/tmpfile
mv /tmp/tmpfile $1

var1=`echo $line|awk -F# '{print $1}'`
var2=`echo $line|awk -F# '{print $2}'`

exit 0

Save the following as "myscript"...
for i in `cat $1`
do
var1=$(echo $i | awk -F\# '{print $1}')
var2=$(echo $i | awk -F\# '{print $2}')
echo "var1=${var1} var2=${var2}"
done

Assuming your data is in "datafile"...
1211667249500#3265
1211667266687#2875
1211667270781#1828

Run the script....
myscript datafile

Output it produces...
var1=1211667249500 var2=3265
var1=1211667266687 var2=2875
var1=1211667270781 var2=1828

Deleted by owner

Hello,

it's almost working, except one thing. I was a bit unclear about this, sorry!
The logfile gets bigger every time I run a script.

first time:

1211667249500#3265

second time:

1211667249500#3265
1211667266687#2875

third time:

1211667249500#3265
1211667266687#2875
1211667270781#1828

Instead of this as a result from your scripts:

var1=1211667249500 var2=3265
var1=1211667266687 var2=2875
var1=1211667270781 var2=1828

I'd want this as a result:

First time:

var1=1211667249500 var2=3265

Second time:

var1=1211667266687 var2=2875

Third time:

var1=1211667270781 var2=1828

So it actually reads the logfile line by line, instead of all lines at once. But also reads it one line after the previous one.

Thanks for all the replies :cool:

ps.
When I do awk -F'#' '{print $2/1000}' log I get 3.265
and when I do awk -F'#' '{print $1/1000}' log I get 1.21167e+09 is there a way to get the full number instead?

If yout want to get the latest info. One option is to use "tail -f filename"

This will display the latest values as they happen...

tail -f datafile | for i in `cat -`
do
var1=$(echo $i | awk -F\# '{print $1}')
var2=$(echo $i | awk -F\# '{print $2}')
echo "var1=${var1} var2=${var2}"
done

FlyingSquirrel, your last bit of code generates the same output. I found out there's more parameters for tail and tail -n 1 does the job for me.

Thanks everyone for the help, I learned a lot. :smiley:

Next time try to be clear from the beginning :slight_smile:

tail -1 data.file | while read line
do
var1=`echo "scale=3; ${line%#*} / 1000" | bc`
var2=`echo "scale=3; ${line#??????????????} / 1000" | bc`
echo var1 = $var1
echo var2 = $var2
done