r/scripting • u/joat2 • Jun 23 '19
Need help with comparing two files and making adjustments to one of the files. Which scripting language is easiest to learn and or would be able to do this?
Background... A program I use doesn't like to merge data itself(no option and developers don't seem like they want to do it) so you have to do it manually. When doing it manually it's just copying the text from one to the end of the other. But in doing this any duplicate entries are ignored and it only uses one of them. So I need a way to compare each one and if found, change a number to be the sum of the two. So if duplicate is found 1 and 2, then change to 3. I put an example below. If any other information is needed ask and will clarify.
Example 1.
File 1
- - id: 445975 - r: 1
- - id: 445976 - r: 1
- - id: 445977 - r: 2
File 2
- - id: 445977 - r: 1
- - id: 445978 - r: 1 - f: 1
- - id: 445987 - r: 1
Output, or adjust one of the files. Below is file 1, merged with file 2.
- - id: 445975 - r: 1
- - id: 445976 - r: 1
- - id: 445977 - r: 3
- - id: 445978 - r: 1 - f: 1
- - id: 445987 - r: 1
Any help will be greatly appreciated.
Edit
Found a solution thank you everyone.
4
u/Shadow_Thief Jun 23 '19
It depends on what operating system you're using.
2
u/joat2 Jun 23 '19
Okay everyone, I found a solution it's not scripting related but I will take everything here and try to still figure it out later when I have the time.
The program mentioned before suddenly started working? By that I mean duplicate entries on the txt file are now counted? I tried this exact same thing months ago and didn't work, now it does?
Hopefully I haven't wasted much of anyone's time. I still think this could be a useful case to figure out on my own when I have the time needed to do it.
I really appreciate all the help.
2
u/Lee_Dailey Jun 23 '19
howdy joat2,
powershell has a
Group-Objectcmdlet that can group those lines by theIDsection. once grouped, you can iterate thru them and sum up thersections. finally, you can write the new file with one line per group and the newrvalue.it's likely even easier in python, but i haven't kept up with it in years. [blush]
take care,
lee
2
u/Lee_Dailey Jun 23 '19 edited Jun 23 '19
howdy joat2,
i got interested and made one solution in powershell. [grin]
this presumes your samples are complete lines. if not then the pattern matching likely won't work.
output to screen ...
file content ...
i thot about sorting the lines by
ID:but decided you likely had no need for that since you didn't mention it.take care,
lee