Avatar of Steve Jennings
Steve Jennings

asked on 

Merging two files with Perl

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.


Shell ScriptingPerl

Avatar of undefined
Last Comment
Steve Jennings

8/22/2022 - Mon