How to introduce the missing number sequentially?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to introduce the missing number sequentially?
# 1  
Old 11-09-2013
How to introduce the missing number sequentially?

Dear Help,
I have an input file which looks like below

Code:
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...

Code:
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

Last edited by Don Cragun; 11-09-2013 at 12:24 PM.. Reason: Fix CODE tags.
# 2  
Old 11-09-2013
Try:
Code:
nawk '$1!=NR{$0=sprintf ("%03d 0 0 0\n%s", NR, $0);NR++}1' input

# 3  
Old 11-09-2013
Thanks Bartus. But my machine is not understanding nawk. It does understand gawk and awk
Thanks
# 4  
Old 11-09-2013
Try using that code with awk or gawk then.
# 5  
Old 11-09-2013
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:
Code:
002 1000 2000 3000
003 2000 3000 4000
005 1000 2000 6000
008 1234 5678 9000
015 4 3 2 1

it will produce:
Code:
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:
Code:
awk '
BEGIN { nl = 1
}
{       while(nl < $1)
                printf("%03d 0 0 0\n", nl++)
        print
        nl++
}' input

produces:
Code:
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.
# 6  
Old 11-10-2013
Like this also it works, you may try

Code:
$ awk '{while(++x<$1)printf("%03d 0 0 0\n", x)}1' file

Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To check the missing file based on sequence number.

Hi All, I have a requirement that i need to list only the missing sequences with a unix script. For Example: Input: FILE_001.txt FILE_002.txt FILE_005.txt FILE_006.txt FILE_008.txt FILE_009.txt FILE_010.txt FILE_014.txt Output: FILE_003.txt FILE_004.txt FILE_007.txt FILE_011.txt... (5 Replies)
Discussion started by: Arun1992
5 Replies

2. Shell Programming and Scripting

Incorrect number of command line arguments and missing required files

I am developing a script. This script takes in one parameter which is the name of a file whose content is a list of names of some files. The script can check whether those files exist in current directory. Here is my question: If the number of provided parameters is less than one or one of the... (2 Replies)
Discussion started by: Ray Sun
2 Replies

3. Shell Programming and Scripting

How to take the missing sequence Number?

Am using unix aix KSH... I have the files called MMRR0106.DAT MMRR0206.DAT MMRR0406.DAT MMRR0506.DAT MMRR0806.DAT .... ... MMRR3006.DAT MMRR0207.DAT These files are in one dircetory /venky ? I want the output like this ? Missing files are : MMRR0306.DAT MMRR0606.DAT... (7 Replies)
Discussion started by: Venkatesh1
7 Replies

4. Shell Programming and Scripting

Script to output a line missing a number

Ok, Lets see if I can explain this We have a script that pulls information from multiple files and outputs it, however I only need 2 Columns (of 11) from it right now I run the script like this: tkxtrn | awk '{print $5" "" "$9}' This gives me column 5 and 9, the only two I care for ... (5 Replies)
Discussion started by: shadowkraze
5 Replies

5. Shell Programming and Scripting

Perl : print the sequence number without missing number

Dear Perl users, I need your help to solve my problem below. I want to print the sequence number without missing number within the range. E.g. my sequence number : 1 2 3 4 5 6 7 8 11 12 13 14 my desired output: 1 -8 , 11-14 my code below but still problem with the result: 1 - 14 1 -... (2 Replies)
Discussion started by: mandai
2 Replies

6. Shell Programming and Scripting

How do i find the first number in each line and insert dummy string into the missing columns?

Hi, I have one input file with the following content: MY_inpfile.txt Aname1 Cname1 Cname2 1808 5 Aname2 Cname1 1802 47 Bname1 ? 1819 22 Bname2 Cname1 1784 11 Bname3 1817 9 Zname1 Cname1 1805 59 Zname2 Cname1 Cname2 Cname3 1797 27 Every line in my input file have a 4 digit... (5 Replies)
Discussion started by: Szaffy
5 Replies

7. Shell Programming and Scripting

join based on line number when one file is missing lines

I have a file that contains 87 lines, each with a set of coordinates (x & y). This file looks like: 1 200.3 -0.3 2 201.7 -0.32 ... 87 200.2 -0.314 I have another file which contains data that was taken at certain of these 87 positions. i.e.: 37 125 42 175 86 142 where the first... (1 Reply)
Discussion started by: jackiev
1 Replies

8. UNIX for Dummies Questions & Answers

To find missing numbers from a number series

Hi, My requirement is I have an input file with a continuous series from 10000 to 99999. I have some numbers missing from those series. I want a output file which produces those missing numbers. Eg: 10002, 99999 are missing from the series then the output file should contain those... (4 Replies)
Discussion started by: rakeshbharadwaj
4 Replies

9. Shell Programming and Scripting

Finding missing files that are named sequentially with Perl?

Hello I am new to Perl, in fact I am on chapter one of the book. :) However I am in need of a Perl Script faster than I can finish the book. Perhaps someone can help me with my immediate need while I read my book. I have a directory with hundreds of files that are all named like... (4 Replies)
Discussion started by: newftronics
4 Replies
Login or Register to Ask a Question