help write a unix script to compare and print two numeric fields

Posted on 2007-07-24
Last Modified: 2012-08-14
I have a text file that contains 2 columns of numbers which are comma delimited, here is a sample
column1, column2
319290 ,319292        
319328 ,319329        
319358 ,319358
312       ,316      
319450 ,319451
I need a unix script that does the following:
1) check the length of the number in the first column if it is not equal to  6 digits put "0" instead of the missing digit like 312 in the sample above would be 312000
2) then check the length of the number in the second column number if it is not equal to 6 digits, replace the missing digit/digits with 9 so the 316 in second column would be 316999

3)compare  both the numbers in column 1 and 2 and if the number in column 2 is greater than the one in column 1  break the range and print all the numbers for example
column1, column2
319290 ,319292    
I want the script to print the whole range like this:
finally want to put output of this in another text file called routes.

Question by:Aquamarine77
    1 Comment
    LVL 58

    Accepted Solution

    awk -F, '{gsub(/ /, "");} length($1) != 6 {$1 = substr($1 "000000", 1, 6);} length($2) != 6 {$2 = substr($2 "999999", 1, 6);} $2 > $1 {while ($2 >= $1) print $1++;}' YourFileName

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
    Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now