How can I modify a line which contains 2nd occurence of a string?Add a string to a text file from terminalHow...
What are ways to record who took the pictures if a camera is used by multiple people?
Is it rude to ask my opponent to resign an online game when they have a lost endgame?
Divide Numbers by 0
Why do modes sound so different, although they are basically the same as a mode of another scale?
Are there consequences for not filing a DMCA (any country)
Blogging in LaTeX
The 7-numbers crossword
Why are CEOs generally fired rather being demoted?
Could these polynomials be identified?
How to encrypt the .viminfo file and still get Vim to read it?
Polarity of gas discharge tubes?
Why don't "echo -e" commands seem to produce the right output?
Why do we need explainable AI?
Turn off Google Chrome's Notification for "Flash Player will no longer be supported after December 2020."
How could reincarnation magic be limited to prevent overuse?
Tasha's Hideous Laughter used on a deaf person?
Is it good practice to speed up and slow down where not written in a song?
Displaying Time in HH:MM Format
How can I store milk for long periods of time?
D Scale Question
Was there an original and definitive use of alternate dimensions/realities in fiction?
Is there anything in the universe that cannot be compressed?
Are there balance issues when allowing attack of opportunity against any creature?
In Toy Story, are toys the only inanimate objects that become alive? And if so, why?
How can I modify a line which contains 2nd occurence of a string?
Add a string to a text file from terminalHow to add exports to fish like in .bashrc?How can I hibernate/suspend from the command line and do so at a specific timeA command to list all users? And how to add, delete, modify users?How does the system know which paths to search for executables?Comment, Uncomment, change setting based on string, add line from terminalHow do I modify paths and change to a real path?How do I find a single unique line in a file?How to use sed to modify last 3 digit of a line with random number when a string matches in a file
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
This post explains how to add a line at the beginning of a file from the terminal. But how do I modify from the terminal a line somewhere in a file if I do not know which line it is?
I should modify the line eni=10.*10**9
to eni=10.*10**8
note the exponents. It is the second time that eni
appears
command-line
|
show 1 more comment
This post explains how to add a line at the beginning of a file from the terminal. But how do I modify from the terminal a line somewhere in a file if I do not know which line it is?
I should modify the line eni=10.*10**9
to eni=10.*10**8
note the exponents. It is the second time that eni
appears
command-line
Do you mean the exact middle of a file?
– guillermo chamorro
10 hours ago
No I mean somewhere in the file
– mattiav27
10 hours ago
If not line number is there any pattern or some kind of phrase which is to be followed? Like add this line before a line which contains "blabla".
– Kulfy
10 hours ago
@Kulfy it is the second time the term 'eni' appears if that can be useful
– mattiav27
10 hours ago
3
What exactly kind of modifications do you want? Please edit your question and try to make it more descriptive and specific.
– Kulfy
10 hours ago
|
show 1 more comment
This post explains how to add a line at the beginning of a file from the terminal. But how do I modify from the terminal a line somewhere in a file if I do not know which line it is?
I should modify the line eni=10.*10**9
to eni=10.*10**8
note the exponents. It is the second time that eni
appears
command-line
This post explains how to add a line at the beginning of a file from the terminal. But how do I modify from the terminal a line somewhere in a file if I do not know which line it is?
I should modify the line eni=10.*10**9
to eni=10.*10**8
note the exponents. It is the second time that eni
appears
command-line
command-line
edited 8 hours ago
Kulfy
7,59010 gold badges31 silver badges58 bronze badges
7,59010 gold badges31 silver badges58 bronze badges
asked 10 hours ago
mattiav27mattiav27
1871 gold badge1 silver badge9 bronze badges
1871 gold badge1 silver badge9 bronze badges
Do you mean the exact middle of a file?
– guillermo chamorro
10 hours ago
No I mean somewhere in the file
– mattiav27
10 hours ago
If not line number is there any pattern or some kind of phrase which is to be followed? Like add this line before a line which contains "blabla".
– Kulfy
10 hours ago
@Kulfy it is the second time the term 'eni' appears if that can be useful
– mattiav27
10 hours ago
3
What exactly kind of modifications do you want? Please edit your question and try to make it more descriptive and specific.
– Kulfy
10 hours ago
|
show 1 more comment
Do you mean the exact middle of a file?
– guillermo chamorro
10 hours ago
No I mean somewhere in the file
– mattiav27
10 hours ago
If not line number is there any pattern or some kind of phrase which is to be followed? Like add this line before a line which contains "blabla".
– Kulfy
10 hours ago
@Kulfy it is the second time the term 'eni' appears if that can be useful
– mattiav27
10 hours ago
3
What exactly kind of modifications do you want? Please edit your question and try to make it more descriptive and specific.
– Kulfy
10 hours ago
Do you mean the exact middle of a file?
– guillermo chamorro
10 hours ago
Do you mean the exact middle of a file?
– guillermo chamorro
10 hours ago
No I mean somewhere in the file
– mattiav27
10 hours ago
No I mean somewhere in the file
– mattiav27
10 hours ago
If not line number is there any pattern or some kind of phrase which is to be followed? Like add this line before a line which contains "blabla".
– Kulfy
10 hours ago
If not line number is there any pattern or some kind of phrase which is to be followed? Like add this line before a line which contains "blabla".
– Kulfy
10 hours ago
@Kulfy it is the second time the term 'eni' appears if that can be useful
– mattiav27
10 hours ago
@Kulfy it is the second time the term 'eni' appears if that can be useful
– mattiav27
10 hours ago
3
3
What exactly kind of modifications do you want? Please edit your question and try to make it more descriptive and specific.
– Kulfy
10 hours ago
What exactly kind of modifications do you want? Please edit your question and try to make it more descriptive and specific.
– Kulfy
10 hours ago
|
show 1 more comment
3 Answers
3
active
oldest
votes
I think this is what you want:
line=$(grep -n -m2 "eni" file | tail -n1 | cut -f1 -d:)
sed -i $line's/9$/8/' file
@wjandrea modified the answer, take a look.
– guillermo chamorro
8 hours ago
2
Nice! You could simplify getting the line number:line="$(awk '/eni/ {count+=1;if(count==2){print NR}}' test.txt)"
– wjandrea
8 hours ago
This answer will match the second occurrence ofeni
and return the line number. Then it will change the last occurrence of9
with8
in that line. If more than one9
are present in the same line, it will only change the last one and might miss the intended one.
– Raffa
7 hours ago
1
@Raffa Yes, a more elaborate regex could be done, but OP's data doesn't show more text after the line, that's why I chose to simplify it to match what OP shows as a line. Note that OP wrote "I should modify the line", not x content in the line.
– guillermo chamorro
7 hours ago
@guillermochamorro You did a good job, but I however recommend you add such explanation to your answer as it could prevent unintended modification to the file. Thank you
– Raffa
7 hours ago
add a comment |
Using sed:
sed -i ': 1 ; N ; $!b1 ; s/eni=10.*10**9/eni=10.*10**8/2' filename
/
is one of the delimiters and is the escape character.
is used so that bash won't interpret special characters as some command, like
*
as wildcard.
This answer will match the second occurrence ofeni=10.*10**9
in a new line and will change it witheni=10.*10**8
. Run it only once.
– Raffa
7 hours ago
add a comment |
Since Ubuntu now ships with GNU Awk v4.0+ (which provides an inplace
module) you could do something like
gawk -i inplace '/eni=/ {if (++c == 2) sub(/10**9/,"10**8")} 1' file
You can make the regular expressions /eni=/
and/or /10**9/
more or less specific as required.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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%2faskubuntu.com%2fquestions%2f1170057%2fhow-can-i-modify-a-line-which-contains-2nd-occurence-of-a-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think this is what you want:
line=$(grep -n -m2 "eni" file | tail -n1 | cut -f1 -d:)
sed -i $line's/9$/8/' file
@wjandrea modified the answer, take a look.
– guillermo chamorro
8 hours ago
2
Nice! You could simplify getting the line number:line="$(awk '/eni/ {count+=1;if(count==2){print NR}}' test.txt)"
– wjandrea
8 hours ago
This answer will match the second occurrence ofeni
and return the line number. Then it will change the last occurrence of9
with8
in that line. If more than one9
are present in the same line, it will only change the last one and might miss the intended one.
– Raffa
7 hours ago
1
@Raffa Yes, a more elaborate regex could be done, but OP's data doesn't show more text after the line, that's why I chose to simplify it to match what OP shows as a line. Note that OP wrote "I should modify the line", not x content in the line.
– guillermo chamorro
7 hours ago
@guillermochamorro You did a good job, but I however recommend you add such explanation to your answer as it could prevent unintended modification to the file. Thank you
– Raffa
7 hours ago
add a comment |
I think this is what you want:
line=$(grep -n -m2 "eni" file | tail -n1 | cut -f1 -d:)
sed -i $line's/9$/8/' file
@wjandrea modified the answer, take a look.
– guillermo chamorro
8 hours ago
2
Nice! You could simplify getting the line number:line="$(awk '/eni/ {count+=1;if(count==2){print NR}}' test.txt)"
– wjandrea
8 hours ago
This answer will match the second occurrence ofeni
and return the line number. Then it will change the last occurrence of9
with8
in that line. If more than one9
are present in the same line, it will only change the last one and might miss the intended one.
– Raffa
7 hours ago
1
@Raffa Yes, a more elaborate regex could be done, but OP's data doesn't show more text after the line, that's why I chose to simplify it to match what OP shows as a line. Note that OP wrote "I should modify the line", not x content in the line.
– guillermo chamorro
7 hours ago
@guillermochamorro You did a good job, but I however recommend you add such explanation to your answer as it could prevent unintended modification to the file. Thank you
– Raffa
7 hours ago
add a comment |
I think this is what you want:
line=$(grep -n -m2 "eni" file | tail -n1 | cut -f1 -d:)
sed -i $line's/9$/8/' file
I think this is what you want:
line=$(grep -n -m2 "eni" file | tail -n1 | cut -f1 -d:)
sed -i $line's/9$/8/' file
edited 9 hours ago
answered 9 hours ago
guillermo chamorroguillermo chamorro
6611 silver badge13 bronze badges
6611 silver badge13 bronze badges
@wjandrea modified the answer, take a look.
– guillermo chamorro
8 hours ago
2
Nice! You could simplify getting the line number:line="$(awk '/eni/ {count+=1;if(count==2){print NR}}' test.txt)"
– wjandrea
8 hours ago
This answer will match the second occurrence ofeni
and return the line number. Then it will change the last occurrence of9
with8
in that line. If more than one9
are present in the same line, it will only change the last one and might miss the intended one.
– Raffa
7 hours ago
1
@Raffa Yes, a more elaborate regex could be done, but OP's data doesn't show more text after the line, that's why I chose to simplify it to match what OP shows as a line. Note that OP wrote "I should modify the line", not x content in the line.
– guillermo chamorro
7 hours ago
@guillermochamorro You did a good job, but I however recommend you add such explanation to your answer as it could prevent unintended modification to the file. Thank you
– Raffa
7 hours ago
add a comment |
@wjandrea modified the answer, take a look.
– guillermo chamorro
8 hours ago
2
Nice! You could simplify getting the line number:line="$(awk '/eni/ {count+=1;if(count==2){print NR}}' test.txt)"
– wjandrea
8 hours ago
This answer will match the second occurrence ofeni
and return the line number. Then it will change the last occurrence of9
with8
in that line. If more than one9
are present in the same line, it will only change the last one and might miss the intended one.
– Raffa
7 hours ago
1
@Raffa Yes, a more elaborate regex could be done, but OP's data doesn't show more text after the line, that's why I chose to simplify it to match what OP shows as a line. Note that OP wrote "I should modify the line", not x content in the line.
– guillermo chamorro
7 hours ago
@guillermochamorro You did a good job, but I however recommend you add such explanation to your answer as it could prevent unintended modification to the file. Thank you
– Raffa
7 hours ago
@wjandrea modified the answer, take a look.
– guillermo chamorro
8 hours ago
@wjandrea modified the answer, take a look.
– guillermo chamorro
8 hours ago
2
2
Nice! You could simplify getting the line number:
line="$(awk '/eni/ {count+=1;if(count==2){print NR}}' test.txt)"
– wjandrea
8 hours ago
Nice! You could simplify getting the line number:
line="$(awk '/eni/ {count+=1;if(count==2){print NR}}' test.txt)"
– wjandrea
8 hours ago
This answer will match the second occurrence of
eni
and return the line number. Then it will change the last occurrence of 9
with 8
in that line. If more than one 9
are present in the same line, it will only change the last one and might miss the intended one.– Raffa
7 hours ago
This answer will match the second occurrence of
eni
and return the line number. Then it will change the last occurrence of 9
with 8
in that line. If more than one 9
are present in the same line, it will only change the last one and might miss the intended one.– Raffa
7 hours ago
1
1
@Raffa Yes, a more elaborate regex could be done, but OP's data doesn't show more text after the line, that's why I chose to simplify it to match what OP shows as a line. Note that OP wrote "I should modify the line", not x content in the line.
– guillermo chamorro
7 hours ago
@Raffa Yes, a more elaborate regex could be done, but OP's data doesn't show more text after the line, that's why I chose to simplify it to match what OP shows as a line. Note that OP wrote "I should modify the line", not x content in the line.
– guillermo chamorro
7 hours ago
@guillermochamorro You did a good job, but I however recommend you add such explanation to your answer as it could prevent unintended modification to the file. Thank you
– Raffa
7 hours ago
@guillermochamorro You did a good job, but I however recommend you add such explanation to your answer as it could prevent unintended modification to the file. Thank you
– Raffa
7 hours ago
add a comment |
Using sed:
sed -i ': 1 ; N ; $!b1 ; s/eni=10.*10**9/eni=10.*10**8/2' filename
/
is one of the delimiters and is the escape character.
is used so that bash won't interpret special characters as some command, like
*
as wildcard.
This answer will match the second occurrence ofeni=10.*10**9
in a new line and will change it witheni=10.*10**8
. Run it only once.
– Raffa
7 hours ago
add a comment |
Using sed:
sed -i ': 1 ; N ; $!b1 ; s/eni=10.*10**9/eni=10.*10**8/2' filename
/
is one of the delimiters and is the escape character.
is used so that bash won't interpret special characters as some command, like
*
as wildcard.
This answer will match the second occurrence ofeni=10.*10**9
in a new line and will change it witheni=10.*10**8
. Run it only once.
– Raffa
7 hours ago
add a comment |
Using sed:
sed -i ': 1 ; N ; $!b1 ; s/eni=10.*10**9/eni=10.*10**8/2' filename
/
is one of the delimiters and is the escape character.
is used so that bash won't interpret special characters as some command, like
*
as wildcard.
Using sed:
sed -i ': 1 ; N ; $!b1 ; s/eni=10.*10**9/eni=10.*10**8/2' filename
/
is one of the delimiters and is the escape character.
is used so that bash won't interpret special characters as some command, like
*
as wildcard.
edited 8 hours ago
answered 8 hours ago
KulfyKulfy
7,59010 gold badges31 silver badges58 bronze badges
7,59010 gold badges31 silver badges58 bronze badges
This answer will match the second occurrence ofeni=10.*10**9
in a new line and will change it witheni=10.*10**8
. Run it only once.
– Raffa
7 hours ago
add a comment |
This answer will match the second occurrence ofeni=10.*10**9
in a new line and will change it witheni=10.*10**8
. Run it only once.
– Raffa
7 hours ago
This answer will match the second occurrence of
eni=10.*10**9
in a new line and will change it with eni=10.*10**8
. Run it only once.– Raffa
7 hours ago
This answer will match the second occurrence of
eni=10.*10**9
in a new line and will change it with eni=10.*10**8
. Run it only once.– Raffa
7 hours ago
add a comment |
Since Ubuntu now ships with GNU Awk v4.0+ (which provides an inplace
module) you could do something like
gawk -i inplace '/eni=/ {if (++c == 2) sub(/10**9/,"10**8")} 1' file
You can make the regular expressions /eni=/
and/or /10**9/
more or less specific as required.
add a comment |
Since Ubuntu now ships with GNU Awk v4.0+ (which provides an inplace
module) you could do something like
gawk -i inplace '/eni=/ {if (++c == 2) sub(/10**9/,"10**8")} 1' file
You can make the regular expressions /eni=/
and/or /10**9/
more or less specific as required.
add a comment |
Since Ubuntu now ships with GNU Awk v4.0+ (which provides an inplace
module) you could do something like
gawk -i inplace '/eni=/ {if (++c == 2) sub(/10**9/,"10**8")} 1' file
You can make the regular expressions /eni=/
and/or /10**9/
more or less specific as required.
Since Ubuntu now ships with GNU Awk v4.0+ (which provides an inplace
module) you could do something like
gawk -i inplace '/eni=/ {if (++c == 2) sub(/10**9/,"10**8")} 1' file
You can make the regular expressions /eni=/
and/or /10**9/
more or less specific as required.
answered 4 hours ago
steeldriversteeldriver
78.4k12 gold badges129 silver badges213 bronze badges
78.4k12 gold badges129 silver badges213 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1170057%2fhow-can-i-modify-a-line-which-contains-2nd-occurence-of-a-string%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
Do you mean the exact middle of a file?
– guillermo chamorro
10 hours ago
No I mean somewhere in the file
– mattiav27
10 hours ago
If not line number is there any pattern or some kind of phrase which is to be followed? Like add this line before a line which contains "blabla".
– Kulfy
10 hours ago
@Kulfy it is the second time the term 'eni' appears if that can be useful
– mattiav27
10 hours ago
3
What exactly kind of modifications do you want? Please edit your question and try to make it more descriptive and specific.
– Kulfy
10 hours ago