Mike,


I made a few changes to the script per your request:


#!/usr/bin/gawk -f


# print_ranges.awk
# usage: takes csv arg string to-from1,to-from2, ... 
# ex: cat file | print_ranges.awk 92-97,5-8,23-42,55-71


BEGIN {
 range_cnt = split(ARGV[1], ranges, ",");


 while(getline < "-") {
 line_arr[++n] = $0;
 } close("-")


 for(i = 1; i <= range_cnt; i++) { 
 num = split(ranges[i], start_stop, "-");

 if(num == 1) {
 start = ranges[i];
 stop = start;
 } else {
 start = start_stop[1]
 stop = start_stop[2]
 }


 for(j = start; j <= stop; j++) { 
 print line_arr[j];
 }
 }
}




-----Original Message-----
From: Mike Miller [mailto:mbmiller+l at gmail.com]
Sent: Sunday, June 2, 2013 05:14 PM
To: 'TCLUG Mailing List'
Subject: Re: [tclug-list] line cut: another missing coreutil

Thanks. Did you try it at all? I tried this:$ seq 10000 | print_ranges.awk - "1-5 55 27"12345Seeing that it ignored the 55 and 27, I tried this:$ seq 10000 | print_ranges.awk - "1-5 55-55 27-27"123455527Pretty 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.MikeOn 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"> }> }_______________________________________________TCLUG Mailing List - Minneapolis/St. Paul, Minnesotatclug-list at mn-linux.orghttp://mailman.mn-linux.org/mailman/listinfo/tclug-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.mn-linux.org/pipermail/tclug-list/attachments/20130603/862cc7da/attachment-0001.html>