LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell script to convert values on successive lines into consecutive ranges? (https://www.linuxquestions.org/questions/programming-9/shell-script-to-convert-values-on-successive-lines-into-consecutive-ranges-818535/)

kmkocot 07-07-2010 11:54 AM

Shell script to convert values on successive lines into consecutive ranges?
 
Hi all,

I have a series of input files formatted like this:
Code:

RTREVF, KOG3266 = 111
RTREVF, KOG3294 = 130
RTREVF, KOG3295 = 177
WAGF, KOG3307 = 107
JTTF, KOG3320 = 174

Each line represents a portion of a data matrix. I want to convert the numbers after the "=" to the range of that partition in the matrix such that the output file looks like this:
Code:

RTREVF, KOG3266 = 1-111
RTREVF, KOG3294 = 112-241
RTREVF, KOG3295 = 242-418
WAGF, KOG3307 = 419-525
JTTF, KOG3320 = 526-699

Can anyone think of an easy way to do this?

Thanks,
Kevin

ntubski 07-07-2010 12:21 PM

Code:

awk -vn=0 -F= '{printf("%s= %d-%d\n", $1, n+1, n+=$2);}' < input-file > output-file

grail 07-07-2010 08:39 PM

You can just use the print in this case too:
Code:

awk -F= '{print $1"+ "(n+1)"-"(n+=$2)}' infile
Much of a muchness I guess :)

kmkocot 07-08-2010 06:11 PM

Wow! Thanks guys! Both work great!

syg00 07-08-2010 06:38 PM

No concerns if the first record is "1" (or worse, "0") ?.
Corner cases are always a potential problem.

kmkocot 07-09-2010 10:59 AM

That's a good thought that I hadn't considered but a previous step in the pipeline throws out any sequences that are less than 25 characters long.

Thanks!
Kevin


All times are GMT -5. The time now is 05:47 AM.