Dear Help,
I have an input file which looks like below
002 1000 2000 3000
003 2000 3000 4000
005 1000 2000 6000
I would like to have an output which inserts the missing number in sequential sorting as shown below...
001 0 0 0
002 1000 2000 3000
003 2000 3000 4000
004 0 0 0
005 1000 2000 6000
I wonder how is it possible...any help is highly appreciated
Cheers
Try:
nawk '$1!=NR{$0=sprintf ("%03d 0 0 0\n%s", NR, $0);NR++}1' input
Thanks Bartus. But my machine is not understanding nawk. It does understand gawk and awk
Thanks
Try using that code with awk or gawk then.
The script bartus11 provided works perfectly with your sample input and should work reliably as long as your input file never has two or more adjacent missing lines. But, with the input:
002 1000 2000 3000
003 2000 3000 4000
005 1000 2000 6000
008 1234 5678 9000
015 4 3 2 1
it will produce:
001 0 0 0
002 1000 2000 3000
003 2000 3000 4000
004 0 0 0
005 1000 2000 6000
006 0 0 0
008 1234 5678 9000
008 0 0 0
015 4 3 2 1
If this is an issue with the input you expect to process, the script:
awk '
BEGIN { nl = 1
}
{ while(nl < $1)
printf("%03d 0 0 0\n", nl++)
print
nl++
}' input
produces:
001 0 0 0
002 1000 2000 3000
003 2000 3000 4000
004 0 0 0
005 1000 2000 6000
006 0 0 0
007 0 0 0
008 1234 5678 9000
009 0 0 0
010 0 0 0
011 0 0 0
012 0 0 0
013 0 0 0
014 0 0 0
015 4 3 2 1
If you want to try this on a Solaris/SunOS system change awk
in the above script to nawk
like bartus11 did or to /usr/xpg4/bin/awk
or /usr/xpg6/bin/awk
.
Like this also it works, you may try
$ awk '{while(++x<$1)printf("%03d 0 0 0\n", x)}1' file
001 0 0 0
002 1000 2000 3000
003 2000 3000 4000
004 0 0 0
005 1000 2000 6000
006 0 0 0
007 0 0 0
008 1234 5678 9000
009 0 0 0
010 0 0 0
011 0 0 0
012 0 0 0
013 0 0 0
014 0 0 0
015 4 3 2 1