How can I do filtering between two matrix? Using awk?sed?grep?Or what else?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
File1:
91 23 56 44 87 77
99 34 56 22 22 95
41 88 26 79 60 27
95 55 66 69 92 25
File2:
pass fail pass pass pass fail
pass fail pass fail fail pass
pass pass fail pass pass fail
pass pass fail pass pass fail
As I want to sum up the total fail marks for each row, here is the expected output.
output:
100
78
53
91
I would like to ask that how can I do the filtering on file1 based on the word "fail" in file2 in order to get the sum of fail marks.
linux shell-script text-processing
add a comment |Â
up vote
2
down vote
favorite
File1:
91 23 56 44 87 77
99 34 56 22 22 95
41 88 26 79 60 27
95 55 66 69 92 25
File2:
pass fail pass pass pass fail
pass fail pass fail fail pass
pass pass fail pass pass fail
pass pass fail pass pass fail
As I want to sum up the total fail marks for each row, here is the expected output.
output:
100
78
53
91
I would like to ask that how can I do the filtering on file1 based on the word "fail" in file2 in order to get the sum of fail marks.
linux shell-script text-processing
What is producing these two files and can't that program do this?
– Kusalananda
3 hours ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
File1:
91 23 56 44 87 77
99 34 56 22 22 95
41 88 26 79 60 27
95 55 66 69 92 25
File2:
pass fail pass pass pass fail
pass fail pass fail fail pass
pass pass fail pass pass fail
pass pass fail pass pass fail
As I want to sum up the total fail marks for each row, here is the expected output.
output:
100
78
53
91
I would like to ask that how can I do the filtering on file1 based on the word "fail" in file2 in order to get the sum of fail marks.
linux shell-script text-processing
File1:
91 23 56 44 87 77
99 34 56 22 22 95
41 88 26 79 60 27
95 55 66 69 92 25
File2:
pass fail pass pass pass fail
pass fail pass fail fail pass
pass pass fail pass pass fail
pass pass fail pass pass fail
As I want to sum up the total fail marks for each row, here is the expected output.
output:
100
78
53
91
I would like to ask that how can I do the filtering on file1 based on the word "fail" in file2 in order to get the sum of fail marks.
linux shell-script text-processing
linux shell-script text-processing
edited 3 hours ago
asked 4 hours ago
Owen
233
233
What is producing these two files and can't that program do this?
– Kusalananda
3 hours ago
add a comment |Â
What is producing these two files and can't that program do this?
– Kusalananda
3 hours ago
What is producing these two files and can't that program do this?
– Kusalananda
3 hours ago
What is producing these two files and can't that program do this?
– Kusalananda
3 hours ago
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
Here is my awk
approach:
awk 'NR==FNRfor(i=1;i<=NF;i++) a[NR"-"i]=$i;next
for(j=1;j<=NF;j++) if($j=="fail") b[FNR]+=a[FNR"-"j]
ENDfor(k in b) print b[k]' file1 file2
The output doesn't agree with yours on the 3rd row:
100
78
53
91
add a comment |Â
up vote
1
down vote
I guess using an Awk
script would make this requirement a bit easy to solve. Do something like below. I guess its a bit slower than now posted jimmij's answer
#!/usr/bin/awk -f
FNR == NR
for(i=1;i<=NF;i++)
if ( $i == "fail")
idxArray[FNR] = (idxArray[FNR]) ? (idxArray[FNR]" "i):(i)
next
delete Array
delete Line
i=""
j=""
sum=""
n=split(idxArray[FNR],Array," ")
l=split($0,Line," ")
for (i=1;i<=n;i++)
for (j=1;j<=l;j++)
if (Array[i] == j )
sum += Line[j]
print sum
and run the script as
awk -f script.awk file2 file1
add a comment |Â
up vote
0
down vote
I would use a matrix language for such a task, e.g. GNU Octave.
Assuming you converted the pass/fail file into numerical values, e.g.:
sed 's/pass/1/g; s/fail/0/g' passfail > passfail.nums
You can now do the following:
marks = dlmread('marks');
passfail = dlmread('passfail.nums');
for i = 1:size(marks)(1)
sum(marks(i,:)(passfail(i,:) == 0))
end
Output:
ans = 100
ans = 78
ans = 53
ans = 91
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Here is my awk
approach:
awk 'NR==FNRfor(i=1;i<=NF;i++) a[NR"-"i]=$i;next
for(j=1;j<=NF;j++) if($j=="fail") b[FNR]+=a[FNR"-"j]
ENDfor(k in b) print b[k]' file1 file2
The output doesn't agree with yours on the 3rd row:
100
78
53
91
add a comment |Â
up vote
2
down vote
Here is my awk
approach:
awk 'NR==FNRfor(i=1;i<=NF;i++) a[NR"-"i]=$i;next
for(j=1;j<=NF;j++) if($j=="fail") b[FNR]+=a[FNR"-"j]
ENDfor(k in b) print b[k]' file1 file2
The output doesn't agree with yours on the 3rd row:
100
78
53
91
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Here is my awk
approach:
awk 'NR==FNRfor(i=1;i<=NF;i++) a[NR"-"i]=$i;next
for(j=1;j<=NF;j++) if($j=="fail") b[FNR]+=a[FNR"-"j]
ENDfor(k in b) print b[k]' file1 file2
The output doesn't agree with yours on the 3rd row:
100
78
53
91
Here is my awk
approach:
awk 'NR==FNRfor(i=1;i<=NF;i++) a[NR"-"i]=$i;next
for(j=1;j<=NF;j++) if($j=="fail") b[FNR]+=a[FNR"-"j]
ENDfor(k in b) print b[k]' file1 file2
The output doesn't agree with yours on the 3rd row:
100
78
53
91
answered 4 hours ago
jimmij
30.1k867102
30.1k867102
add a comment |Â
add a comment |Â
up vote
1
down vote
I guess using an Awk
script would make this requirement a bit easy to solve. Do something like below. I guess its a bit slower than now posted jimmij's answer
#!/usr/bin/awk -f
FNR == NR
for(i=1;i<=NF;i++)
if ( $i == "fail")
idxArray[FNR] = (idxArray[FNR]) ? (idxArray[FNR]" "i):(i)
next
delete Array
delete Line
i=""
j=""
sum=""
n=split(idxArray[FNR],Array," ")
l=split($0,Line," ")
for (i=1;i<=n;i++)
for (j=1;j<=l;j++)
if (Array[i] == j )
sum += Line[j]
print sum
and run the script as
awk -f script.awk file2 file1
add a comment |Â
up vote
1
down vote
I guess using an Awk
script would make this requirement a bit easy to solve. Do something like below. I guess its a bit slower than now posted jimmij's answer
#!/usr/bin/awk -f
FNR == NR
for(i=1;i<=NF;i++)
if ( $i == "fail")
idxArray[FNR] = (idxArray[FNR]) ? (idxArray[FNR]" "i):(i)
next
delete Array
delete Line
i=""
j=""
sum=""
n=split(idxArray[FNR],Array," ")
l=split($0,Line," ")
for (i=1;i<=n;i++)
for (j=1;j<=l;j++)
if (Array[i] == j )
sum += Line[j]
print sum
and run the script as
awk -f script.awk file2 file1
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I guess using an Awk
script would make this requirement a bit easy to solve. Do something like below. I guess its a bit slower than now posted jimmij's answer
#!/usr/bin/awk -f
FNR == NR
for(i=1;i<=NF;i++)
if ( $i == "fail")
idxArray[FNR] = (idxArray[FNR]) ? (idxArray[FNR]" "i):(i)
next
delete Array
delete Line
i=""
j=""
sum=""
n=split(idxArray[FNR],Array," ")
l=split($0,Line," ")
for (i=1;i<=n;i++)
for (j=1;j<=l;j++)
if (Array[i] == j )
sum += Line[j]
print sum
and run the script as
awk -f script.awk file2 file1
I guess using an Awk
script would make this requirement a bit easy to solve. Do something like below. I guess its a bit slower than now posted jimmij's answer
#!/usr/bin/awk -f
FNR == NR
for(i=1;i<=NF;i++)
if ( $i == "fail")
idxArray[FNR] = (idxArray[FNR]) ? (idxArray[FNR]" "i):(i)
next
delete Array
delete Line
i=""
j=""
sum=""
n=split(idxArray[FNR],Array," ")
l=split($0,Line," ")
for (i=1;i<=n;i++)
for (j=1;j<=l;j++)
if (Array[i] == j )
sum += Line[j]
print sum
and run the script as
awk -f script.awk file2 file1
edited 3 hours ago
answered 3 hours ago


