Re: File Handling (346 Views)
Reply
Occasional Advisor
Panpalia
Posts: 13
Registered: ‎05-29-2008
Message 1 of 9 (346 Views)
Accepted Solution

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.
Honored Contributor
Leif Halvarsson_2
Posts: 6,682
Registered: ‎01-09-2002
Message 2 of 9 (346 Views)

Re: File Handling

Hi,

Have a look at the "join" command (man join),
Respected Contributor
OFC_EDM
Posts: 740
Registered: ‎07-13-2004
Message 3 of 9 (346 Views)

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.
Respected Contributor
OFC_EDM
Posts: 740
Registered: ‎07-13-2004
Message 4 of 9 (346 Views)

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.
Occasional Advisor
Panpalia
Posts: 13
Registered: ‎05-29-2008
Message 5 of 9 (346 Views)

Re: File Handling

Thnaks Kevin for quick response.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 6 of 9 (346 Views)

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...
Honored Contributor
Leif Halvarsson_2
Posts: 6,682
Registered: ‎01-09-2002
Message 7 of 9 (346 Views)

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".
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 9 (346 Views)

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...
Occasional Advisor
Panpalia
Posts: 13
Registered: ‎05-29-2008
Message 9 of 9 (346 Views)

Re: File Handling

Thanks everybody.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.