LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-07-2018, 01:02 PM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Wished-for awk function


awk has a built-in function called substring.

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?

Daniel B. Martin

.
 
Old 02-07-2018, 01:52 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
Quote:
Originally Posted by danielbmartin View Post
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.
 
Old 02-07-2018, 07:55 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,145

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
Quote:
Originally Posted by danielbmartin View Post
I wish awk had a similar function called subword.
So write it - and include it in any other code that requires it.

As for getting it into the upstream, the documentation has a section on this very subject.
 
Old 02-07-2018, 09:48 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
Quote:
Originally Posted by syg00 View Post
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.
 
Old 02-07-2018, 09:55 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,145

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
Not the manpage, the users guide from the gnu.org website.
 
Old 02-07-2018, 11:35 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
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
 
  


Reply

Tags
awk



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
the MATCH function in awk alexandnpu Linux - Software 2 07-11-2011 10:04 AM
[SOLVED] Awk sub() function webhope Programming 43 04-22-2010 02:28 AM
awk , I need help for awk, just a display function mcandy General 1 12-15-2008 12:21 PM
Need help with awk function philosophia Programming 1 02-20-2007 05:53 PM
To those Americans who wished Bush was out!!! d00bid00b General 3 11-01-2005 03:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration