Subtracting numbers from adjacent columns and successive rows using awk The Next CEO of Stack Overflowtext processing-perlawk manipulationAvoid conversion to scientific notation in awkBash to join columns from multiple filesUsing AWK to calculate mean and variance of columnsHow extract values which are less than 0 and dot from specific columns and print the entire row using awktext processing - Extracting using cshell and awkSubtracting same column between two rows in awkSwitch columns in CSV using awk?Looping through columns and rows to count specific values
logical reads on global temp table, but not on session-level temp table
Is there a rule of thumb for determining the amount one should accept for a settlement offer?
Calculate the Mean mean of two numbers
That's an odd coin - I wonder why
Incomplete cube
"Eavesdropping" vs "Listen in on"
Traveling with my 5 year old daughter (as the father) without the mother from Germany to Mexico
What day is it again?
How does a dynamic QR code work?
Small nick on power cord from an electric alarm clock, and copper wiring exposed but intact
How can I prove that a state of equilibrium is unstable?
Creating a script with console commands
Is a distribution that is normal, but highly skewed, considered Gaussian?
Why did early computer designers eschew integers?
Why can't we say "I have been having a dog"?
How can the PCs determine if an item is a phylactery?
Does Germany produce more waste than the US?
What steps are necessary to read a Modern SSD in Medieval Europe?
Upgrading From a 9 Speed Sora Derailleur?
How can I replace x-axis labels with pre-determined symbols?
Shortening a title without changing its meaning
How dangerous is XSS
Masking layers by a vector polygon layer in QGIS
Planeswalker Ability and Death Timing
Subtracting numbers from adjacent columns and successive rows using awk
The Next CEO of Stack Overflowtext processing-perlawk manipulationAvoid conversion to scientific notation in awkBash to join columns from multiple filesUsing AWK to calculate mean and variance of columnsHow extract values which are less than 0 and dot from specific columns and print the entire row using awktext processing - Extracting using cshell and awkSubtracting same column between two rows in awkSwitch columns in CSV using awk?Looping through columns and rows to count specific values
I have a tab-separated file that looks like this:
NZ_CP023599.1 WP_003911075.1 302845 305406
NZ_CP023599.1 WP_003898428.1 471171 472583
NZ_CP023599.1 WP_003402248.1 534387 535157
NZ_CP023599.1 WP_003402301.1 552556 553950
NZ_CP023599.1 WP_003402318.1 558837 559697
I need to subtract the number in 4th column of each row from the number in 3rd column of the next line, and then print the difference in the next line as a 5th column.
The output would look like this:
NZ_CP023599.1 WP_003911075.1 302845 305406
NZ_CP023599.1 WP_003898428.1 471171 472583 165765
NZ_CP023599.1 WP_003402248.1 534387 535157 61804
NZ_CP023599.1 WP_003402301.1 552556 553950 17399
NZ_CP023599.1 WP_003402318.1 558837 559697 4887
How do I go about this using awk?
awk
New contributor
add a comment |
I have a tab-separated file that looks like this:
NZ_CP023599.1 WP_003911075.1 302845 305406
NZ_CP023599.1 WP_003898428.1 471171 472583
NZ_CP023599.1 WP_003402248.1 534387 535157
NZ_CP023599.1 WP_003402301.1 552556 553950
NZ_CP023599.1 WP_003402318.1 558837 559697
I need to subtract the number in 4th column of each row from the number in 3rd column of the next line, and then print the difference in the next line as a 5th column.
The output would look like this:
NZ_CP023599.1 WP_003911075.1 302845 305406
NZ_CP023599.1 WP_003898428.1 471171 472583 165765
NZ_CP023599.1 WP_003402248.1 534387 535157 61804
NZ_CP023599.1 WP_003402301.1 552556 553950 17399
NZ_CP023599.1 WP_003402318.1 558837 559697 4887
How do I go about this using awk?
awk
New contributor
add a comment |
I have a tab-separated file that looks like this:
NZ_CP023599.1 WP_003911075.1 302845 305406
NZ_CP023599.1 WP_003898428.1 471171 472583
NZ_CP023599.1 WP_003402248.1 534387 535157
NZ_CP023599.1 WP_003402301.1 552556 553950
NZ_CP023599.1 WP_003402318.1 558837 559697
I need to subtract the number in 4th column of each row from the number in 3rd column of the next line, and then print the difference in the next line as a 5th column.
The output would look like this:
NZ_CP023599.1 WP_003911075.1 302845 305406
NZ_CP023599.1 WP_003898428.1 471171 472583 165765
NZ_CP023599.1 WP_003402248.1 534387 535157 61804
NZ_CP023599.1 WP_003402301.1 552556 553950 17399
NZ_CP023599.1 WP_003402318.1 558837 559697 4887
How do I go about this using awk?
awk
New contributor
I have a tab-separated file that looks like this:
NZ_CP023599.1 WP_003911075.1 302845 305406
NZ_CP023599.1 WP_003898428.1 471171 472583
NZ_CP023599.1 WP_003402248.1 534387 535157
NZ_CP023599.1 WP_003402301.1 552556 553950
NZ_CP023599.1 WP_003402318.1 558837 559697
I need to subtract the number in 4th column of each row from the number in 3rd column of the next line, and then print the difference in the next line as a 5th column.
The output would look like this:
NZ_CP023599.1 WP_003911075.1 302845 305406
NZ_CP023599.1 WP_003898428.1 471171 472583 165765
NZ_CP023599.1 WP_003402248.1 534387 535157 61804
NZ_CP023599.1 WP_003402301.1 552556 553950 17399
NZ_CP023599.1 WP_003402318.1 558837 559697 4887
How do I go about this using awk?
awk
awk
New contributor
New contributor
New contributor
asked 15 hours ago
BhushanDhamaleBhushanDhamale
1233
1233
New contributor
New contributor
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can do this as below. Defer the subtraction except for the first line but get its last column value as input for the subsequent line.
awk -F't' 'BEGIN OFS = FS NR == 1 last = $4; print; next $5 = $3 - last; last = $4 1' file
add a comment |
awk -F\t ' if (length(prev4)>0) col5 = FS ($3-prev4) ; print $0 col5; prev4 = $4 ' InputFile
New contributor
add a comment |
Tried with Below command and its working fine
for ((i=1;i<=5;i++)); do if [[ $i == 1 ]]; then awk -v i="$i" 'NR==iprint $0' l.txt ; j=`awk -v i="$i" 'NR==iprint $3 - $4 ' l.txt`; else awk -v i="$i" -v j="$j" 'NR==i$5=j;print $0' l.txt;j=`awk -v i="$i" 'NR == iprint $3 - $4' l.txt`;fi;done
This is extremely inefficient for several reasons, among them (1) awk is invoked once per line, with all the overhead of starting and stopping a process; (2) each time it is started, awk has to search through the file until it comes to the next line to be edited
– pericynthion
8 hours ago
In fact, this is so inefficient I'm wondering if it's an April fool's? If so, well played.
– pericynthion
8 hours ago
(1) This does not produce the correct, desired result. (2) Even though previous answers have shown that it is possible to do this with a single invocation ofawk
, you believe that it needs 10? (3) If sample data were real data (and the only data that will ever occur), we could just take the desired result from the question and wrap it in aprintf
or acat
. You have assumed that the real data will have only 5 lines of data, based on the fact that the sample input has only 5 lines of data.
– G-Man
8 hours ago
add a comment |
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
);
);
BhushanDhamale is a new contributor. Be nice, and check out our Code of Conduct.
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%2f509834%2fsubtracting-numbers-from-adjacent-columns-and-successive-rows-using-awk%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
You can do this as below. Defer the subtraction except for the first line but get its last column value as input for the subsequent line.
awk -F't' 'BEGIN OFS = FS NR == 1 last = $4; print; next $5 = $3 - last; last = $4 1' file
add a comment |
You can do this as below. Defer the subtraction except for the first line but get its last column value as input for the subsequent line.
awk -F't' 'BEGIN OFS = FS NR == 1 last = $4; print; next $5 = $3 - last; last = $4 1' file
add a comment |
You can do this as below. Defer the subtraction except for the first line but get its last column value as input for the subsequent line.
awk -F't' 'BEGIN OFS = FS NR == 1 last = $4; print; next $5 = $3 - last; last = $4 1' file
You can do this as below. Defer the subtraction except for the first line but get its last column value as input for the subsequent line.
awk -F't' 'BEGIN OFS = FS NR == 1 last = $4; print; next $5 = $3 - last; last = $4 1' file
edited 14 hours ago
answered 15 hours ago
InianInian
5,3001530
5,3001530
add a comment |
add a comment |
awk -F\t ' if (length(prev4)>0) col5 = FS ($3-prev4) ; print $0 col5; prev4 = $4 ' InputFile
New contributor
add a comment |
awk -F\t ' if (length(prev4)>0) col5 = FS ($3-prev4) ; print $0 col5; prev4 = $4 ' InputFile
New contributor
add a comment |
awk -F\t ' if (length(prev4)>0) col5 = FS ($3-prev4) ; print $0 col5; prev4 = $4 ' InputFile
New contributor
awk -F\t ' if (length(prev4)>0) col5 = FS ($3-prev4) ; print $0 col5; prev4 = $4 ' InputFile
New contributor
New contributor
answered 11 hours ago
John MartinJohn Martin
1
1
New contributor
New contributor
add a comment |
add a comment |
Tried with Below command and its working fine
for ((i=1;i<=5;i++)); do if [[ $i == 1 ]]; then awk -v i="$i" 'NR==iprint $0' l.txt ; j=`awk -v i="$i" 'NR==iprint $3 - $4 ' l.txt`; else awk -v i="$i" -v j="$j" 'NR==i$5=j;print $0' l.txt;j=`awk -v i="$i" 'NR == iprint $3 - $4' l.txt`;fi;done
This is extremely inefficient for several reasons, among them (1) awk is invoked once per line, with all the overhead of starting and stopping a process; (2) each time it is started, awk has to search through the file until it comes to the next line to be edited
– pericynthion
8 hours ago
In fact, this is so inefficient I'm wondering if it's an April fool's? If so, well played.
– pericynthion
8 hours ago
(1) This does not produce the correct, desired result. (2) Even though previous answers have shown that it is possible to do this with a single invocation ofawk
, you believe that it needs 10? (3) If sample data were real data (and the only data that will ever occur), we could just take the desired result from the question and wrap it in aprintf
or acat
. You have assumed that the real data will have only 5 lines of data, based on the fact that the sample input has only 5 lines of data.
– G-Man
8 hours ago
add a comment |
Tried with Below command and its working fine
for ((i=1;i<=5;i++)); do if [[ $i == 1 ]]; then awk -v i="$i" 'NR==iprint $0' l.txt ; j=`awk -v i="$i" 'NR==iprint $3 - $4 ' l.txt`; else awk -v i="$i" -v j="$j" 'NR==i$5=j;print $0' l.txt;j=`awk -v i="$i" 'NR == iprint $3 - $4' l.txt`;fi;done
This is extremely inefficient for several reasons, among them (1) awk is invoked once per line, with all the overhead of starting and stopping a process; (2) each time it is started, awk has to search through the file until it comes to the next line to be edited
– pericynthion
8 hours ago
In fact, this is so inefficient I'm wondering if it's an April fool's? If so, well played.
– pericynthion
8 hours ago
(1) This does not produce the correct, desired result. (2) Even though previous answers have shown that it is possible to do this with a single invocation ofawk
, you believe that it needs 10? (3) If sample data were real data (and the only data that will ever occur), we could just take the desired result from the question and wrap it in aprintf
or acat
. You have assumed that the real data will have only 5 lines of data, based on the fact that the sample input has only 5 lines of data.
– G-Man
8 hours ago
add a comment |
Tried with Below command and its working fine
for ((i=1;i<=5;i++)); do if [[ $i == 1 ]]; then awk -v i="$i" 'NR==iprint $0' l.txt ; j=`awk -v i="$i" 'NR==iprint $3 - $4 ' l.txt`; else awk -v i="$i" -v j="$j" 'NR==i$5=j;print $0' l.txt;j=`awk -v i="$i" 'NR == iprint $3 - $4' l.txt`;fi;done
Tried with Below command and its working fine
for ((i=1;i<=5;i++)); do if [[ $i == 1 ]]; then awk -v i="$i" 'NR==iprint $0' l.txt ; j=`awk -v i="$i" 'NR==iprint $3 - $4 ' l.txt`; else awk -v i="$i" -v j="$j" 'NR==i$5=j;print $0' l.txt;j=`awk -v i="$i" 'NR == iprint $3 - $4' l.txt`;fi;done
answered 9 hours ago
Praveen Kumar BSPraveen Kumar BS
1,7101311
1,7101311
This is extremely inefficient for several reasons, among them (1) awk is invoked once per line, with all the overhead of starting and stopping a process; (2) each time it is started, awk has to search through the file until it comes to the next line to be edited
– pericynthion
8 hours ago
In fact, this is so inefficient I'm wondering if it's an April fool's? If so, well played.
– pericynthion
8 hours ago
(1) This does not produce the correct, desired result. (2) Even though previous answers have shown that it is possible to do this with a single invocation ofawk
, you believe that it needs 10? (3) If sample data were real data (and the only data that will ever occur), we could just take the desired result from the question and wrap it in aprintf
or acat
. You have assumed that the real data will have only 5 lines of data, based on the fact that the sample input has only 5 lines of data.
– G-Man
8 hours ago
add a comment |
This is extremely inefficient for several reasons, among them (1) awk is invoked once per line, with all the overhead of starting and stopping a process; (2) each time it is started, awk has to search through the file until it comes to the next line to be edited
– pericynthion
8 hours ago
In fact, this is so inefficient I'm wondering if it's an April fool's? If so, well played.
– pericynthion
8 hours ago
(1) This does not produce the correct, desired result. (2) Even though previous answers have shown that it is possible to do this with a single invocation ofawk
, you believe that it needs 10? (3) If sample data were real data (and the only data that will ever occur), we could just take the desired result from the question and wrap it in aprintf
or acat
. You have assumed that the real data will have only 5 lines of data, based on the fact that the sample input has only 5 lines of data.
– G-Man
8 hours ago
This is extremely inefficient for several reasons, among them (1) awk is invoked once per line, with all the overhead of starting and stopping a process; (2) each time it is started, awk has to search through the file until it comes to the next line to be edited
– pericynthion
8 hours ago
This is extremely inefficient for several reasons, among them (1) awk is invoked once per line, with all the overhead of starting and stopping a process; (2) each time it is started, awk has to search through the file until it comes to the next line to be edited
– pericynthion
8 hours ago
In fact, this is so inefficient I'm wondering if it's an April fool's? If so, well played.
– pericynthion
8 hours ago
In fact, this is so inefficient I'm wondering if it's an April fool's? If so, well played.
– pericynthion
8 hours ago
(1) This does not produce the correct, desired result. (2) Even though previous answers have shown that it is possible to do this with a single invocation of
awk
, you believe that it needs 10? (3) If sample data were real data (and the only data that will ever occur), we could just take the desired result from the question and wrap it in a printf
or a cat
. You have assumed that the real data will have only 5 lines of data, based on the fact that the sample input has only 5 lines of data.– G-Man
8 hours ago
(1) This does not produce the correct, desired result. (2) Even though previous answers have shown that it is possible to do this with a single invocation of
awk
, you believe that it needs 10? (3) If sample data were real data (and the only data that will ever occur), we could just take the desired result from the question and wrap it in a printf
or a cat
. You have assumed that the real data will have only 5 lines of data, based on the fact that the sample input has only 5 lines of data.– G-Man
8 hours ago
add a comment |
BhushanDhamale is a new contributor. Be nice, and check out our Code of Conduct.
BhushanDhamale is a new contributor. Be nice, and check out our Code of Conduct.
BhushanDhamale is a new contributor. Be nice, and check out our Code of Conduct.
BhushanDhamale is a new contributor. Be nice, and check out our Code of Conduct.
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%2f509834%2fsubtracting-numbers-from-adjacent-columns-and-successive-rows-using-awk%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