Computing.Net > Forums > Unix > Using a script with UNIX commands t

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Using a script with UNIX commands t

Reply to Message Icon

Name: Kevin
Date: September 10, 2002 at 16:50:53 Pacific
OS: ??
CPU/Ram: ??
Comment:

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




Sponsored Link
Ads by Google

Response Number 1
Name: Jerry Lemieux
Date: September 10, 2002 at 18:54:32 Pacific
Reply:

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


0
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Using a script with UNIX commands t

padding a number with leading zeros www.computing.net/answers/unix/padding-a-number-with-leading-zeros/5218.html

facing problem in writting a script www.computing.net/answers/unix/facing-problem-in-writting-a-script/7831.html

remote login using a script www.computing.net/answers/unix/remote-login-using-a-script/3776.html