2017-03-21 3 views
0

Ich habe eine Datei, die Inhalte wie folgt enthält:Platz einfache Anführungszeichen um Strings mit sed

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

ich es

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

Ich habe versucht, verschiedene sed Befehle wie

sed 's/[A-Za-z]*[0-9]*[.] 
ändern müssen

Aber sie haben nicht funktioniert ..

Antwort

0

hat es funktioniert.

sed -r "s/^|$/'/g" records.config | sed "s/\s/': '/g" 

Das sieht gut aus. hoffe, das ist der bessere Weg.

0

Mit Rückreferenzierung:

sed 's/^\(.*\) \(.*\)/'\''\1'\'': '\''\2'\''/' file 

oder mit umgebenden doppelten Anführungszeichen:

sed "s/^\(.*\) \(.*\)/'\1': '\2'/" file 
0

Try this -

awk -v OFS=":" '{print v$1v,v$2v}' v="'" f 
'proxy.config.cluster.mc_group_addr':'224.0.1.37' 
'proxy.config.log.logging_enabled':'3' 
'proxy.config.log.squid_log_enabled':'1' 
0

sed Lösung:

sed -E "s/^([^ ]+) (.+)$/'\1': '\2'/" testfile 

^([^ ]+) - eine Folge von Zeichen der Abgleichung am Anfang der Zeile mit Ausnahme Raum

Der Ausgang:

'proxy.config.cluster.mc_group_addr': '224.0.1.37' 

'proxy.config.log.logging_enabled': '3' 

'proxy.config.log.squid_log_enabled': '1' 

awk Lösung (ohne Leerzeilen) :

awk 'BEGIN{FS=" "; ORS="\n";}{ if(NF) print "\47"$1"\47: \47"$2"\47"}' testfile 

if(NF) - prüft, ob eine Zeile der nächste Ausdruck nicht leer ist
"\47" auszuführen - verweist auf ein Apostroph '

Der Ausgang:

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