in reply to Re: process multiline text and print in desired format
in thread process multiline text and print in desired format
"... removes all the leading and trailing space ... avoid the "^\s*" repetition ..."
Your posted data had no trailing spaces. In my example code, I did remove all leading spaces for this very reason: "... map /^\s*(.+)$/ ...". Subsequent regexes looked like "/^color ..."; not "/^\s*color ...".
Removal of potential trailing spaces may well be a good idea. I don't know the source of your input, but trailing spaces are often impossible to spot by inspection; e.g.
$ cat > fred abc def $ cat fred abc def $ cat -vet fred abc$ def $
If you want to do this, you can modify my regex; however, be aware of a subtle gotcha. You cannot simply tag another \s* on the end; you'll also need to change .+ to .+?. Compare these examples:
$ perl -E 'my $x = " xyz "; say "|$_|" for map /^\s*(.+)$/, $x' |xyz | $ perl -E 'my $x = " xyz "; say "|$_|" for map /^\s*(.+)\s*$/, $x' |xyz | $ perl -E 'my $x = " xyz "; say "|$_|" for map /^\s*(.+?)\s*$/, $x' |xyz|
— Ken
|
|---|