简体   繁体   中英

How to join to files in linux?

So I basically have a file named vendors and another named products and I have to join them based on the vendors code, outputting the product's code and name and the vendor's name in that order and append it to a file called lab4.txt. The question also says to "Ensure that a line is produced for each unpairable line in file vendors and text "Not Available" is used to replace the empty fields for the unpairable lines". I've been trying to to this with the join command but just can get it right. Here is what the files look like:

Vendors:

1201:Cromwell Interiors
1221:Design Extras Inc.
1320:Piedmont Plastics Inc.
1340:Morgan Catering Service Ltd.
1350:Pullman Elevators
1360:Johnson Office Products

The other file called products:

S0107:Lobby Furniture:1201
S0109:Ballroom Specialties:1221
S0110:Poolside Carts:1320
S0130:Formal Dining Specials:1340
S0201:Reservation Logs:1410

Result should be:

S0107:Lobby Furniture:Cromwell Interiors
S0109:Ballroom Specialties:Design Extras Inc.
S0110:Poolside Carts:Piedmont Plastics Inc.
S0130:Formal Dining Specials:Morgan Catering Service Ltd.
Not Available:Not Available:Pullman Elevators
Not Available:Not Available:Johnson Office Products

Corrected Answer

Ok. Here is how to get your desired output.

join -a2 -o 1.1,1.2,2.2 -e "Not Available" -t':' -1 3 -2 1 products vendors

  • -a2 print unpairable lines from file 2
  • -o 1.1,1.2,2.2 output format is FILE1FIELD1:FILE1FIELD2:FILE2FIELD2
  • -e "Not Available" replace missing input fields with Not Available. Requires that -o also be specified.
  • -t':' use semicolon as column delimiter
  • -1 3 -2 1 join based on column 3 of file 1 and column 1 of file 2.
  • products vendors files to join; file 1 is products, file 2 is vendors

Command Output:

[larntz@dido:/home/larntz/temp$ join -a2 -o 1.1,1.2,2.2 -e "Not Available" -t':' -1 3 -2 1 products vendors

S0107:Lobby Furniture:Cromwell Interiors
S0109:Ballroom Specialties:Design Extras Inc.
S0110:Poolside Carts:Piedmont Plastics Inc.
S0130:Formal Dining Specials:Morgan Catering Service Ltd.
Not Available:Not Available:Pullman Elevators
Not Available:Not Available:Johnson Office Products


Original Incorrect Answer

Given the two files in your post this works with join version 8.30 (Arch Linux).

join -a1 -a2 -e"Not Available" -t':' -1 1 -2 3 vendors products
  • -a1 -a2 tells join to print all lines from both files
  • -e"Not Available" tells join to replace empty fields with Not Available. Unfortunately, this is not working for some reason. EDIT -e only works if -o (the output format) is also specified.
  • -t':' tells join to use semicolon as the field separator
  • -1 1 tells join to use field number 1 in file 1 (vendors) to match with file 2
  • -2 3 tells join to use field number 3 in file 2 (products) to match with file 1
  • vendors products are the two files to join, order matters based on the -1 and -2 parameters.

Command Output:

[larntz@dido:/home/larntz/temp$ join -a1 -a2 -e"Not Available"  -t':' -1 1 -2 3 vendors products
1201:Cromwell Interiors:S0107:Lobby Furniture
1221:Design Extras Inc.:S0109:Ballroom Specialties
1320:Piedmont Plastics Inc.:S0110:Poolside Carts
1340:Morgan Catering Service Ltd.:S0130:Formal Dining Specials
1350:Pullman Elevators
1360:Johnson Office Products
1410:S0201:Reservation Logs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM