Combine two file into one file
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
Suppose I have 2 Files, ABC.txt & DEF.txt with the data shown below as an example:
ABC.txt:
abc 14
dka 1
def 51
DEF.txt:
def 12
ckd 41
I want to grep column 1& 2 from both files and write into a third file so that third file contain both file without any repeated column 1 and thire value put for two file and put zero for values that didn't exit in files. How can it be done?
My expected output is (output:
ABC DEF
abc 14 0
ckd 0 41
def 51 12
dka 1 0
text-processing command-line
add a comment |Â
up vote
0
down vote
favorite
Suppose I have 2 Files, ABC.txt & DEF.txt with the data shown below as an example:
ABC.txt:
abc 14
dka 1
def 51
DEF.txt:
def 12
ckd 41
I want to grep column 1& 2 from both files and write into a third file so that third file contain both file without any repeated column 1 and thire value put for two file and put zero for values that didn't exit in files. How can it be done?
My expected output is (output:
ABC DEF
abc 14 0
ckd 0 41
def 51 12
dka 1 0
text-processing command-line
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Suppose I have 2 Files, ABC.txt & DEF.txt with the data shown below as an example:
ABC.txt:
abc 14
dka 1
def 51
DEF.txt:
def 12
ckd 41
I want to grep column 1& 2 from both files and write into a third file so that third file contain both file without any repeated column 1 and thire value put for two file and put zero for values that didn't exit in files. How can it be done?
My expected output is (output:
ABC DEF
abc 14 0
ckd 0 41
def 51 12
dka 1 0
text-processing command-line
Suppose I have 2 Files, ABC.txt & DEF.txt with the data shown below as an example:
ABC.txt:
abc 14
dka 1
def 51
DEF.txt:
def 12
ckd 41
I want to grep column 1& 2 from both files and write into a third file so that third file contain both file without any repeated column 1 and thire value put for two file and put zero for values that didn't exit in files. How can it be done?
My expected output is (output:
ABC DEF
abc 14 0
ckd 0 41
def 51 12
dka 1 0
text-processing command-line
edited Dec 15 '17 at 13:48
0xC0000022L
7,1051359106
7,1051359106
asked Dec 15 '17 at 13:41
fvosta
11
11
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
down vote
$ join -j 1 -a 1 -a 2 -o 0,1.2,2.2 -e 0 <(sort ABC.txt) <(sort DEF.txt)
abc 14 0
ckd 0 41
def 51 12
dka 1 0
The header and column spacing are left as an exercise.
add a comment |Â
up vote
3
down vote
Awk
solution:
awk 'BEGIN
OFS="t"; print "", "ABC", "DEF"
NR==FNR a[$1]=$2; next
if ($1 in a) v=a[$1]; delete a[$1]
$3 = v+0
1;
END for(i in a) print i, 0, a[i] ' DEF.txt ABC.txt
The output:
ABC DEF
abc 14 0
dka 1 0
def 51 12
ckd 0 41
add a comment |Â
up vote
2
down vote
GNU datamash has a cross-tabulation (pivot table) option that is quite nice for this sort of thing - although your data would need some pre-processing:
awk 'print $0, substr(FILENAME,1,length(FILENAME)-4)' ABC.txt DEF.txt |
datamash -Ws --filler='0' crosstab 1,3 unique 2
ABC DEF
abc 14 0
ckd 0 41
def 51 12
dka 1 0
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
$ join -j 1 -a 1 -a 2 -o 0,1.2,2.2 -e 0 <(sort ABC.txt) <(sort DEF.txt)
abc 14 0
ckd 0 41
def 51 12
dka 1 0
The header and column spacing are left as an exercise.
add a comment |Â
up vote
4
down vote
$ join -j 1 -a 1 -a 2 -o 0,1.2,2.2 -e 0 <(sort ABC.txt) <(sort DEF.txt)
abc 14 0
ckd 0 41
def 51 12
dka 1 0
The header and column spacing are left as an exercise.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
$ join -j 1 -a 1 -a 2 -o 0,1.2,2.2 -e 0 <(sort ABC.txt) <(sort DEF.txt)
abc 14 0
ckd 0 41
def 51 12
dka 1 0
The header and column spacing are left as an exercise.
$ join -j 1 -a 1 -a 2 -o 0,1.2,2.2 -e 0 <(sort ABC.txt) <(sort DEF.txt)
abc 14 0
ckd 0 41
def 51 12
dka 1 0
The header and column spacing are left as an exercise.
answered Dec 15 '17 at 14:25
glenn jackman
46.7k265103
46.7k265103
add a comment |Â
add a comment |Â
up vote
3
down vote
Awk
solution:
awk 'BEGIN
OFS="t"; print "", "ABC", "DEF"
NR==FNR a[$1]=$2; next
if ($1 in a) v=a[$1]; delete a[$1]
$3 = v+0
1;
END for(i in a) print i, 0, a[i] ' DEF.txt ABC.txt
The output:
ABC DEF
abc 14 0
dka 1 0
def 51 12
ckd 0 41
add a comment |Â
up vote
3
down vote
Awk
solution:
awk 'BEGIN
OFS="t"; print "", "ABC", "DEF"
NR==FNR a[$1]=$2; next
if ($1 in a) v=a[$1]; delete a[$1]
$3 = v+0
1;
END for(i in a) print i, 0, a[i] ' DEF.txt ABC.txt
The output:
ABC DEF
abc 14 0
dka 1 0
def 51 12
ckd 0 41
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Awk
solution:
awk 'BEGIN
OFS="t"; print "", "ABC", "DEF"
NR==FNR a[$1]=$2; next
if ($1 in a) v=a[$1]; delete a[$1]
$3 = v+0
1;
END for(i in a) print i, 0, a[i] ' DEF.txt ABC.txt
The output:
ABC DEF
abc 14 0
dka 1 0
def 51 12
ckd 0 41
Awk
solution:
awk 'BEGIN
OFS="t"; print "", "ABC", "DEF"
NR==FNR a[$1]=$2; next
if ($1 in a) v=a[$1]; delete a[$1]
$3 = v+0
1;
END for(i in a) print i, 0, a[i] ' DEF.txt ABC.txt
The output:
ABC DEF
abc 14 0
dka 1 0
def 51 12
ckd 0 41
answered Dec 15 '17 at 14:07
RomanPerekhrest
22.4k12145
22.4k12145
add a comment |Â
add a comment |Â
up vote
2
down vote
GNU datamash has a cross-tabulation (pivot table) option that is quite nice for this sort of thing - although your data would need some pre-processing:
awk 'print $0, substr(FILENAME,1,length(FILENAME)-4)' ABC.txt DEF.txt |
datamash -Ws --filler='0' crosstab 1,3 unique 2
ABC DEF
abc 14 0
ckd 0 41
def 51 12
dka 1 0
add a comment |Â
up vote
2
down vote
GNU datamash has a cross-tabulation (pivot table) option that is quite nice for this sort of thing - although your data would need some pre-processing:
awk 'print $0, substr(FILENAME,1,length(FILENAME)-4)' ABC.txt DEF.txt |
datamash -Ws --filler='0' crosstab 1,3 unique 2
ABC DEF
abc 14 0
ckd 0 41
def 51 12
dka 1 0
add a comment |Â
up vote
2
down vote
up vote
2
down vote
GNU datamash has a cross-tabulation (pivot table) option that is quite nice for this sort of thing - although your data would need some pre-processing:
awk 'print $0, substr(FILENAME,1,length(FILENAME)-4)' ABC.txt DEF.txt |
datamash -Ws --filler='0' crosstab 1,3 unique 2
ABC DEF
abc 14 0
ckd 0 41
def 51 12
dka 1 0
GNU datamash has a cross-tabulation (pivot table) option that is quite nice for this sort of thing - although your data would need some pre-processing:
awk 'print $0, substr(FILENAME,1,length(FILENAME)-4)' ABC.txt DEF.txt |
datamash -Ws --filler='0' crosstab 1,3 unique 2
ABC DEF
abc 14 0
ckd 0 41
def 51 12
dka 1 0
answered Dec 15 '17 at 15:16
steeldriver
31.7k34979
31.7k34979
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%2f411061%2fcombine-two-file-into-one-file%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