1748095 Members
5905 Online
108758 Solutions
New Discussion юеВ

Re: File Handling

 
SOLVED
Go to solution
Panpalia
Occasional Advisor

File Handling

Hi,
I want to read one file and fetch data from other file. please see the example
file A contain employee id and file B contain employee details along with employee id.
So i want to fetch record from file B by reading file A.

Thanks in advanced.
8 REPLIES 8
Leif Halvarsson_2
Honored Contributor
Solution

Re: File Handling

Hi,

Have a look at the "join" command (man join),
OFC_EDM
Respected Contributor

Re: File Handling

File A
-------------------
employeeID otherinfo

File B
-------------------
employeeID firstname lastname favouritehobby


cat | \
while read ID OTHERSTUFF
do

grep "${ID}" /path/to/FileB

done

My syntax may not be bang on. But this will simply read File A line by line and put the first field into variable ID.

Then it greps the ID from FileB.

This will just display the lines to the screen. You can redirect it to a file if you like.

cat | \
while read ID OTHERSTUFF
do

grep "${ID}" /path/to/FileB >> tonewfile.txt

done

And then there's the join command. I use the method above because it reads each line into variables (whitespace separated). I can then customize the order of the fields when creating output quite easily. Because the data is in variables.

You can make this more advanced by using sed as well. But if a simple grep will do the trick I use that.


Cheers

The Devil is in the detail.
OFC_EDM
Respected Contributor

Re: File Handling

My prev post is not great as it stripped out my whitespace.

But in my example theres 2 fields in FileA and 4 fields in FileB.
The Devil is in the detail.
Panpalia
Occasional Advisor

Re: File Handling

Thnaks Kevin for quick response.
James R. Ferguson
Acclaimed Contributor

Re: File Handling

Hi:

Here's another way:

# cat fileA
1
3
5
7
9

# cat fileB
a 1
b 2
e 5
f 6
i 9

# grep -f fileA fileB
a 1
e 5
i 9

Regards!

...JRF...
Leif Halvarsson_2
Honored Contributor

Re: File Handling

Hi,

Of course there is several ways to solve a problem but I would prefer "join" in a case like this.

One reason is that "join" can easily handle a "one to many" relation ( one line in the first file matches several lines in the second file).

Another reason is that both files is only read once (if using "grep", the entire file 2 will be read for every line in file1).

The output from, join can be composed from any of the filds in the two files but, if you need post-processing (e.g. headers, summary) you can pipe the output to "awk".
James R. Ferguson
Acclaimed Contributor

Re: File Handling

Hi (again):

Indeed TMTOWTDI

A better definition of the two files; their size; their fields; their field delimiters; their order (sorted or unsorted); whether or not the output order needs to match that of the input; whether or not duplicate matches should be discarded or output; all of this would lead to a better answer.

Regards!

...JRF...
Panpalia
Occasional Advisor

Re: File Handling

Thanks everybody.