How to Replace a string by another in a text file
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I would like to replace a given string in a text file into by another, but with one extra detail.
Example:
If the file content's is :
Library Hello1
===============
any text here
version: 0.1
--------------
Library Hello2
===============
any text here
version:0.1
--------------
Library Hello3
===============
any text here
version: 0.2
--------------
I would like to grep
all lines containing the word Library and replace all "Library" by the other word, eg. "myStr". However this should only be done for those which version is, for example, 0.1. All the others should be ignored.
Please notice that the search/replace should be done inside each block of code. The blocks are delimited by ===== and -----.
Note: I can do search and replace, but I don't know how to simultaneously search for the version inside a block.
files string search replace
add a comment |Â
up vote
1
down vote
favorite
I would like to replace a given string in a text file into by another, but with one extra detail.
Example:
If the file content's is :
Library Hello1
===============
any text here
version: 0.1
--------------
Library Hello2
===============
any text here
version:0.1
--------------
Library Hello3
===============
any text here
version: 0.2
--------------
I would like to grep
all lines containing the word Library and replace all "Library" by the other word, eg. "myStr". However this should only be done for those which version is, for example, 0.1. All the others should be ignored.
Please notice that the search/replace should be done inside each block of code. The blocks are delimited by ===== and -----.
Note: I can do search and replace, but I don't know how to simultaneously search for the version inside a block.
files string search replace
Is âÂÂany text hereâ always exactly one line? In this case I suggest that you useawk
to read all parts of a section into three variables (for Library, any text, and version) and output the, possible modified, section after readingversion
or after reading--------------
.
â Renardo
Jun 21 at 10:08
Pedro, it appears you've suggested an edit with a new (separate) account; please register and use the first one so that you can edit your own posts.
â Jeff Schaller
Jun 21 at 10:55
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I would like to replace a given string in a text file into by another, but with one extra detail.
Example:
If the file content's is :
Library Hello1
===============
any text here
version: 0.1
--------------
Library Hello2
===============
any text here
version:0.1
--------------
Library Hello3
===============
any text here
version: 0.2
--------------
I would like to grep
all lines containing the word Library and replace all "Library" by the other word, eg. "myStr". However this should only be done for those which version is, for example, 0.1. All the others should be ignored.
Please notice that the search/replace should be done inside each block of code. The blocks are delimited by ===== and -----.
Note: I can do search and replace, but I don't know how to simultaneously search for the version inside a block.
files string search replace
I would like to replace a given string in a text file into by another, but with one extra detail.
Example:
If the file content's is :
Library Hello1
===============
any text here
version: 0.1
--------------
Library Hello2
===============
any text here
version:0.1
--------------
Library Hello3
===============
any text here
version: 0.2
--------------
I would like to grep
all lines containing the word Library and replace all "Library" by the other word, eg. "myStr". However this should only be done for those which version is, for example, 0.1. All the others should be ignored.
Please notice that the search/replace should be done inside each block of code. The blocks are delimited by ===== and -----.
Note: I can do search and replace, but I don't know how to simultaneously search for the version inside a block.
files string search replace
edited Jun 21 at 13:27
Pedro Cardoso
32
32
asked Jun 21 at 9:44
Pedro
61
61
Is âÂÂany text hereâ always exactly one line? In this case I suggest that you useawk
to read all parts of a section into three variables (for Library, any text, and version) and output the, possible modified, section after readingversion
or after reading--------------
.
â Renardo
Jun 21 at 10:08
Pedro, it appears you've suggested an edit with a new (separate) account; please register and use the first one so that you can edit your own posts.
â Jeff Schaller
Jun 21 at 10:55
add a comment |Â
Is âÂÂany text hereâ always exactly one line? In this case I suggest that you useawk
to read all parts of a section into three variables (for Library, any text, and version) and output the, possible modified, section after readingversion
or after reading--------------
.
â Renardo
Jun 21 at 10:08
Pedro, it appears you've suggested an edit with a new (separate) account; please register and use the first one so that you can edit your own posts.
â Jeff Schaller
Jun 21 at 10:55
Is âÂÂany text hereâ always exactly one line? In this case I suggest that you use
awk
to read all parts of a section into three variables (for Library, any text, and version) and output the, possible modified, section after reading version
or after reading --------------
.â Renardo
Jun 21 at 10:08
Is âÂÂany text hereâ always exactly one line? In this case I suggest that you use
awk
to read all parts of a section into three variables (for Library, any text, and version) and output the, possible modified, section after reading version
or after reading --------------
.â Renardo
Jun 21 at 10:08
Pedro, it appears you've suggested an edit with a new (separate) account; please register and use the first one so that you can edit your own posts.
â Jeff Schaller
Jun 21 at 10:55
Pedro, it appears you've suggested an edit with a new (separate) account; please register and use the first one so that you can edit your own posts.
â Jeff Schaller
Jun 21 at 10:55
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
You can try this sed
sed '/^Library/!b;:A;N;/version/!bA;/0.1$/s/^Library/myStr/' infile
Look first for Library.
If find get all the following line until version.
When find look for 0.1 and make change if it's ok.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You can try this sed
sed '/^Library/!b;:A;N;/version/!bA;/0.1$/s/^Library/myStr/' infile
Look first for Library.
If find get all the following line until version.
When find look for 0.1 and make change if it's ok.
add a comment |Â
up vote
0
down vote
You can try this sed
sed '/^Library/!b;:A;N;/version/!bA;/0.1$/s/^Library/myStr/' infile
Look first for Library.
If find get all the following line until version.
When find look for 0.1 and make change if it's ok.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can try this sed
sed '/^Library/!b;:A;N;/version/!bA;/0.1$/s/^Library/myStr/' infile
Look first for Library.
If find get all the following line until version.
When find look for 0.1 and make change if it's ok.
You can try this sed
sed '/^Library/!b;:A;N;/version/!bA;/0.1$/s/^Library/myStr/' infile
Look first for Library.
If find get all the following line until version.
When find look for 0.1 and make change if it's ok.
answered Jun 21 at 11:29
ctac_
996116
996116
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451057%2fhow-to-replace-a-string-by-another-in-a-text-file%23new-answer', 'question_page');
);
Post as a guest
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
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
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
Is âÂÂany text hereâ always exactly one line? In this case I suggest that you use
awk
to read all parts of a section into three variables (for Library, any text, and version) and output the, possible modified, section after readingversion
or after reading--------------
.â Renardo
Jun 21 at 10:08
Pedro, it appears you've suggested an edit with a new (separate) account; please register and use the first one so that you can edit your own posts.
â Jeff Schaller
Jun 21 at 10:55