modify the test file by any script

Hi All the Helpers!

I have a text file which looks like input.txt.I would request to please suggest me how can I make this file look like output.txt

input.txt

VOP 111 0 1 2
DEM 111 0 222 333 444 555
DEM       879 888 987 888 989
DEM       879 888 987 888 989      

VOP 118 0 12 3 6
DEM 118 0 333 555 768 946
DEM       879 888 987 888 989
DEM       879 888 987 888 989

output.txt

VOP 0001 0 1 2
DEM 0001 0 222 333 444 555
DEM       879 888 987 888 989
DEM       879 888 987 888 989

VOP 0002 0 12 3 6
DEM 0002 0 333 555 768 946
DEM       879 888 987 888 989
DEM       879 888 987 888 989

So I need that the second column value get replaced by 0001,0002,0003 and goes on with incremantal 0001,but only for VOP and the 1st DEM after VOP.

Your help appreciated as always!!
Best Regards

Try this:

awk '
/^VOP/ {$2 = sprintf("%04d", ++cnt)
        print
        c1 = 1
        next
}
c1 && /^DEM/ {
        $2 = sprintf("%04d", cnt);
        c1 = 0
}
{       print}' input.txt > output.txt

And what was your other thread about then?

Hi Senari,
The input file is bit modified.The last script was doing like for all the lines with DEM.But now I want it only for the line with DEM just following the VOP.
Any idea for a simple script for this...appreciated as always

Why you need to create special thread for very small change in requirement.
You can add those requirements to the same thread. :slight_smile:

Ok sorry pamu.....please help me

Don Cragun already answered your question..:slight_smile:
Please check post 2.

thanks all.....its done as I wanted!!Cheers