reformat string using sed
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}
regex sed awk
add a comment |
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}
regex sed awk
1
But what's your question?
– Arkadiusz Drabczyk
Dec 29 '18 at 12:58
What have you been trying to do and where have you been stuck?
– Cyrus
Dec 29 '18 at 13:56
add a comment |
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}
regex sed awk
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}
regex sed awk
regex sed awk
asked Dec 29 '18 at 12:49
djjeffdjjeff
61
61
1
But what's your question?
– Arkadiusz Drabczyk
Dec 29 '18 at 12:58
What have you been trying to do and where have you been stuck?
– Cyrus
Dec 29 '18 at 13:56
add a comment |
1
But what's your question?
– Arkadiusz Drabczyk
Dec 29 '18 at 12:58
What have you been trying to do and where have you been stuck?
– Cyrus
Dec 29 '18 at 13:56
1
1
But what's your question?
– Arkadiusz Drabczyk
Dec 29 '18 at 12:58
But what's your question?
– Arkadiusz Drabczyk
Dec 29 '18 at 12:58
What have you been trying to do and where have you been stuck?
– Cyrus
Dec 29 '18 at 13:56
What have you been trying to do and where have you been stuck?
– Cyrus
Dec 29 '18 at 13:56
add a comment |
1 Answer
1
active
oldest
votes
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
this worked perfectly thanks +1 rep
– djjeff
Dec 30 '18 at 7:55
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1388727%2freformat-string-using-sed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
this worked perfectly thanks +1 rep
– djjeff
Dec 30 '18 at 7:55
add a comment |
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
this worked perfectly thanks +1 rep
– djjeff
Dec 30 '18 at 7:55
add a comment |
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
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
answered Dec 29 '18 at 15:17
TotoToto
3,65391226
3,65391226
this worked perfectly thanks +1 rep
– djjeff
Dec 30 '18 at 7:55
add a comment |
this worked perfectly thanks +1 rep
– djjeff
Dec 30 '18 at 7:55
this worked perfectly thanks +1 rep
– djjeff
Dec 30 '18 at 7:55
this worked perfectly thanks +1 rep
– djjeff
Dec 30 '18 at 7:55
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1388727%2freformat-string-using-sed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
But what's your question?
– Arkadiusz Drabczyk
Dec 29 '18 at 12:58
What have you been trying to do and where have you been stuck?
– Cyrus
Dec 29 '18 at 13:56