[Top][All Lists]

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

[bug-gawk] Gawk Enhancement Suggestion

From: Ed Morton
Subject: [bug-gawk] Gawk Enhancement Suggestion
Date: Fri, 02 Dec 2011 07:36:50 -0600
User-agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0

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).



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

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:]]+/)

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:]]+/,"#")

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

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

print cut($0,"1-4",FS,"|")

reply via email to

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