I have input which has some fields
Here is an example input:
active=1 'oldest active'=0s disabled=0 'function call'=0
I would like to replace :
|
and _
Output would be:
active=1|'oldest_active'=0s|disabled=0|'function_call'=0
I tried different solutions with sed
or perl
found on the web but did not managed to do want I want.
$ s="active=1 'oldest active'=0s disabled=0 'function call'=0"
$ echo "$s" | perl -pe "s/'[^']*'(*SKIP)(*F)| /|/g; s/ /_/g"
active=1|'oldest_active'=0s|disabled=0|'function_call'=0
Two step replacement:
'[^']*'(*SKIP)(*F)
will skip all patterns surrounded by '
and replace the remaining spaces with |
'
will be replaced with _
Alternate solution:
$ echo "$s" | perl -pe "s/'[^']*'/$& =~ s| |_|gr/ge; s/ /|/g"
active=1|'oldest_active'=0s|disabled=0|'function_call'=0
'[^']*'/$& =~ s| |_|gr/ge
replace all spaces in matched pattern '[^']*'
using another substitute command. The e
modifier allows using command instead of string in replacement sections/ /|/g
Further reading: