lddgnz lddgnz - 9 months ago 73
Bash Question

add and maintain sequence continuity across files using shell script

I am looking for help to insert a sequence number column in a file.

I have a file generated everyday with data like:




I need to add a sequence number column to this, so that the data comes out as:



... and so on.

But that's not it. The next day when a new file is generated, the continuity of the sequence should be maintained from the previous day file.

For e.g. if the last line of the previous day file was:


then my current day file sequence number should start with 3.

Can this be achieved with shell script ? Any help is appreciated.

Thanks in advance

Answer Source

Here are some initial thoughts. Depending on how/where these files are stored you can use ls or find to find the latest file. If none is found then you know you have to start your numbering at 1. If a file is found, a combination of tail and/or sed will make it easy to find the last sequence number used. Adding the sequence number column is almost trivial with awk.