2

I am trying join two files by one common column (1ª) between two file (File1 and file2), resulting in file 3, but the resultant file is not working. Can someone help me? I used this command:

awk 'NR==FNR {h[$1] = $0; next} {print h[$1],$0}' file1 file2 > file3

File 1.

1 1767 0 1986 28061997 1 1 1 0 29031998 972 34176 9 1 9 9 55 97 42 1 0

2 1876 0 1986 25051995 1 1 1 0 22072000 952 30438 1 1 3 9 1009 95 25 4 0

3 5878 0 1986 16071996 1 1 1 0 22071997 963 30438 4 1 3 9 1009 96 40 4 0

File 2:

1 0 5 8 25031998 2 11 1071997 943 11 1101997 944 11 1011998 951 18 1011998 95 BX1001337 AX54651 BR909511 0

2 0 53 8 19021996 2 8 22031996 6 8 27051996 1 14 27051996 2 7 27051996 2 15 27051996 2 8 21021997 2 8 22031997 6 8 20041997 1 14 20041997 3 15 20041997 3 8 22071997 6 8 28091998 1 14 28091998 4 15 28091998 4 8 25061999  2 8 22091999 6 8 7032000 1 14 7032000 5 15 7032000 3 16 7032000 1 11 1071995     
923 11 1101995 924 11 1011996 931 18 1011996 93 11 1041996 932 11 1071996 933 11 1101996 934 11 1011997 941 18 1011997 94 11 1041997 942 11 1071997 943 11 1101997 944 11 1011998 951 18 1011998 95 11 1041998 952 17 1041998 1005 11 1071998 953 17 1071998 1007 11 1101998 954 11 1011999 961 18 1011999 96 11 1041999 962 17 1041999 1009 11 1071999 963 17 1071999 1007 11 1101999 964 17 1101999 1003 11 1012000 971 18 1012000 97 17 1012000 1005 11 1042000 972 11 1072000 973 BR1001881 AX62116 BR872756 0

3 0 6 8 12041997 2 11 1101996 934 11 1011997 941 18 1011997 94 11 1041997 942 11 1071997 943 BR1003576 AX52602 BR830819 0

File3. It’s working wrong.Is putting the file 2 in 2ª line..

1 1767 0 1986 28061997 1 1 1 0 29031998 972 34176 9 1 9 9 55 97 42 1 0
1 1 0 5 8 25031998 2 11 1071997 943 11 1101997 944 11 1011998 951 18 1011998 95 BR1001337 AX54651 BR909511 0

2 1876 0 1986 25051995 1 1 1 0 22072000 952 30438 1 1 3 9 1009 95 25 4 0
2 4 0 53 8 19021996 2 8 22031996 6 8 27051996 1 14 27051996 2 7 27051996 2 15 27051996 2 8 21021997 2 8 22031997 6 8 20041997 1 14 20041997 3 15 20041997 3 8 22071997 6 8 28091998 1 14 28091998 4 15 28091998 4 8 25061999  2 8 22091999 6 8 7032000 1 14 7032000 5 15 7032000 3 16 7032000 1 11 1071995     
923 11 1101995 924 11 1011996 931 18 1011996 93 11 1041996 932 11 1071996 933 11 1101996 934 11 1011997 941 18 1011997 94 11 1041997 942 11 1071997 943 11 1101997 944 11 1011998 951 18 1011998 95 11 1041998 952 17 1041998 1005 11 1071998 953 17 1071998 1007 11 1101998 954 11 1011999 961 18     
1011999 96 11 1041999 962 17 1041999 1009 11 1071999 963 17 1071999 1007 11 1101999 964 17 1101999 1003 11 1012000 971 18 1012000 97 17 1012000 1005 11 1042000 972 11 1072000 973 BR1001881 AX62116 BR872756 0

3 5878 0 1986 16071996 1 1 1 0 22071997 963 30438 4 1 3 9 1009 96 40 4 0
3 4 0 6 8 12041997 2 11 1101996 934 11 1011997 941 18 1011997 94 11 1041997 942 11 1071997 943 BR1003576 AX52602 BR830819 0

File3. I would like that:

1 1767 0 1986 28061997 1 1 1 0 29031998 972 34176 9 1 9 9 55 97 42 1 0 0 5 8 25031998 2 11 1071997 943 11 1101997 944 11 1011998 951 18 1011998 95 BR1001337 AX54651 BR909511 0

