On 08.07.2017 06:29, Ian Zimmerman wrote:
> Is there a concise oneliner, in _standard_ awk, to do what strip does in
> python and chop does in perl? Not only on $0 but separately on each
> field.
What do those functions in those languages? (Strip leading/trailling
whitespace?)
>
> I can see a way with the match function with the nonstandard gawk third
> argument, but otherwise awk's matching apparatus seems to suffer
> severely from the lack of backreferences.
>
> Motivation: I have input like
>
> foo : bar
>
> foobar : baz
What is the field separator in your samples here; a colon, or colon
with surrounding blanks, or is the above just one field?
> # This can happen, too
> blah:etch
>
> and I need to generate output where whitespace is significant (so must
> be absent, in the above example).
*Significant* whitespace must be absent? - It would be helpful to show
expected output for your samples.
Generally, if you want to work on individual fields in standard awk you
iterate over the fields and use a sub() call to remove undesired data
gsub(/^ +| +$/,"",$i)
Another (a bit bulky) option is to avoid a loop and work directly on
the whole line with a constructed dynamic regexp with FS and spaces to
be replaced by your desired OFS.
Janis