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

Posted on 2007-07-24
Medium Priority
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

amit_g earned 2000 total points
ID: 19560887
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

807 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