replace a string from a specified line number and column
I would like to replace from line number 4, the character "K" which is right after 19823 but not the "K" character which follows
19822SOL OW60021 7.173
19822SOL HW160022 7.230
19822SOL HW260023 7.124
19823K K60024 8.427
so the new file should look like
19822SOL OW60021 7.173
19822SOL HW160022 7.230
19822SOL HW260023 7.124
19823exc K60024 8.427
currently i do know how to use sed command to replace K from line 4 with exc such as
sed -i "4s/K/exc/g" <newfile>
but I do not know how to specify which column to select
awk sed
add a comment |
I would like to replace from line number 4, the character "K" which is right after 19823 but not the "K" character which follows
19822SOL OW60021 7.173
19822SOL HW160022 7.230
19822SOL HW260023 7.124
19823K K60024 8.427
so the new file should look like
19822SOL OW60021 7.173
19822SOL HW160022 7.230
19822SOL HW260023 7.124
19823exc K60024 8.427
currently i do know how to use sed command to replace K from line 4 with exc such as
sed -i "4s/K/exc/g" <newfile>
but I do not know how to specify which column to select
awk sed
I can use sed -i "4s/19823K /19823exc/g" <newfile> but I want to avoid this ideally
– Dimitris Mintis
Dec 9 at 22:30
OK, so you don't want to change aK
that occurs after a19823
; you want to change aK
that occurs in the first column of line 4. Or do you want to change all theK
s that occur in the first column of line 4? Or do you want to change aK
that occurs in a specified column of line 4 (or some other specified line)? The simple solution of leaving off theg
doesn't help if you want to manipulate a column other than the first.
– G-Man
Dec 10 at 1:14
add a comment |
I would like to replace from line number 4, the character "K" which is right after 19823 but not the "K" character which follows
19822SOL OW60021 7.173
19822SOL HW160022 7.230
19822SOL HW260023 7.124
19823K K60024 8.427
so the new file should look like
19822SOL OW60021 7.173
19822SOL HW160022 7.230
19822SOL HW260023 7.124
19823exc K60024 8.427
currently i do know how to use sed command to replace K from line 4 with exc such as
sed -i "4s/K/exc/g" <newfile>
but I do not know how to specify which column to select
awk sed
I would like to replace from line number 4, the character "K" which is right after 19823 but not the "K" character which follows
19822SOL OW60021 7.173
19822SOL HW160022 7.230
19822SOL HW260023 7.124
19823K K60024 8.427
so the new file should look like
19822SOL OW60021 7.173
19822SOL HW160022 7.230
19822SOL HW260023 7.124
19823exc K60024 8.427
currently i do know how to use sed command to replace K from line 4 with exc such as
sed -i "4s/K/exc/g" <newfile>
but I do not know how to specify which column to select
awk sed
awk sed
asked Dec 9 at 22:17
Dimitris Mintis
425
425
I can use sed -i "4s/19823K /19823exc/g" <newfile> but I want to avoid this ideally
– Dimitris Mintis
Dec 9 at 22:30
OK, so you don't want to change aK
that occurs after a19823
; you want to change aK
that occurs in the first column of line 4. Or do you want to change all theK
s that occur in the first column of line 4? Or do you want to change aK
that occurs in a specified column of line 4 (or some other specified line)? The simple solution of leaving off theg
doesn't help if you want to manipulate a column other than the first.
– G-Man
Dec 10 at 1:14
add a comment |
I can use sed -i "4s/19823K /19823exc/g" <newfile> but I want to avoid this ideally
– Dimitris Mintis
Dec 9 at 22:30
OK, so you don't want to change aK
that occurs after a19823
; you want to change aK
that occurs in the first column of line 4. Or do you want to change all theK
s that occur in the first column of line 4? Or do you want to change aK
that occurs in a specified column of line 4 (or some other specified line)? The simple solution of leaving off theg
doesn't help if you want to manipulate a column other than the first.
– G-Man
Dec 10 at 1:14
I can use sed -i "4s/19823K /19823exc/g" <newfile> but I want to avoid this ideally
– Dimitris Mintis
Dec 9 at 22:30
I can use sed -i "4s/19823K /19823exc/g" <newfile> but I want to avoid this ideally
– Dimitris Mintis
Dec 9 at 22:30
OK, so you don't want to change a
K
that occurs after a 19823
; you want to change a K
that occurs in the first column of line 4. Or do you want to change all the K
s that occur in the first column of line 4? Or do you want to change a K
that occurs in a specified column of line 4 (or some other specified line)? The simple solution of leaving off the g
doesn't help if you want to manipulate a column other than the first.– G-Man
Dec 10 at 1:14
OK, so you don't want to change a
K
that occurs after a 19823
; you want to change a K
that occurs in the first column of line 4. Or do you want to change all the K
s that occur in the first column of line 4? Or do you want to change a K
that occurs in a specified column of line 4 (or some other specified line)? The simple solution of leaving off the g
doesn't help if you want to manipulate a column other than the first.– G-Man
Dec 10 at 1:14
add a comment |
2 Answers
2
active
oldest
votes
If it's just about that particular K
, then getting rid of the g
flag will do, as sed
will only replace K
once. Thus:
sed -i "4s/K/exc/" <newfile>
Namely, it replaces the first appearance of K
with exc
on line 4.
add a comment |
If you want the first K
, just get rid of the g
flag:
sed -i '4s/K/exc/g' <newfile>
If you want to replace the 2nd, 3rd, etc. K
, use 2
, 3
, 4
instead of g
sed -i '4s/K/exc/2' <newfile>
sed -i '4s/K/exc/3' <newfile>
If you want to replace all K
s from the 3rd up to the end:
echo 'KKKKKK' | sed s/K/E/3g
KKEEEE
And btw, don't use double quotes ("
) unless you really want to insert shell variables, command expansions, etc in the string passed to sed
.
Note that using both a number andg
as flags to the substitute command requires GNUsed
.
– Kusalananda
Dec 15 at 10:20
add a comment |
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%2f487006%2freplace-a-string-from-a-specified-line-number-and-column%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If it's just about that particular K
, then getting rid of the g
flag will do, as sed
will only replace K
once. Thus:
sed -i "4s/K/exc/" <newfile>
Namely, it replaces the first appearance of K
with exc
on line 4.
add a comment |
If it's just about that particular K
, then getting rid of the g
flag will do, as sed
will only replace K
once. Thus:
sed -i "4s/K/exc/" <newfile>
Namely, it replaces the first appearance of K
with exc
on line 4.
add a comment |
If it's just about that particular K
, then getting rid of the g
flag will do, as sed
will only replace K
once. Thus:
sed -i "4s/K/exc/" <newfile>
Namely, it replaces the first appearance of K
with exc
on line 4.
If it's just about that particular K
, then getting rid of the g
flag will do, as sed
will only replace K
once. Thus:
sed -i "4s/K/exc/" <newfile>
Namely, it replaces the first appearance of K
with exc
on line 4.
answered Dec 9 at 22:41
Tomasz
9,18852965
9,18852965
add a comment |
add a comment |
If you want the first K
, just get rid of the g
flag:
sed -i '4s/K/exc/g' <newfile>
If you want to replace the 2nd, 3rd, etc. K
, use 2
, 3
, 4
instead of g
sed -i '4s/K/exc/2' <newfile>
sed -i '4s/K/exc/3' <newfile>
If you want to replace all K
s from the 3rd up to the end:
echo 'KKKKKK' | sed s/K/E/3g
KKEEEE
And btw, don't use double quotes ("
) unless you really want to insert shell variables, command expansions, etc in the string passed to sed
.
Note that using both a number andg
as flags to the substitute command requires GNUsed
.
– Kusalananda
Dec 15 at 10:20
add a comment |
If you want the first K
, just get rid of the g
flag:
sed -i '4s/K/exc/g' <newfile>
If you want to replace the 2nd, 3rd, etc. K
, use 2
, 3
, 4
instead of g
sed -i '4s/K/exc/2' <newfile>
sed -i '4s/K/exc/3' <newfile>
If you want to replace all K
s from the 3rd up to the end:
echo 'KKKKKK' | sed s/K/E/3g
KKEEEE
And btw, don't use double quotes ("
) unless you really want to insert shell variables, command expansions, etc in the string passed to sed
.
Note that using both a number andg
as flags to the substitute command requires GNUsed
.
– Kusalananda
Dec 15 at 10:20
add a comment |
If you want the first K
, just get rid of the g
flag:
sed -i '4s/K/exc/g' <newfile>
If you want to replace the 2nd, 3rd, etc. K
, use 2
, 3
, 4
instead of g
sed -i '4s/K/exc/2' <newfile>
sed -i '4s/K/exc/3' <newfile>
If you want to replace all K
s from the 3rd up to the end:
echo 'KKKKKK' | sed s/K/E/3g
KKEEEE
And btw, don't use double quotes ("
) unless you really want to insert shell variables, command expansions, etc in the string passed to sed
.
If you want the first K
, just get rid of the g
flag:
sed -i '4s/K/exc/g' <newfile>
If you want to replace the 2nd, 3rd, etc. K
, use 2
, 3
, 4
instead of g
sed -i '4s/K/exc/2' <newfile>
sed -i '4s/K/exc/3' <newfile>
If you want to replace all K
s from the 3rd up to the end:
echo 'KKKKKK' | sed s/K/E/3g
KKEEEE
And btw, don't use double quotes ("
) unless you really want to insert shell variables, command expansions, etc in the string passed to sed
.
answered Dec 10 at 0:32
Uncle Billy
1244
1244
Note that using both a number andg
as flags to the substitute command requires GNUsed
.
– Kusalananda
Dec 15 at 10:20
add a comment |
Note that using both a number andg
as flags to the substitute command requires GNUsed
.
– Kusalananda
Dec 15 at 10:20
Note that using both a number and
g
as flags to the substitute command requires GNU sed
.– Kusalananda
Dec 15 at 10:20
Note that using both a number and
g
as flags to the substitute command requires GNU sed
.– Kusalananda
Dec 15 at 10:20
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f487006%2freplace-a-string-from-a-specified-line-number-and-column%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
I can use sed -i "4s/19823K /19823exc/g" <newfile> but I want to avoid this ideally
– Dimitris Mintis
Dec 9 at 22:30
OK, so you don't want to change a
K
that occurs after a19823
; you want to change aK
that occurs in the first column of line 4. Or do you want to change all theK
s that occur in the first column of line 4? Or do you want to change aK
that occurs in a specified column of line 4 (or some other specified line)? The simple solution of leaving off theg
doesn't help if you want to manipulate a column other than the first.– G-Man
Dec 10 at 1:14