- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- File Handling
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2008 01:48 AM
тАО05-30-2008 01:48 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2008 01:58 AM
- Tags:
- join
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2008 02:25 AM
тАО05-30-2008 02:25 AM
Re: File Handling
-------------------
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2008 02:27 AM
тАО05-30-2008 02:27 AM
Re: File Handling
But in my example theres 2 fields in FileA and 4 fields in FileB.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2008 02:53 AM
тАО05-30-2008 02:53 AM
Re: File Handling
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2008 03:39 AM
тАО05-30-2008 03:39 AM
Re: File Handling
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...
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2008 04:30 AM
тАО05-30-2008 04:30 AM
Re: File Handling
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".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2008 04:45 AM
тАО05-30-2008 04:45 AM
Re: File Handling
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-02-2008 08:54 PM
тАО06-02-2008 08:54 PM