Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I need help with merging two files with matching data. I know very little about writing scripts so please answer at the most granular level if possible. All help will be greatly appreciated.
I am trying to write a script that will take two files (A&B) and extract the needed and matching data to a third file(C).
File A contains detail data with no line numbers.
File B contains the line number of the detail line in file A in the second pipe delimited field that has corresponding information.
The result I am looking for is a file C that contains both file A and file B information on for the matching lines only.This is what I think I need to do, but I don’t know how to do it.
1. Have the script receive both file names.
2. Write a line number to each of the lines in the A file. (Use the wr command maybe?)
3. Do a grep on the B file, so it contains only the lines that have “Error” in the line.
4. Use the join command to match the line number that I have added in the A file to the matching line number in the second pipe delimited field of the B file to create a C file that contains the line information that has been combined from both files.Once again if you could break it down to the lines in the script that would be great. I know very little about writing scripts and using UNIX command.
Thanks for your help.
Kevin

wr is a kernel function used for drivers. You can't use it in a script. There are many ways to get your line numbers into fileA. The nl command will put line numbers into a file. Try this command on your file:
nl -nrz -v5 -i5 filename
This will number your file (starting at 5 and incrementing by 5 and does not number blank lines) and add leading zeros. The man pages will give you more information. If this does the trick, you can use:
nl -nrz -v5 -i5 filename > outFile
To create a copy of your file with line numbers included.
Without seeing a short sample of the 2 files you are working with, it is difficult to recommend a solution. Might want to post a couple of lines from both files so the script writers here can give you a sound recommendation. Using grep and join might work, depending on how the data is structured. A simple loop using a pattern match (probably with awk) might be a good solution. However, you will get a better recommendation if you post a couple of lines from each file. Change the data to protect the innocent if the data is sensitive.
Jerry

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |