reformat string using sed

0

I have a file containing lines

["foo"] = ".worldport 0 322.373138 -1487.853882 43.720089",
["bar"] = ".worldport 0 386.938000 212.299000 43.699400",
["baz"] = ".worldport 0 1239.120000 -286.705000 42.476400",

which I would like to be edited (keeping the name and values) with sed or awk to become...

["foo"]={["position_x"]=322.373138,["position_y"]=-1487.853882,["position_z"]=43.720089,["map"]=0}
["bar"]={["position_x"]=386.938000,["position_y"]=212.299000,["position_z"]=43.699400,["map"]=0}
["baz"]={["position_x"]=1239.120000,["position_y"]=-286.705000,["position_z"]=42.476400,["map"]=0}

djjeff

Posted 2018-12-29T12:49:10.257

Reputation: 11

1But what's your question? – Arkadiusz Drabczyk – 2018-12-29T12:58:52.453

What have you been trying to do and where have you been stuck? – Cyrus – 2018-12-29T13:56:46.477

Answers

0

A perl way:

perl -ape 's/ = "\.\w+\h+(\d+)\h+(\S+)\h+(\S+)\h+(\S+?)",/={["position_x"]=$2,["position_y"]=$3,[position_z"]=$4,["map"]=$1}/' file.txt
["foo"]={["position_x"]=322.373138,["position_y"]=-1487.853882,[position_z"]=43.720089,["map"]=0}
["bar"]={["position_x"]=386.938000,["position_y"]=212.299000,[position_z"]=43.699400,["map"]=0}
["baz"]={["position_x"]=1239.120000,["position_y"]=-286.705000,[position_z"]=42.476400,["map"]=0}

Explanation:

\w+     # 1 or more word characters
\d+     # 1 or more digits
\h+     # 1 or more horizontal spaces
\S+     # 1 or more NON space characters

Toto

Posted 2018-12-29T12:49:10.257

Reputation: 7 722

this worked perfectly thanks +1 rep – djjeff – 2018-12-30T07:55:55.670