We help IT Professionals succeed at work.
Get Started

Merging two files with Perl

Steve Jennings
Last Modified: 2017-04-20
I have 2 files that I need to compare and merge. File1 has 3 fields: device_name, status, days_in_cur_status. File1 is the "master status" file.

File2 has 2 fields: device_name, status. File2 is the file produced from checking the current status.

- File1 -
deviceA available 10
deviceB available 8
deviceC unavailable 3

- File2 -
deviceA available
deviceB unavailable
deviceC available
deviceD available

After comparing/merging the files I'd like to produce File3 which should look like:

deviceA available 11
deviceB unavailable 0
deviceC available 0
deviceD available 0

I can't figure out how to open both files and compare a line in file1 to each line in file2. I began by reading both files into an array and then iterating through the arrays, but that's not working. I'd be glad to post the code, but I'm pretty sure that having to read thru file 2 once for each line in file 1 is, well, dumb.


Watch Question
This problem has been solved!
Unlock 2 Answers and 5 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE