awk replace value in colunm p row i by value in same column next row
#!/usr/bin/env bash
cat > example_file.txt <<EOL
group, , price
1, value, 3.21
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
EOL
I want to replace the value in the 1 row, 2nd column of this file by the value in the 2nd row 2nd column. So I'm looking to have awk return this:
#!/usr/bin/env bash
cat > example_file.txt <<EOL
group, value, price
1, value, 3.21
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
EOL
I cannot find how to reference the next line in awk:
cat example_file.txt | awk -F, 'BEGIN { OFS = FS } { if (NR==1) $2 = ??}'
awk
add a comment |
#!/usr/bin/env bash
cat > example_file.txt <<EOL
group, , price
1, value, 3.21
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
EOL
I want to replace the value in the 1 row, 2nd column of this file by the value in the 2nd row 2nd column. So I'm looking to have awk return this:
#!/usr/bin/env bash
cat > example_file.txt <<EOL
group, value, price
1, value, 3.21
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
EOL
I cannot find how to reference the next line in awk:
cat example_file.txt | awk -F, 'BEGIN { OFS = FS } { if (NR==1) $2 = ??}'
awk
add a comment |
#!/usr/bin/env bash
cat > example_file.txt <<EOL
group, , price
1, value, 3.21
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
EOL
I want to replace the value in the 1 row, 2nd column of this file by the value in the 2nd row 2nd column. So I'm looking to have awk return this:
#!/usr/bin/env bash
cat > example_file.txt <<EOL
group, value, price
1, value, 3.21
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
EOL
I cannot find how to reference the next line in awk:
cat example_file.txt | awk -F, 'BEGIN { OFS = FS } { if (NR==1) $2 = ??}'
awk
#!/usr/bin/env bash
cat > example_file.txt <<EOL
group, , price
1, value, 3.21
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
EOL
I want to replace the value in the 1 row, 2nd column of this file by the value in the 2nd row 2nd column. So I'm looking to have awk return this:
#!/usr/bin/env bash
cat > example_file.txt <<EOL
group, value, price
1, value, 3.21
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
EOL
I cannot find how to reference the next line in awk:
cat example_file.txt | awk -F, 'BEGIN { OFS = FS } { if (NR==1) $2 = ??}'
awk
awk
edited Jan 15 at 3:19
user2413
asked Jan 15 at 3:01
user2413user2413
3,931133962
3,931133962
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can use getline
to get the next line of input, and split
to split it into an array of fields using the current FS
:
$ awk -F, 'BEGIN { OFS = FS } NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]} 1' example_file.txt
group, value, price
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
If you want to print the "got" line as well, it's still in ln
after splitting (to get the order right, we also need to explicitly print
first, and skip the default print using next
):
$ awk -F, '
BEGIN { OFS = FS }
NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]; print; print ln; next} 1
' example_file.txt
group, value, price
1, value, 3.21
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
sorry, I meant for the second line to stay as it was (just the value of one particular cell of it being copied one line up). Editing the question now to make this clear
– user2413
Jan 15 at 3:18
@user2413 please see amended answer
– steeldriver
Jan 15 at 3:40
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%2f1109796%2fawk-replace-value-in-colunm-p-row-i-by-value-in-same-column-next-row%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 can use getline
to get the next line of input, and split
to split it into an array of fields using the current FS
:
$ awk -F, 'BEGIN { OFS = FS } NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]} 1' example_file.txt
group, value, price
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
If you want to print the "got" line as well, it's still in ln
after splitting (to get the order right, we also need to explicitly print
first, and skip the default print using next
):
$ awk -F, '
BEGIN { OFS = FS }
NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]; print; print ln; next} 1
' example_file.txt
group, value, price
1, value, 3.21
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
sorry, I meant for the second line to stay as it was (just the value of one particular cell of it being copied one line up). Editing the question now to make this clear
– user2413
Jan 15 at 3:18
@user2413 please see amended answer
– steeldriver
Jan 15 at 3:40
add a comment |
You can use getline
to get the next line of input, and split
to split it into an array of fields using the current FS
:
$ awk -F, 'BEGIN { OFS = FS } NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]} 1' example_file.txt
group, value, price
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
If you want to print the "got" line as well, it's still in ln
after splitting (to get the order right, we also need to explicitly print
first, and skip the default print using next
):
$ awk -F, '
BEGIN { OFS = FS }
NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]; print; print ln; next} 1
' example_file.txt
group, value, price
1, value, 3.21
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
sorry, I meant for the second line to stay as it was (just the value of one particular cell of it being copied one line up). Editing the question now to make this clear
– user2413
Jan 15 at 3:18
@user2413 please see amended answer
– steeldriver
Jan 15 at 3:40
add a comment |
You can use getline
to get the next line of input, and split
to split it into an array of fields using the current FS
:
$ awk -F, 'BEGIN { OFS = FS } NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]} 1' example_file.txt
group, value, price
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
If you want to print the "got" line as well, it's still in ln
after splitting (to get the order right, we also need to explicitly print
first, and skip the default print using next
):
$ awk -F, '
BEGIN { OFS = FS }
NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]; print; print ln; next} 1
' example_file.txt
group, value, price
1, value, 3.21
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
You can use getline
to get the next line of input, and split
to split it into an array of fields using the current FS
:
$ awk -F, 'BEGIN { OFS = FS } NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]} 1' example_file.txt
group, value, price
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
If you want to print the "got" line as well, it's still in ln
after splitting (to get the order right, we also need to explicitly print
first, and skip the default print using next
):
$ awk -F, '
BEGIN { OFS = FS }
NR==1 && (getline ln) > 0 { split(ln,a); $2 = a[2]; print; print ln; next} 1
' example_file.txt
group, value, price
1, value, 3.21
1, 3.42, 4.11
1, 3.5, 1.22
2, 4.1, 9.2
2, 4.2, 2.11
edited Jan 15 at 3:40
answered Jan 15 at 3:16
steeldriversteeldriver
67.3k11109181
67.3k11109181
sorry, I meant for the second line to stay as it was (just the value of one particular cell of it being copied one line up). Editing the question now to make this clear
– user2413
Jan 15 at 3:18
@user2413 please see amended answer
– steeldriver
Jan 15 at 3:40
add a comment |
sorry, I meant for the second line to stay as it was (just the value of one particular cell of it being copied one line up). Editing the question now to make this clear
– user2413
Jan 15 at 3:18
@user2413 please see amended answer
– steeldriver
Jan 15 at 3:40
sorry, I meant for the second line to stay as it was (just the value of one particular cell of it being copied one line up). Editing the question now to make this clear
– user2413
Jan 15 at 3:18
sorry, I meant for the second line to stay as it was (just the value of one particular cell of it being copied one line up). Editing the question now to make this clear
– user2413
Jan 15 at 3:18
@user2413 please see amended answer
– steeldriver
Jan 15 at 3:40
@user2413 please see amended answer
– steeldriver
Jan 15 at 3:40
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%2f1109796%2fawk-replace-value-in-colunm-p-row-i-by-value-in-same-column-next-row%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