Subtracting same column between two rows in awk

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
4
down vote
favorite
I want to subtract the second line with the first line. The file is like this
tmptxt
A B 1 2 3 4
C D 9 8 7 6
The desired output is
8 6 4 2
How to do this in awk?
I managed to output for a single column only:
awk '$temp=$3-prev3; prev3=$3print $temp'
shell-script text-processing awk
add a comment |Â
up vote
4
down vote
favorite
I want to subtract the second line with the first line. The file is like this
tmptxt
A B 1 2 3 4
C D 9 8 7 6
The desired output is
8 6 4 2
How to do this in awk?
I managed to output for a single column only:
awk '$temp=$3-prev3; prev3=$3print $temp'
shell-script text-processing awk
1
@John1024I managed to output for single columnawk '$temp=$3-prev3; prev3=$3print $temp'. The only limitation is i am using tsch on CentOS. I know tsch is inferior compared to other interpreter but this is what i have to use. I tried this too but didnt get what i wantcat tmpfile | awk 'for(i=3;i<=NF;i++)$temp[i]=$i-prev; prev=$i;print $temp[i]'
â nabilishes
Nov 23 '17 at 5:08
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I want to subtract the second line with the first line. The file is like this
tmptxt
A B 1 2 3 4
C D 9 8 7 6
The desired output is
8 6 4 2
How to do this in awk?
I managed to output for a single column only:
awk '$temp=$3-prev3; prev3=$3print $temp'
shell-script text-processing awk
I want to subtract the second line with the first line. The file is like this
tmptxt
A B 1 2 3 4
C D 9 8 7 6
The desired output is
8 6 4 2
How to do this in awk?
I managed to output for a single column only:
awk '$temp=$3-prev3; prev3=$3print $temp'
shell-script text-processing awk
edited Nov 23 '17 at 5:57
asked Nov 23 '17 at 4:07
nabilishes
337
337
1
@John1024I managed to output for single columnawk '$temp=$3-prev3; prev3=$3print $temp'. The only limitation is i am using tsch on CentOS. I know tsch is inferior compared to other interpreter but this is what i have to use. I tried this too but didnt get what i wantcat tmpfile | awk 'for(i=3;i<=NF;i++)$temp[i]=$i-prev; prev=$i;print $temp[i]'
â nabilishes
Nov 23 '17 at 5:08
add a comment |Â
1
@John1024I managed to output for single columnawk '$temp=$3-prev3; prev3=$3print $temp'. The only limitation is i am using tsch on CentOS. I know tsch is inferior compared to other interpreter but this is what i have to use. I tried this too but didnt get what i wantcat tmpfile | awk 'for(i=3;i<=NF;i++)$temp[i]=$i-prev; prev=$i;print $temp[i]'
â nabilishes
Nov 23 '17 at 5:08
1
1
@John1024I managed to output for single column
awk '$temp=$3-prev3; prev3=$3print $temp'. The only limitation is i am using tsch on CentOS. I know tsch is inferior compared to other interpreter but this is what i have to use. I tried this too but didnt get what i want cat tmpfile | awk 'for(i=3;i<=NF;i++)$temp[i]=$i-prev; prev=$i;print $temp[i]'â nabilishes
Nov 23 '17 at 5:08
@John1024I managed to output for single column
awk '$temp=$3-prev3; prev3=$3print $temp'. The only limitation is i am using tsch on CentOS. I know tsch is inferior compared to other interpreter but this is what i have to use. I tried this too but didnt get what i want cat tmpfile | awk 'for(i=3;i<=NF;i++)$temp[i]=$i-prev; prev=$i;print $temp[i]'â nabilishes
Nov 23 '17 at 5:08
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
A solution with awk
awk '
NR==1 split($0,a)
NR==2 split($0,b)
END for(i=1;i<=NF;i++) printf "%d ", b[i]-a[i]
' input.txt
gives a result of
0 0 8 6 4 2
Since awk interpret strings without valid numbers as 0 during arithmetic operations, in case you want to remove the results in which the source field contains non-numeric values, you can do this by adding an additional condition.
awk '
NR==1 split($0,a)
NR==2 split($0,b)
END
for(i=1;i<=NF;i++)
if(a[i] ~ /^[0-9]+$/ && b[i] ~ /^[0-9]+$/)
printf "%d ", b[i]-a[i]
' input.txt
gives a result of
8 6 4 2
1
thanks! this somehow help me in understanding lines splitting. However due to the nature of my workstation running on tsch, the arrangement of output is different on my side. It gives out with your first solution6 4 2 0 0 8
â nabilishes
Nov 23 '17 at 6:11
@nabilishes Hmm, that's odd, what's the awk version you are currently usingawk --version?
â etopylight
Nov 23 '17 at 6:52
Its GNU Awk 3.1.7
â nabilishes
Nov 23 '17 at 6:54
1
@nabilishes Thanks for the feedback, I can reproduce the same result usinggawk 3.1.7withbash, it seems that the scanning order of array in awk is different in older versions. A safer solution is to replacefor(i in a)with an explicit orderfor(i=1;i<=NF;i++)
â etopylight
Nov 23 '17 at 7:09
Thanks! this solution works like a charm. I previously had the same problem with the array sorting - Here is the link of the same array sorting problem. It is in the comment section. Now i know its due to the version and not environment or interpreter
â nabilishes
Nov 23 '17 at 7:23
add a comment |Â
up vote
2
down vote
Alternative Python solution:
python -c 'import sys; f=open(sys.argv[1],"r");
print(" ".join(str(int(d2)-int(d1))
for d1,d2 in zip(next(f).split()[2:], next(f).split()[2:]))); f.close()' file
The output:
8 6 4 2
add a comment |Â
up vote
0
down vote
below command also provides same output. l.txt contains the input which you have provided in question
for i in 1..6; do awk -v i="$i" 'BEGINsum=0sum=$i-sumENDprint sum' l.txt ; done| tr "n" " " | awk '$1="";$2="";print $0' | sed "s/^ //g"
output
8 6 4 2
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
A solution with awk
awk '
NR==1 split($0,a)
NR==2 split($0,b)
END for(i=1;i<=NF;i++) printf "%d ", b[i]-a[i]
' input.txt
gives a result of
0 0 8 6 4 2
Since awk interpret strings without valid numbers as 0 during arithmetic operations, in case you want to remove the results in which the source field contains non-numeric values, you can do this by adding an additional condition.
awk '
NR==1 split($0,a)
NR==2 split($0,b)
END
for(i=1;i<=NF;i++)
if(a[i] ~ /^[0-9]+$/ && b[i] ~ /^[0-9]+$/)
printf "%d ", b[i]-a[i]
' input.txt
gives a result of
8 6 4 2
1
thanks! this somehow help me in understanding lines splitting. However due to the nature of my workstation running on tsch, the arrangement of output is different on my side. It gives out with your first solution6 4 2 0 0 8
â nabilishes
Nov 23 '17 at 6:11
@nabilishes Hmm, that's odd, what's the awk version you are currently usingawk --version?
â etopylight
Nov 23 '17 at 6:52
Its GNU Awk 3.1.7
â nabilishes
Nov 23 '17 at 6:54
1
@nabilishes Thanks for the feedback, I can reproduce the same result usinggawk 3.1.7withbash, it seems that the scanning order of array in awk is different in older versions. A safer solution is to replacefor(i in a)with an explicit orderfor(i=1;i<=NF;i++)
â etopylight
Nov 23 '17 at 7:09
Thanks! this solution works like a charm. I previously had the same problem with the array sorting - Here is the link of the same array sorting problem. It is in the comment section. Now i know its due to the version and not environment or interpreter
â nabilishes
Nov 23 '17 at 7:23
add a comment |Â
up vote
4
down vote
accepted
A solution with awk
awk '
NR==1 split($0,a)
NR==2 split($0,b)
END for(i=1;i<=NF;i++) printf "%d ", b[i]-a[i]
' input.txt
gives a result of
0 0 8 6 4 2
Since awk interpret strings without valid numbers as 0 during arithmetic operations, in case you want to remove the results in which the source field contains non-numeric values, you can do this by adding an additional condition.
awk '
NR==1 split($0,a)
NR==2 split($0,b)
END
for(i=1;i<=NF;i++)
if(a[i] ~ /^[0-9]+$/ && b[i] ~ /^[0-9]+$/)
printf "%d ", b[i]-a[i]
' input.txt
gives a result of
8 6 4 2
1
thanks! this somehow help me in understanding lines splitting. However due to the nature of my workstation running on tsch, the arrangement of output is different on my side. It gives out with your first solution6 4 2 0 0 8
â nabilishes
Nov 23 '17 at 6:11
@nabilishes Hmm, that's odd, what's the awk version you are currently usingawk --version?
â etopylight
Nov 23 '17 at 6:52
Its GNU Awk 3.1.7
â nabilishes
Nov 23 '17 at 6:54
1
@nabilishes Thanks for the feedback, I can reproduce the same result usinggawk 3.1.7withbash, it seems that the scanning order of array in awk is different in older versions. A safer solution is to replacefor(i in a)with an explicit orderfor(i=1;i<=NF;i++)
â etopylight
Nov 23 '17 at 7:09
Thanks! this solution works like a charm. I previously had the same problem with the array sorting - Here is the link of the same array sorting problem. It is in the comment section. Now i know its due to the version and not environment or interpreter
â nabilishes
Nov 23 '17 at 7:23
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
A solution with awk
awk '
NR==1 split($0,a)
NR==2 split($0,b)
END for(i=1;i<=NF;i++) printf "%d ", b[i]-a[i]
' input.txt
gives a result of
0 0 8 6 4 2
Since awk interpret strings without valid numbers as 0 during arithmetic operations, in case you want to remove the results in which the source field contains non-numeric values, you can do this by adding an additional condition.
awk '
NR==1 split($0,a)
NR==2 split($0,b)
END
for(i=1;i<=NF;i++)
if(a[i] ~ /^[0-9]+$/ && b[i] ~ /^[0-9]+$/)
printf "%d ", b[i]-a[i]
' input.txt
gives a result of
8 6 4 2
A solution with awk
awk '
NR==1 split($0,a)
NR==2 split($0,b)
END for(i=1;i<=NF;i++) printf "%d ", b[i]-a[i]
' input.txt
gives a result of
0 0 8 6 4 2
Since awk interpret strings without valid numbers as 0 during arithmetic operations, in case you want to remove the results in which the source field contains non-numeric values, you can do this by adding an additional condition.
awk '
NR==1 split($0,a)
NR==2 split($0,b)
END
for(i=1;i<=NF;i++)
if(a[i] ~ /^[0-9]+$/ && b[i] ~ /^[0-9]+$/)
printf "%d ", b[i]-a[i]
' input.txt
gives a result of
8 6 4 2
edited Nov 23 '17 at 7:17
answered Nov 23 '17 at 4:45
etopylight
383117
383117
1
thanks! this somehow help me in understanding lines splitting. However due to the nature of my workstation running on tsch, the arrangement of output is different on my side. It gives out with your first solution6 4 2 0 0 8
â nabilishes
Nov 23 '17 at 6:11
@nabilishes Hmm, that's odd, what's the awk version you are currently usingawk --version?
â etopylight
Nov 23 '17 at 6:52
Its GNU Awk 3.1.7
â nabilishes
Nov 23 '17 at 6:54
1
@nabilishes Thanks for the feedback, I can reproduce the same result usinggawk 3.1.7withbash, it seems that the scanning order of array in awk is different in older versions. A safer solution is to replacefor(i in a)with an explicit orderfor(i=1;i<=NF;i++)
â etopylight
Nov 23 '17 at 7:09
Thanks! this solution works like a charm. I previously had the same problem with the array sorting - Here is the link of the same array sorting problem. It is in the comment section. Now i know its due to the version and not environment or interpreter
â nabilishes
Nov 23 '17 at 7:23
add a comment |Â
1
thanks! this somehow help me in understanding lines splitting. However due to the nature of my workstation running on tsch, the arrangement of output is different on my side. It gives out with your first solution6 4 2 0 0 8
â nabilishes
Nov 23 '17 at 6:11
@nabilishes Hmm, that's odd, what's the awk version you are currently usingawk --version?
â etopylight
Nov 23 '17 at 6:52
Its GNU Awk 3.1.7
â nabilishes
Nov 23 '17 at 6:54
1
@nabilishes Thanks for the feedback, I can reproduce the same result usinggawk 3.1.7withbash, it seems that the scanning order of array in awk is different in older versions. A safer solution is to replacefor(i in a)with an explicit orderfor(i=1;i<=NF;i++)
â etopylight
Nov 23 '17 at 7:09
Thanks! this solution works like a charm. I previously had the same problem with the array sorting - Here is the link of the same array sorting problem. It is in the comment section. Now i know its due to the version and not environment or interpreter
â nabilishes
Nov 23 '17 at 7:23
1
1
thanks! this somehow help me in understanding lines splitting. However due to the nature of my workstation running on tsch, the arrangement of output is different on my side. It gives out with your first solution
6 4 2 0 0 8â nabilishes
Nov 23 '17 at 6:11
thanks! this somehow help me in understanding lines splitting. However due to the nature of my workstation running on tsch, the arrangement of output is different on my side. It gives out with your first solution
6 4 2 0 0 8â nabilishes
Nov 23 '17 at 6:11
@nabilishes Hmm, that's odd, what's the awk version you are currently using
awk --version?â etopylight
Nov 23 '17 at 6:52
@nabilishes Hmm, that's odd, what's the awk version you are currently using
awk --version?â etopylight
Nov 23 '17 at 6:52
Its GNU Awk 3.1.7
â nabilishes
Nov 23 '17 at 6:54
Its GNU Awk 3.1.7
â nabilishes
Nov 23 '17 at 6:54
1
1
@nabilishes Thanks for the feedback, I can reproduce the same result using
gawk 3.1.7 with bash, it seems that the scanning order of array in awk is different in older versions. A safer solution is to replace for(i in a) with an explicit order for(i=1;i<=NF;i++)â etopylight
Nov 23 '17 at 7:09
@nabilishes Thanks for the feedback, I can reproduce the same result using
gawk 3.1.7 with bash, it seems that the scanning order of array in awk is different in older versions. A safer solution is to replace for(i in a) with an explicit order for(i=1;i<=NF;i++)â etopylight
Nov 23 '17 at 7:09
Thanks! this solution works like a charm. I previously had the same problem with the array sorting - Here is the link of the same array sorting problem. It is in the comment section. Now i know its due to the version and not environment or interpreter
â nabilishes
Nov 23 '17 at 7:23
Thanks! this solution works like a charm. I previously had the same problem with the array sorting - Here is the link of the same array sorting problem. It is in the comment section. Now i know its due to the version and not environment or interpreter
â nabilishes
Nov 23 '17 at 7:23
add a comment |Â
up vote
2
down vote
Alternative Python solution:
python -c 'import sys; f=open(sys.argv[1],"r");
print(" ".join(str(int(d2)-int(d1))
for d1,d2 in zip(next(f).split()[2:], next(f).split()[2:]))); f.close()' file
The output:
8 6 4 2
add a comment |Â
up vote
2
down vote
Alternative Python solution:
python -c 'import sys; f=open(sys.argv[1],"r");
print(" ".join(str(int(d2)-int(d1))
for d1,d2 in zip(next(f).split()[2:], next(f).split()[2:]))); f.close()' file
The output:
8 6 4 2
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Alternative Python solution:
python -c 'import sys; f=open(sys.argv[1],"r");
print(" ".join(str(int(d2)-int(d1))
for d1,d2 in zip(next(f).split()[2:], next(f).split()[2:]))); f.close()' file
The output:
8 6 4 2
Alternative Python solution:
python -c 'import sys; f=open(sys.argv[1],"r");
print(" ".join(str(int(d2)-int(d1))
for d1,d2 in zip(next(f).split()[2:], next(f).split()[2:]))); f.close()' file
The output:
8 6 4 2
answered Nov 23 '17 at 8:31
RomanPerekhrest
22.4k12144
22.4k12144
add a comment |Â
add a comment |Â
up vote
0
down vote
below command also provides same output. l.txt contains the input which you have provided in question
for i in 1..6; do awk -v i="$i" 'BEGINsum=0sum=$i-sumENDprint sum' l.txt ; done| tr "n" " " | awk '$1="";$2="";print $0' | sed "s/^ //g"
output
8 6 4 2
add a comment |Â
up vote
0
down vote
below command also provides same output. l.txt contains the input which you have provided in question
for i in 1..6; do awk -v i="$i" 'BEGINsum=0sum=$i-sumENDprint sum' l.txt ; done| tr "n" " " | awk '$1="";$2="";print $0' | sed "s/^ //g"
output
8 6 4 2
add a comment |Â
up vote
0
down vote
up vote
0
down vote
below command also provides same output. l.txt contains the input which you have provided in question
for i in 1..6; do awk -v i="$i" 'BEGINsum=0sum=$i-sumENDprint sum' l.txt ; done| tr "n" " " | awk '$1="";$2="";print $0' | sed "s/^ //g"
output
8 6 4 2
below command also provides same output. l.txt contains the input which you have provided in question
for i in 1..6; do awk -v i="$i" 'BEGINsum=0sum=$i-sumENDprint sum' l.txt ; done| tr "n" " " | awk '$1="";$2="";print $0' | sed "s/^ //g"
output
8 6 4 2
edited Nov 23 '17 at 12:23
Jeff Schaller
30.7k846104
30.7k846104
answered Nov 23 '17 at 6:44
Praveen Kumar BS
1,010128
1,010128
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%2f406465%2fsubtracting-same-column-between-two-rows-in-awk%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
1
@John1024I managed to output for single column
awk '$temp=$3-prev3; prev3=$3print $temp'. The only limitation is i am using tsch on CentOS. I know tsch is inferior compared to other interpreter but this is what i have to use. I tried this too but didnt get what i wantcat tmpfile | awk 'for(i=3;i<=NF;i++)$temp[i]=$i-prev; prev=$i;print $temp[i]'â nabilishes
Nov 23 '17 at 5:08