sed for string manipulation

I have a file which contains contents like below

proxy.config.cluster.mc_group_addr	224.0.1.37
proxy.config.log.logging_enabled	3
proxy.config.log.squid_log_enabled	1

Need to modify to

'proxy.config.cluster.mc_group_addr': '224.0.1.37'
'proxy.config.log.logging_enabled': '3'
'proxy.config.log.squid_log_enabled': '	1'

tried various sed like

 sed 's/[A-Za-z]*[0-9]*[.]

But didnt help..

perl -npi -e "s/^(\S+) (.+)/'\1' a2/" tmp.dat

Hello esham,

Could you please try following and let me know if this helps you.

sed  's/\(.[^ ]*\)\( \)\(.*\)/'"'"'\1'"'"': '"'"'\3'"'"'/g'   Input_file

If you are happy with above code's output then change sed to sed -i .

Thanks,
R. Singh

sed -r "s/^|$/'/g; s/	/': '/" file
'proxy.config.cluster.mc_group_addr': '224.0.1.37'
'proxy.config.log.logging_enabled': '3'
'proxy.config.log.squid_log_enabled': '1'

Be aware that the second s ubstitute command has a <TAB> in its pattern...
And, Can't see how and why you have a leading space in front of the 1 but not the 3 .

1 Like

You might want to consider the following, which is built on RudiCs idea. It removes the necessity to have consistent separators between "fields". Replace "<b>" and "<t>" with literal blank and tab characters. It furthermore removes an (maybe unnecessary) ambiguity about trailing and leading blanks RudiCs solution would have tripped over:

sed 's/^[<b><t>]*/&\'/;s/[<b><t>]*$/'&/;s/[<b><t>:]*/\'&\'/g' file

I hope this helps.

bakunin

got it working..

sed -r "s/^|$/'/g" records.config  | sed "s/\s/': '/g"
'proxy.config.log.squid_log_enabled: '1'
'proxy.config.log.logfile_dir: '/tmp/logs'
'proxy.config.log.squid_log_is_ascii: '1'
'proxy.config.log.squid_log_name: 'foo'

There's a single quote missing in your output. And, why a pipe with two sed invocations?

Different approach:

sed "s/[^[:space:]]*/'&'/g; s/ /: /g" file
'proxy.config.cluster.mc_group_addr': '224.0.1.37'
'proxy.config.log.logging_enabled': '3'
'proxy.config.log.squid_log_enabled': '1'
 sed -r "s/^|$/'/g" records.config  | sed "s/\s/': '/g"
'proxy.config.exec_thread.autoconfig': '1'
'proxy.config.exec_thread.autoconfig.scale': '1.5'
'proxy.config.exec_thread.limit': '2'
'proxy.config.ssl.number.threads': '-1'
'proxy.config.accept_threads': '1'

PLEASE DON'T modify posts if people have already referred / answered to it, pulling the rug from under therir feet!

Sure. After long time, getting back to forums.. Will follow the rules.:b: