1

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
  • 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
1

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 for file2

The output:

aa , 6 , tg16
bb , 7 , tg17
cc , 8 , tg18
dd , 3 , tg13
0

The simplest solution I could think of:

grep -f file1 file2 | sort

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.