How to merge two files with different lengths and columns linux
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have 2 files with different columns and length and I want to merge them as desired output file given below, Please Help!!!
File 1
aa
bb
cc
dd
File 2
ff , 2 , tg12
dd , 3 , tg13
gg , 4 , tg14
hh , 5 , tg15
aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
ii , 9 , tg19
Desired output
aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
dd , 3 , tg13
awk files merge
add a comment |Â
up vote
1
down vote
favorite
I have 2 files with different columns and length and I want to merge them as desired output file given below, Please Help!!!
File 1
aa
bb
cc
dd
File 2
ff , 2 , tg12
dd , 3 , tg13
gg , 4 , tg14
hh , 5 , tg15
aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
ii , 9 , tg19
Desired output
aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
dd , 3 , tg13
awk files merge
format your question to make it readable
â RomanPerekhrest
Nov 5 '17 at 20:11
@steeldriver can you suggest how can I do that?
â Amber
Nov 5 '17 at 20:22
@Amber, is the order of lines by "starting field" matters?
â RomanPerekhrest
Nov 5 '17 at 20:27
@RomanPerekhrest yes the order of line matters
â Amber
Nov 5 '17 at 20:29
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have 2 files with different columns and length and I want to merge them as desired output file given below, Please Help!!!
File 1
aa
bb
cc
dd
File 2
ff , 2 , tg12
dd , 3 , tg13
gg , 4 , tg14
hh , 5 , tg15
aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
ii , 9 , tg19
Desired output
aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
dd , 3 , tg13
awk files merge
I have 2 files with different columns and length and I want to merge them as desired output file given below, Please Help!!!
File 1
aa
bb
cc
dd
File 2
ff , 2 , tg12
dd , 3 , tg13
gg , 4 , tg14
hh , 5 , tg15
aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
ii , 9 , tg19
Desired output
aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
dd , 3 , tg13
awk files merge
edited Nov 5 '17 at 20:26
Thor
11.1k13256
11.1k13256
asked Nov 5 '17 at 19:58
Amber
92
92
format your question to make it readable
â RomanPerekhrest
Nov 5 '17 at 20:11
@steeldriver can you suggest how can I do that?
â Amber
Nov 5 '17 at 20:22
@Amber, is the order of lines by "starting field" matters?
â RomanPerekhrest
Nov 5 '17 at 20:27
@RomanPerekhrest yes the order of line matters
â Amber
Nov 5 '17 at 20:29
add a comment |Â
format your question to make it readable
â RomanPerekhrest
Nov 5 '17 at 20:11
@steeldriver can you suggest how can I do that?
â Amber
Nov 5 '17 at 20:22
@Amber, is the order of lines by "starting field" matters?
â RomanPerekhrest
Nov 5 '17 at 20:27
@RomanPerekhrest yes the order of line matters
â Amber
Nov 5 '17 at 20:29
format your question to make it readable
â RomanPerekhrest
Nov 5 '17 at 20:11
format your question to make it readable
â RomanPerekhrest
Nov 5 '17 at 20:11
@steeldriver can you suggest how can I do that?
â Amber
Nov 5 '17 at 20:22
@steeldriver can you suggest how can I do that?
â Amber
Nov 5 '17 at 20:22
@Amber, is the order of lines by "starting field" matters?
â RomanPerekhrest
Nov 5 '17 at 20:27
@Amber, is the order of lines by "starting field" matters?
â RomanPerekhrest
Nov 5 '17 at 20:27
@RomanPerekhrest yes the order of line matters
â Amber
Nov 5 '17 at 20:29
@RomanPerekhrest yes the order of line matters
â Amber
Nov 5 '17 at 20:29
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
AWK
solution (ordered by sort
command):
awk 'NR==FNR a[$1]; next $1 in a' file1 FS='[[:space:]]*,[[:space:]]' file2 | sort
FS='[[:space:]]*,[[:space:]]'
- field separator that is set forfile2
The output:
aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
dd , 3 , tg13
add a comment |Â
up vote
0
down vote
The simplest solution I could think of:
grep -f file1 file2 | sort
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
AWK
solution (ordered by sort
command):
awk 'NR==FNR a[$1]; next $1 in a' file1 FS='[[:space:]]*,[[:space:]]' file2 | sort
FS='[[:space:]]*,[[:space:]]'
- field separator that is set forfile2
The output:
aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
dd , 3 , tg13
add a comment |Â
up vote
1
down vote
AWK
solution (ordered by sort
command):
awk 'NR==FNR a[$1]; next $1 in a' file1 FS='[[:space:]]*,[[:space:]]' file2 | sort
FS='[[:space:]]*,[[:space:]]'
- field separator that is set forfile2
The output:
aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
dd , 3 , tg13
add a comment |Â
up vote
1
down vote
up vote
1
down vote
AWK
solution (ordered by sort
command):
awk 'NR==FNR a[$1]; next $1 in a' file1 FS='[[:space:]]*,[[:space:]]' file2 | sort
FS='[[:space:]]*,[[:space:]]'
- field separator that is set forfile2
The output:
aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
dd , 3 , tg13
AWK
solution (ordered by sort
command):
awk 'NR==FNR a[$1]; next $1 in a' file1 FS='[[:space:]]*,[[:space:]]' file2 | sort
FS='[[:space:]]*,[[:space:]]'
- field separator that is set forfile2
The output:
aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
dd , 3 , tg13
edited Nov 5 '17 at 20:35
answered Nov 5 '17 at 20:28
RomanPerekhrest
22.5k12145
22.5k12145
add a comment |Â
add a comment |Â
up vote
0
down vote
The simplest solution I could think of:
grep -f file1 file2 | sort
add a comment |Â
up vote
0
down vote
The simplest solution I could think of:
grep -f file1 file2 | sort
add a comment |Â
up vote
0
down vote
up vote
0
down vote
The simplest solution I could think of:
grep -f file1 file2 | sort
The simplest solution I could think of:
grep -f file1 file2 | sort
answered Nov 6 '17 at 8:34
Michael Vehrs
2,17037
2,17037
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%2f402716%2fhow-to-merge-two-files-with-different-lengths-and-columns-linux%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
format your question to make it readable
â RomanPerekhrest
Nov 5 '17 at 20:11
@steeldriver can you suggest how can I do that?
â Amber
Nov 5 '17 at 20:22
@Amber, is the order of lines by "starting field" matters?
â RomanPerekhrest
Nov 5 '17 at 20:27
@RomanPerekhrest yes the order of line matters
â Amber
Nov 5 '17 at 20:29