Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

breaking up range of numbers with awk

Status
Not open for further replies.

arunrr

Programmer
Oct 2, 2009
103
US
Hello,

Input and desired output are as follows...

Example 1 (input): 5 12 34-41 71 207
Example 1 (output): 5 12 34 35 36 37 38 39 40 41 71 207

Example 2 (input): 23 69-72 188 195 245-248
Example 2 (output): 23 69 70 71 72 188 195 245 246 247 248

Objective is read from stdin and split the range as shown above to the individual numbers with awk.

Thanks,
AR
 
Hi

For example this will produce the desired output :
Code:
awk '{for(i=1;i<=NF;i++)if($i~/-/){split($i,a,/-/);$i="";for(j=a[1];j<=a[2];j++)$i=$i ($i?FS:"")j}}1' /input/file
If your [tt]awk[/tt] implementation has trouble with the above, try this :
Code:
awk '{f=0;for(i=1;i<=NF;i++){if($i!~/-/){printf"%s%s",(f++?FS:""),$i;continue}split($i,a,/-/);for(j=a[1];j<=a[2];j++)printf"%s%s",(f++?FS:""),j}print""}' /input/file
Both tested with [tt]gawk[/tt] and [tt]mawk[/tt].


Feherke.
 
Hello Feherke,

Awesome!! Both solutions work with my awk implementation.

Thanks,
AR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top