Find part of String and replace entire string in a file
I have a file named A.xml which contains a strings such as "Ticket_Release1" and "Ticket_V2".
I want to find and replace all the strings in the file starting with Ticket_ with something called Ticket_Final.
Example, replace "Ticket_Release1" and "Ticket_V2" with "Ticket_Final" by searching for string Ticket_
Can you please help me here.
sed find string
bumped to the homepage by Community♦ 17 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I have a file named A.xml which contains a strings such as "Ticket_Release1" and "Ticket_V2".
I want to find and replace all the strings in the file starting with Ticket_ with something called Ticket_Final.
Example, replace "Ticket_Release1" and "Ticket_V2" with "Ticket_Final" by searching for string Ticket_
Can you please help me here.
sed find string
bumped to the homepage by Community♦ 17 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
3
Please edit your question to include sample input and output data. Do these strings occur on new lines, are there other delimeters? etc...
– jasonwryan
Jul 28 '15 at 6:47
1
How do you define a "string"? Is "Ticket 1" one string or two? Are the quotes ("
) part of the string? Should we assume that a string is anything starting withTicket_
until the next whitespace character? How aboutfooTicket_1
? Should that be counted? Please edit your question and clarify.
– terdon♦
Jul 28 '15 at 7:20
add a comment |
I have a file named A.xml which contains a strings such as "Ticket_Release1" and "Ticket_V2".
I want to find and replace all the strings in the file starting with Ticket_ with something called Ticket_Final.
Example, replace "Ticket_Release1" and "Ticket_V2" with "Ticket_Final" by searching for string Ticket_
Can you please help me here.
sed find string
I have a file named A.xml which contains a strings such as "Ticket_Release1" and "Ticket_V2".
I want to find and replace all the strings in the file starting with Ticket_ with something called Ticket_Final.
Example, replace "Ticket_Release1" and "Ticket_V2" with "Ticket_Final" by searching for string Ticket_
Can you please help me here.
sed find string
sed find string
asked Jul 28 '15 at 6:43
PraxPrax
11
11
bumped to the homepage by Community♦ 17 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 17 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
3
Please edit your question to include sample input and output data. Do these strings occur on new lines, are there other delimeters? etc...
– jasonwryan
Jul 28 '15 at 6:47
1
How do you define a "string"? Is "Ticket 1" one string or two? Are the quotes ("
) part of the string? Should we assume that a string is anything starting withTicket_
until the next whitespace character? How aboutfooTicket_1
? Should that be counted? Please edit your question and clarify.
– terdon♦
Jul 28 '15 at 7:20
add a comment |
3
Please edit your question to include sample input and output data. Do these strings occur on new lines, are there other delimeters? etc...
– jasonwryan
Jul 28 '15 at 6:47
1
How do you define a "string"? Is "Ticket 1" one string or two? Are the quotes ("
) part of the string? Should we assume that a string is anything starting withTicket_
until the next whitespace character? How aboutfooTicket_1
? Should that be counted? Please edit your question and clarify.
– terdon♦
Jul 28 '15 at 7:20
3
3
Please edit your question to include sample input and output data. Do these strings occur on new lines, are there other delimeters? etc...
– jasonwryan
Jul 28 '15 at 6:47
Please edit your question to include sample input and output data. Do these strings occur on new lines, are there other delimeters? etc...
– jasonwryan
Jul 28 '15 at 6:47
1
1
How do you define a "string"? Is "Ticket 1" one string or two? Are the quotes (
"
) part of the string? Should we assume that a string is anything starting with Ticket_
until the next whitespace character? How about fooTicket_1
? Should that be counted? Please edit your question and clarify.– terdon♦
Jul 28 '15 at 7:20
How do you define a "string"? Is "Ticket 1" one string or two? Are the quotes (
"
) part of the string? Should we assume that a string is anything starting with Ticket_
until the next whitespace character? How about fooTicket_1
? Should that be counted? Please edit your question and clarify.– terdon♦
Jul 28 '15 at 7:20
add a comment |
1 Answer
1
active
oldest
votes
You could use sed
:
sed -i.BACKUP 's/"Ticket_.*"/"Ticket_Final"/g' <xml_file>
Explanation:
-i.BACKUP
: It will substitute on the same file, but will keep a backup called xml_file.BACKUP, just in case it doesn't work as desired.
s/ORIGINAL_REGEXP/SUBSTITUTION/g
: Substitutes (s
) the ORIGINAL_REGEXP for SUBSTITUTION, for every occurence (g
).
"Ticket_.*"
: Anything like Ticket_... between quotation marks.
2
"Ticket_.*"
will match the longest possible string between tso quotes, better to use"Ticket_[^"]*
.
– 123
Jul 28 '15 at 7:17
You are absolutely right, it will get every string till the final"
it finds. But I think it is a must to still check for the first"
it encounters, like"Ticket_[^"]*"
. But again it will accept anything just like"Ticket_"
which I don't know if it is a requirement.
– Isaac
Jul 28 '15 at 7:27
Yourg
lobal flag is not useful here.
– mikeserv
Jul 28 '15 at 7:40
Would you mind explaining why? As far as I have used sed, which is very basic, thatg
is there just in case there are several "Ticket_..." on the same line, otherwise just the first occurrence will be substituted.
– Isaac
Jul 28 '15 at 7:43
well, if there are several"Ticket_.*"
matches on the same line,.*
eats (at least) from the first through the last, and 1 is the most you can get out of it anyway.
– mikeserv
Jul 28 '15 at 7:48
|
show 2 more comments
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f218764%2ffind-part-of-string-and-replace-entire-string-in-a-file%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
You could use sed
:
sed -i.BACKUP 's/"Ticket_.*"/"Ticket_Final"/g' <xml_file>
Explanation:
-i.BACKUP
: It will substitute on the same file, but will keep a backup called xml_file.BACKUP, just in case it doesn't work as desired.
s/ORIGINAL_REGEXP/SUBSTITUTION/g
: Substitutes (s
) the ORIGINAL_REGEXP for SUBSTITUTION, for every occurence (g
).
"Ticket_.*"
: Anything like Ticket_... between quotation marks.
2
"Ticket_.*"
will match the longest possible string between tso quotes, better to use"Ticket_[^"]*
.
– 123
Jul 28 '15 at 7:17
You are absolutely right, it will get every string till the final"
it finds. But I think it is a must to still check for the first"
it encounters, like"Ticket_[^"]*"
. But again it will accept anything just like"Ticket_"
which I don't know if it is a requirement.
– Isaac
Jul 28 '15 at 7:27
Yourg
lobal flag is not useful here.
– mikeserv
Jul 28 '15 at 7:40
Would you mind explaining why? As far as I have used sed, which is very basic, thatg
is there just in case there are several "Ticket_..." on the same line, otherwise just the first occurrence will be substituted.
– Isaac
Jul 28 '15 at 7:43
well, if there are several"Ticket_.*"
matches on the same line,.*
eats (at least) from the first through the last, and 1 is the most you can get out of it anyway.
– mikeserv
Jul 28 '15 at 7:48
|
show 2 more comments
You could use sed
:
sed -i.BACKUP 's/"Ticket_.*"/"Ticket_Final"/g' <xml_file>
Explanation:
-i.BACKUP
: It will substitute on the same file, but will keep a backup called xml_file.BACKUP, just in case it doesn't work as desired.
s/ORIGINAL_REGEXP/SUBSTITUTION/g
: Substitutes (s
) the ORIGINAL_REGEXP for SUBSTITUTION, for every occurence (g
).
"Ticket_.*"
: Anything like Ticket_... between quotation marks.
2
"Ticket_.*"
will match the longest possible string between tso quotes, better to use"Ticket_[^"]*
.
– 123
Jul 28 '15 at 7:17
You are absolutely right, it will get every string till the final"
it finds. But I think it is a must to still check for the first"
it encounters, like"Ticket_[^"]*"
. But again it will accept anything just like"Ticket_"
which I don't know if it is a requirement.
– Isaac
Jul 28 '15 at 7:27
Yourg
lobal flag is not useful here.
– mikeserv
Jul 28 '15 at 7:40
Would you mind explaining why? As far as I have used sed, which is very basic, thatg
is there just in case there are several "Ticket_..." on the same line, otherwise just the first occurrence will be substituted.
– Isaac
Jul 28 '15 at 7:43
well, if there are several"Ticket_.*"
matches on the same line,.*
eats (at least) from the first through the last, and 1 is the most you can get out of it anyway.
– mikeserv
Jul 28 '15 at 7:48
|
show 2 more comments
You could use sed
:
sed -i.BACKUP 's/"Ticket_.*"/"Ticket_Final"/g' <xml_file>
Explanation:
-i.BACKUP
: It will substitute on the same file, but will keep a backup called xml_file.BACKUP, just in case it doesn't work as desired.
s/ORIGINAL_REGEXP/SUBSTITUTION/g
: Substitutes (s
) the ORIGINAL_REGEXP for SUBSTITUTION, for every occurence (g
).
"Ticket_.*"
: Anything like Ticket_... between quotation marks.
You could use sed
:
sed -i.BACKUP 's/"Ticket_.*"/"Ticket_Final"/g' <xml_file>
Explanation:
-i.BACKUP
: It will substitute on the same file, but will keep a backup called xml_file.BACKUP, just in case it doesn't work as desired.
s/ORIGINAL_REGEXP/SUBSTITUTION/g
: Substitutes (s
) the ORIGINAL_REGEXP for SUBSTITUTION, for every occurence (g
).
"Ticket_.*"
: Anything like Ticket_... between quotation marks.
answered Jul 28 '15 at 7:06
IsaacIsaac
39214
39214
2
"Ticket_.*"
will match the longest possible string between tso quotes, better to use"Ticket_[^"]*
.
– 123
Jul 28 '15 at 7:17
You are absolutely right, it will get every string till the final"
it finds. But I think it is a must to still check for the first"
it encounters, like"Ticket_[^"]*"
. But again it will accept anything just like"Ticket_"
which I don't know if it is a requirement.
– Isaac
Jul 28 '15 at 7:27
Yourg
lobal flag is not useful here.
– mikeserv
Jul 28 '15 at 7:40
Would you mind explaining why? As far as I have used sed, which is very basic, thatg
is there just in case there are several "Ticket_..." on the same line, otherwise just the first occurrence will be substituted.
– Isaac
Jul 28 '15 at 7:43
well, if there are several"Ticket_.*"
matches on the same line,.*
eats (at least) from the first through the last, and 1 is the most you can get out of it anyway.
– mikeserv
Jul 28 '15 at 7:48
|
show 2 more comments
2
"Ticket_.*"
will match the longest possible string between tso quotes, better to use"Ticket_[^"]*
.
– 123
Jul 28 '15 at 7:17
You are absolutely right, it will get every string till the final"
it finds. But I think it is a must to still check for the first"
it encounters, like"Ticket_[^"]*"
. But again it will accept anything just like"Ticket_"
which I don't know if it is a requirement.
– Isaac
Jul 28 '15 at 7:27
Yourg
lobal flag is not useful here.
– mikeserv
Jul 28 '15 at 7:40
Would you mind explaining why? As far as I have used sed, which is very basic, thatg
is there just in case there are several "Ticket_..." on the same line, otherwise just the first occurrence will be substituted.
– Isaac
Jul 28 '15 at 7:43
well, if there are several"Ticket_.*"
matches on the same line,.*
eats (at least) from the first through the last, and 1 is the most you can get out of it anyway.
– mikeserv
Jul 28 '15 at 7:48
2
2
"Ticket_.*"
will match the longest possible string between tso quotes, better to use "Ticket_[^"]*
.– 123
Jul 28 '15 at 7:17
"Ticket_.*"
will match the longest possible string between tso quotes, better to use "Ticket_[^"]*
.– 123
Jul 28 '15 at 7:17
You are absolutely right, it will get every string till the final
"
it finds. But I think it is a must to still check for the first "
it encounters, like "Ticket_[^"]*"
. But again it will accept anything just like "Ticket_"
which I don't know if it is a requirement.– Isaac
Jul 28 '15 at 7:27
You are absolutely right, it will get every string till the final
"
it finds. But I think it is a must to still check for the first "
it encounters, like "Ticket_[^"]*"
. But again it will accept anything just like "Ticket_"
which I don't know if it is a requirement.– Isaac
Jul 28 '15 at 7:27
Your
g
lobal flag is not useful here.– mikeserv
Jul 28 '15 at 7:40
Your
g
lobal flag is not useful here.– mikeserv
Jul 28 '15 at 7:40
Would you mind explaining why? As far as I have used sed, which is very basic, that
g
is there just in case there are several "Ticket_..." on the same line, otherwise just the first occurrence will be substituted.– Isaac
Jul 28 '15 at 7:43
Would you mind explaining why? As far as I have used sed, which is very basic, that
g
is there just in case there are several "Ticket_..." on the same line, otherwise just the first occurrence will be substituted.– Isaac
Jul 28 '15 at 7:43
well, if there are several
"Ticket_.*"
matches on the same line, .*
eats (at least) from the first through the last, and 1 is the most you can get out of it anyway.– mikeserv
Jul 28 '15 at 7:48
well, if there are several
"Ticket_.*"
matches on the same line, .*
eats (at least) from the first through the last, and 1 is the most you can get out of it anyway.– mikeserv
Jul 28 '15 at 7:48
|
show 2 more comments
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f218764%2ffind-part-of-string-and-replace-entire-string-in-a-file%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
3
Please edit your question to include sample input and output data. Do these strings occur on new lines, are there other delimeters? etc...
– jasonwryan
Jul 28 '15 at 6:47
1
How do you define a "string"? Is "Ticket 1" one string or two? Are the quotes (
"
) part of the string? Should we assume that a string is anything starting withTicket_
until the next whitespace character? How aboutfooTicket_1
? Should that be counted? Please edit your question and clarify.– terdon♦
Jul 28 '15 at 7:20