Equbal Quasim
Equbal Quasim

Reputation: 1

How to parse last value from CSV every five minutes?

I need to run script to parse the value from csv every five minutes but should not parse the all the values all the time it should be from the last point.

Upvotes: 1

Views: 108

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207465

Really just putting some flesh on @hek2mgl's suggestion and implementing it for you.

I store the last known length of the logfile in another file called lastlength.

#!/bin/bash

LOGFILE=somelog.csv
LASTLEN=lastlength

# Pre-set seek to start of file...
seek=0

# ... but overwrite if there was a previously seen value
[ -f lastlength ] && seek=$(cat lastlength)

echo DEBUG: Starting from offset $seek

# Update last seen length into file - parameters to "stat" will differ on Linux
stat -f "%Dz" "$LOGFILE" > "$LASTLEN"

# Get last line of file starting from previous position
dd if="$LOGFILE" bs=$seek skip=1 2> /dev/null | tail -1

I am using OSX, so if you are using Linux, the parameters to the stat command in the second to last line will be different, probably

stat -c%s "$LOGFILE"  > "$LASTLEN"

I'll leave you to put it into your crontab so it gets called every 5 minutes.

Upvotes: 1

Related Questions