bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gawk] Gawk Enhancement Suggestion


From: Ed Morton
Subject: Re: [bug-gawk] Gawk Enhancement Suggestion
Date: Wed, 14 Dec 2011 16:22:37 -0600
User-agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0

Arnold - I'll take a look and get back to you. I expect I can code up what I have in mind (which isn't quite what's in that example) as an awk function that people could copy/paste but I was really hoping to get a new builtin string function out of this.

Thanks,

    Ed.

On 12/6/2011 2:38 PM, Aharon Robbins wrote:
Hi Ed.

Re the below. See the node "Cut Program" in the gawk manual.
Adaptation into a function should be straightforward.

A diff to redo that section as a function + code to call it will
be cheerily reviewed and most likely accepted.

Thanks,

Arnold

Date: Fri, 02 Dec 2011 07:36:50 -0600
From: Ed Morton<address@hidden>
To: address@hidden
Subject: [bug-gawk] Gawk Enhancement Suggestion

Arnold - could we get a "cut()" function for gawk similar to the UNIX
one of the same name that just lets people select specific fields or
ranges of fields?  Below is some details (copied from something I posted
at comp.lang.awk).

Regards,

      Ed.

A function that returns a string of selected fields is very often all we need. 
e.g.:

print cut($0,3)       # print from the 3rd field to the end of the record using
FS as separator
print cut($0,3,",")   # print from the 3rd field to the end of the record using
"," as separator
print cut($0,"3-7")   # print from the 3rd field to the 7th field
print cut($0,"3,5,7") # print the 3rd, 5th, and 7th fields

The separating substring between the fields would just be whatever separator
preceeded the trailing fields. So, with this input (with leading blanks):

      a b    c  d

the following code snippets would produce the output that follows them:

print cut($0,"1,3")
a    c

print cut($0,"1,3",/[[:blank:]]+/)
   b

print cut($0,"1-3")
a b    c

print cut($0,"2,4")
b  d

print cut($0,"1-4")
a b    c  d

Obviously $0 can be replaced by any string and cut() should take an optional 4th
arg to specify the separator in it's returned string:

print cut($0,"1,3",FS,OFS)
a c

print cut($0,"1,3",/[[:blank:]]+/,"#")
#b

print cut($0,"1-3",FS,OFS)
a b c

print cut($0,"2,4",FS,":")
b:d

print cut($0,"1-4",FS,"|")
a|b|c|d




reply via email to

[Prev in Thread] Current Thread [Next in Thread]