Inian
3,423822
3,423822
add a comment |Â
add a comment |Â
up vote
0
down vote
I would use a matrix language for such a task, e.g. GNU Octave.
Assuming you converted the pass/fail file into numerical values, e.g.:
sed 's/pass/1/g; s/fail/0/g' passfail > passfail.nums
You can now do the following:
marks = dlmread('marks');
passfail = dlmread('passfail.nums');
for i = 1:size(marks)(1)
sum(marks(i,:)(passfail(i,:) == 0))
end
Output:
ans = 100
ans = 78
ans = 53
ans = 91
add a comment |Â
up vote
0
down vote
I would use a matrix language for such a task, e.g. GNU Octave.
Assuming you converted the pass/fail file into numerical values, e.g.:
sed 's/pass/1/g; s/fail/0/g' passfail > passfail.nums
You can now do the following:
marks = dlmread('marks');
passfail = dlmread('passfail.nums');
for i = 1:size(marks)(1)
sum(marks(i,:)(passfail(i,:) == 0))
end
Output:
ans = 100
ans = 78
ans = 53
ans = 91
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I would use a matrix language for such a task, e.g. GNU Octave.
Assuming you converted the pass/fail file into numerical values, e.g.:
sed 's/pass/1/g; s/fail/0/g' passfail > passfail.nums
You can now do the following:
marks = dlmread('marks');
passfail = dlmread('passfail.nums');
for i = 1:size(marks)(1)
sum(marks(i,:)(passfail(i,:) == 0))
end
Output:
ans = 100
ans = 78
ans = 53
ans = 91
I would use a matrix language for such a task, e.g. GNU Octave.
Assuming you converted the pass/fail file into numerical values, e.g.:
sed 's/pass/1/g; s/fail/0/g' passfail > passfail.nums
You can now do the following:
marks = dlmread('marks');
passfail = dlmread('passfail.nums');
for i = 1:size(marks)(1)
sum(marks(i,:)(passfail(i,:) == 0))
end
Output:
ans = 100
ans = 78
ans = 53
ans = 91
answered 11 mins ago
Thor
11.3k13357
11.3k13357
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%2f480520%2fhow-can-i-do-filtering-between-two-matrix-using-awksedgrepor-what-else%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
What is producing these two files and can't that program do this?
– Kusalananda
3 hours ago