我有一个包含以下行的文件
["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",
我希望用 sed 或 awk 对其进行编辑(保留名称和值)以使其成为......
["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}
答案1
perl 方式:
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}
解释:
\w+ # 1 or more word characters
\d+ # 1 or more digits
\h+ # 1 or more horizontal spaces
\S+ # 1 or more NON space characters