esham
March 21, 2017, 7:14am
1
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
RudiC
March 21, 2017, 8:41am
4
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
esham
March 21, 2017, 1:47pm
6
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'
RudiC
March 21, 2017, 1:52pm
7
There's a single quote missing in your output. And, why a pipe with two sed
invocations?
RudiC
March 21, 2017, 1:57pm
8
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'
esham
March 21, 2017, 1:57pm
9
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'
RudiC
March 21, 2017, 2:00pm
10
PLEASE DON'T modify posts if people have already referred / answered to it, pulling the rug from under therir feet!
esham
March 21, 2017, 2:01pm
11
Sure. After long time, getting back to forums.. Will follow the rules.