ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
substr(s, a, b)
returns b number of chars from string s, starting at position a.
The parameter b is optional. If b is omitted it defaults to infinity.
I wish awk had a similar function called subword.
subword(s, a, b, d)
returns b number of words from string s, starting at word a.
The parameter b is optional. If b is omitted it defaults to infinity.
The parameter d is a string of characters, all of which are delimiters
which define word boundaries.
If d is omitted it defaults to a space (i.e. blank or tab).
Examples of subword ...
s="now is the time forQallZgood men"
subword(s, 2, 3) returns "is the time"
subword(s, 4, 1) returns "time"
subword(s, 4) returns ""time forQallZgood men"
subword(s, 4, 2) returns "time forQallZgood"
subword(s, 4, 2, " Z") returns "time forQall"
What is the mechanism for suggesting this enhancement to awk?
Does subword already exist in an extension package?
What is the mechanism for suggesting this enhancement to awk?
From what I've seen of bugfixes and much smaller tweaks, the method would be to write a patch with the desired functionality and then convince the various awk- amd awk-using projects that the coding style and code quality of the patch is as desirable as the change itself.
As for getting it into the upstream, the documentation has a section on this very subject.
Which version? mawk and gawk don't seem to have anything about extending the specification in their manual pages.
danielbmartin, on a more practical note, if you are running into case like this more than once, or perhaps even once, then you might consider escalating to perl 5. awk is good at a lot of things, but perl 5 takes string manipulation much, much further.
How is subword any better than setting FS and performing a basic loop to return your desired items? Or if on a string, just use split:
Code:
#!/usr/bin/awk -f
BEGIN{
s="now is the time forQallZgood men"
n = split(s,a)
# subword(s, 2, 3) returns "is the time"
for(i=2; i < 5; i++)
out = out (out?" ":"") a[i]
print out
#subword(s, 4, 1) returns "time"
out = ""
for(i=4; i < 5; i++)
out = out (out?" ":"") a[i]
print out
#subword(s, 4) returns ""time forQallZgood men"
out = ""
for(i=4; i <= n; i++)
out = out (out?" ":"") a[i]
print out
#subword(s, 4, 2) returns "time forQallZgood"
out = ""
for(i=4; i < 6; i++)
out = out (out?" ":"") a[i]
print out
#subword(s, 4, 2, " Z") returns "time forQall"
out = ""
split(s,a,"[ Z]")
for(i=4; i < 6; i++)
out = out (out?" ":"") a[i]
print out
}
Output matches that requested
Of course you can just put that in a function if that is also desired
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.