2 1876 0 1986 25051995 1 1 1 0 22072000 952 30438 1 1 3 9 1009 95 25 4 0 0 53 8 19021996 2 8 22031996 6 8 27051996 1 14 27051996 2 7 27051996 2 15 27051996 2 8 21021997 2 8 22031997 6 8 20041997 1 14 20041997 3 15 20041997 3 8 22071997 6 8 28091998 1 14 28091998 4 15 28091998 4 8 25061999 2 8 22091999 6 8 7032000 1 14 7032000 5 15 7032000 3 16 7032000 1 11 1071995 923   
11 1101995 924 11 1011996 931 18 1011996 93 11 1041996 932 11 1071996 933 11 1101996 934 11 1011997 941 18 1011997 94 11 1041997 942 11 1071997 943 11 1101997 944 11 1011998 951 18 1011998 95 11 1041998 952 17 1041998 1005 11 1071998 953 17 1071998 1007 11 1101998 954 11 1011999 961 18 1011999 96 11 1041999 962 17 1041999 1009 11 1071999 963 17 1071999 1007 11 1101999 964 17 1101999 1003 11 1012000 971 18 1012000 97 17 1012000 1005 11 1042000 972 11 1072000 973 BR1001881 AX62116 BR872756 0

3 5878 0 1986 16071996 1 1 1 0 22071997 963 30438 4 1 3 9 1009 96 40 4 0 0 6 8 12041997 2 11 1101996 934 11 1011997 941 18 1011997 94 11 1041997 942 11 1071997 943 BR1003576 AX52602 BR830819 0
  • Run a dos2unix on your files before attempting your command – iruvar Jun 4 '16 at 16:16
  • Thanks, was this the problem (dos2unix)..And I use the command the the other guys wrote below! – Elisandra Lurdes Kern Jun 4 '16 at 17:32
0

Try this one liner!. awk 'FNR==NR{A[$1]=$0;next}{line="";for(i=2;i<=NF;i++)line=line $i" ";sub(" $","",line);if ($1 in A)print A[$1]" "line;}' file1.txt file2.txt > file3

Out

1 1767 0 1986 28061997 1 1 1 0 29031998 972 34176 9 1 9 9 55 97 42 1 0 0 5 8 25031998 2 11 1071997 943 11 1101997 944 11 1011998 951 18 1011998 95 BX1001337 AX54651 BR909511 0
2 1876 0 1986 25051995 1 1 1 0 22072000 952 30438 1 1 3 9 1009 95 25 4 0 0 53 8 19021996 2 8 22031996 6 8 27051996 1 14 27051996 2 7 27051996 2 15 27051996 2 8 21021997 2 8 22031997 6 8 20041997 1 14 20041997 3 15 20041997  3 8 22071997 6 8 28091998 1 14 28091998 4 15 28091998 4 8 25061999 2 8 22091999 6 8 7032000 1 14 7032000 5 15 7032000 3 16 7032000 1 11 1071995 923 11 1101995 924 11 1011996 931 18 1011996 93 11 1041996 932 11 1071996 933 11 1101996 934 11 1011997 941 18 1011997 94 11 1041997 942 11 1071997 943 11 1101997 944 11 1011998 951 18 1011998 95 11 1041998 952 17 1041998 1005 11 1071998 953 17 1071998 1007 11 1101998 954 11 1011999 961 18 1011999 96 11 1041999 962 17 1041999 1009 11 1071999 963 17 1071999 1007 11 1101999 964 17 1101999 1003 11 1012000 971 18 1012000 97 17 1012000 1005 11 1042000 972 11 1072000 973 BR1001881 AX62116 BR872756 0
3 5878 0 1986 16071996 1 1 1 0 22071997 963 30438 4 1 3 9 1009 96 40 4 0 0 6 8 12041997 2 11 1101996 934 11 1011997 941 18 1011997 94 11 1041997 942 11 1071997 943 BR1003576 AX52602 BR830819 0

Saves in A lines from file1 by first column, starts reading file2, skips first column and saves fields in line, if first column in A concatenates both lines in one line and prints.

  • Thanks, but It keeps saving the wrong way.. – Elisandra Lurdes Kern Jun 4 '16 at 16:04
  • So, line two from file2 begins with 2 and ends with BR872756 0? – Firefly Jun 4 '16 at 16:18
  • Keep going wrong.. 1 1767 0 1986 28061997 1 1 1 0 29031998 972 34176 9 1 9 9 55 97 42 1 0 0 5 8 25031998 2 11 1071997 943 11 1101997 944 11 1011998 951 18 1011998 95 BR1001337 AX54651 BR909511 0 is separing in two lines.. – Elisandra Lurdes Kern Jun 4 '16 at 17:02
  • Managed to get the correct file using your script. It only worked after I rode the dos2unix command. Thanks your help! – Elisandra Lurdes Kern Jun 4 '16 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.