Thanks.  Did you try it at all?  I tried this:

$ seq 10000 | print_ranges.awk - "1-5 55 27"
1
2
3
4
5


Seeing that it ignored the 55 and 27, I tried this:

$ seq 10000 | print_ranges.awk - "1-5 55-55 27-27"
1
2
3
4
5


55


27


Pretty close, but it's sending out an extra pair of newlines for every 
space in the format string.  It does seem to be super fast, though, 
probably faster than the perl scripts I'm testing, which is great.

It would be better if the format string used commas instead of spaces and 
if we could use single numbers in addition to ranges (e.g., 1-5,55,27). 
It also would be best for the format string to precede the filename, as in 
cut, and to read from stdin when no filename is given.

Another issue is whether it should put out lines in the specified order or 
in the order from the original file.  cut uses the latter method for 
fields, so 4-5,1-3 does the same thing as 1-5.  Being able to reorder the 
lines is actually a great feature, and it would be nice if cut could do 
that, too, but it really does only *cut* columns out of a file -- it will 
not reorder columns.  I do use awk for that, but I don't know a good way 
to specify the column order without typing all of them out, which is way 
too much work in many cases.

Mike




On Sun, 2 Jun 2013, zedlan at invec.net wrote:

> Here's my script to try:
>
>
> #!/usr/bin/gawk -f
> # print_ranges.awk
> # usage: 2nd arg is string enclosed by quotes, like "to-from1 to-from2 ... "
> # ex: print_ranges file "92-97 5-8 23-42 55-71"
>
>
> BEGIN {
> range_cnt = split(ARGV[2], ranges, " ");
>
>
> while(getline < ARGV[1]) {
> line_arr[++n] = $0;
> } close(ARGV[1])
>
> for(i = 1; i <= range_cnt; i++) {
> split(ranges[i], start_stop, "-");
>
>
> start = start_stop[1]
> stop = start_stop[2]
>
>
> for(j = start; j <= stop; j++) {
> print line_arr[j];
> }
> print "\n"
> }
> }