Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-03-2010, 10:12 AM
|
#1
|
LQ Newbie
Registered: Jul 2010
Posts: 24
Rep:
|
How to get output file from a master file
Frieds,
I am having following problem in linux environment.
I have a following basic file with name " BASEFILE", as shown example below :
sl.no pol.no name status loan
1 123 rama FORCE 500
2 234 jama LAPSE 800
3 345 kama FORCE 900
4 456 lama FORCE 550
5 567 nama WDRAW 650
6 678 sama SURRD 750
7 789 pama FORCE 850
Now I want outputfile with name "OUTPUTFILE" for only few pol nos
like
123
789
678
ie,1. MY OUTPUT FILE IS TO BE WITH ALL THE COLUMNS for THOSE ONLY 3 POL
2. MY OUTPUT FILE IS TO BE WITH SELECTED COLUMNS SAY name and status ,ONLY FOR 3 POLICIES.
( in my work environment, iam having a basic file with 195000 rows and i want output file for only 57000 policies, such the case )
I request you to suggest the command for drawing such output file.
, if awk programme is necessary, pl tell me where, and how to write such
programme.
Pl guide me
Rao
|
|
|
08-03-2010, 10:21 AM
|
#2
|
LQ Newbie
Registered: Dec 2009
Location: San Jose Ca
Distribution: Kubuntu 9.10
Posts: 2
Rep:
|
just use grep
so like:
grep "123" BASEFILE > OUTPUTFILE
would give you all the lines containing the 123 on them and then
grep "789" BASEFILE >> OUTPUTFILE
would append all the lines with 789 on them to the OUTPUTFILE.
etc..
I hope this helps.
Norm
|
|
0 members found this post helpful.
|
08-03-2010, 10:24 AM
|
#3
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
Here are 2 ways of doing just that:
Awk:
awk '$2==123||$2==789||$2==678' infile > outputfile
Grep:
egrep " 123 | 789 | 678 " infile > outputfile
Hope this helps.
Last edited by anon237; 08-03-2010 at 10:34 AM.
|
|
1 members found this post helpful.
|
08-03-2010, 10:26 AM
|
#4
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
Quote:
Originally Posted by aka_norm
grep "123" BASEFILE > OUTPUTFILE
|
Be carefull with this, it will probably inlcude false hits!!
With a 195000 entries there is probably an entry like 123, but also one like 1230. Both will be shown with the above example.
Include the space/tab in front and behind the number (see my post #3).
Hope this clears things up.
|
|
1 members found this post helpful.
|
08-03-2010, 10:30 AM
|
#5
|
LQ Newbie
Registered: Dec 2009
Location: San Jose Ca
Distribution: Kubuntu 9.10
Posts: 2
Rep:
|
Thanks Druuna... Great points.. After I posted, I noticed that the input file might also have " 123 " in the loan field so the awk method is the best way so that you are only looking at second field.
|
|
1 members found this post helpful.
|
08-03-2010, 10:33 AM
|
#6
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
@aka_norm: LOL!! I overlooked the loan part (shame on me.....)
The awk one is indeed the one to go with.
After taking a good look: If the loan part is the last word on the line, the grep will work (I included a leading and a trailing space).
Last edited by anon237; 08-03-2010 at 10:36 AM.
|
|
|
08-03-2010, 11:12 AM
|
#7
|
LQ Newbie
Registered: Jul 2010
Posts: 24
Original Poster
Rep:
|
friends,
like 123 678, i have to at a stretch i need outputfile with 57000 entries, like 123,678,789,...... (570000).
It is little bit difficult to write all the nos in command line. pl help me in getting output file for all those 57000 entries from base file of 195000.
Pl guide me.
Rao
|
|
|
08-03-2010, 11:20 AM
|
#8
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
- Where are these 57000 stored?
- How are they stored?
Both not mentioned in your original post and how are we to know that this and i want output file for only 57000 policies means you have 57000 policies floating around somewhere and that is the input? I see no good reason to flag all answers as unhelpful if your initial description of the problem is (partially) incorrect.....
Please be precise if you describe your problem.
|
|
|
08-03-2010, 11:34 AM
|
#9
|
LQ Newbie
Registered: Jul 2010
Posts: 24
Original Poster
Rep:
|
Dear friend, I have in my first posting also i have noted that out of a base file with 195000 i want output file for 57000 entries. ( pl see in brackets in my first posting.)
Further my problem is like this,
in my office policy data master file is with 195000 policy numbers with various details like loan amount, status, name of policyholder, address, commencement date like so.
Now another data file with 57000 i have recd from our higher office in linux format ie policy numbers are in the filed 1 to 9 , ie 123456789
325645893
256321456
.... like so (570000 policy nos)
Now my higher office requires a out put file with the following fields for all the 57000 policies
field like status, loanamount,name, address etc.
That is the reason, i have a given a test example for my above situation.
ie for few nos , output file is required with mentioned fields from the base file.
now pl guide me
Rao
|
|
|
08-03-2010, 11:46 AM
|
#10
|
LQ Newbie
Registered: Jul 2010
Posts: 24
Original Poster
Rep:
|
How to get output file from a master file-pl see my earlier posting
Dear friend, I have in my first posting also i have noted that out of a base file with 195000 i want output file for 57000 entries. ( pl see in brackets in my first posting.)
Further my problem is like this,
in my office policy data master file is with 195000 policy numbers with various details like loan amount, status, name of policyholder, address, commencement date like so.
Now another data file with 57000 i have recd from our higher office in linux format ie policy numbers are in the filed 1 to 9 , ie 123456789
325645893
256321456
.... like so (570000 policy nos)
Now my higher office requires a out put file with the following fields for all the 57000 policies
field like status, loanamount,name, address etc.
That is the reason, i have a given a test example for my above situation.
ie for few nos , output file is required with mentioned fields from the base file.
now pl guide me
|
|
|
08-03-2010, 11:56 AM
|
#11
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi again,
First of all: Both aka_norm and me never understood from your first post what is was you wanted. And I did read what's between the braces (I even quoted from it and told you it was not clear). Please be as precise as possible when describing your problem and keep in mind that you might not have been understood before pushing Thanks/Yes/No buttons.......
Code:
#!/bin/bash
policyFile="policyfile"
inFile="INFILE"
outFile="OUTPUTFILE"
> $OUTPUTFILE
while read polNo
do
awk '$2=='$polNo'' $inFile >> $outFile
done < $policyFile
Example run:
Code:
$ cat INFILE
sl.no pol.no name status loan
1 123 rama FORCE 500
2 234 jama LAPSE 800
3 345 kama FORCE 900
4 456 lama FORCE 550
5 567 nama WDRAW 650
6 678 sama SURRD 750
7 789 pama FORCE 850
$ cat policyfile
123
789
678
$ ./extract.sh
$ cat OUTPUTFILE
1 123 rama FORCE 500
7 789 pama FORCE 850
6 678 sama SURRD 750
|
|
1 members found this post helpful.
|
08-03-2010, 12:00 PM
|
#12
|
Moderator
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696
|
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. Your threads have been merged.
|
|
|
08-04-2010, 12:12 AM
|
#13
|
Member
Registered: Mar 2010
Distribution: Arch
Posts: 211
Rep:
|
To select from text files based on key fields, you want the 'join' command - which can also do other stuff, but this is what you need it for now.
join works on files sorted in key field order. Just to be safe, make a sorted policies file:
Code:
sort -nk2 pol >sorted.pol
and then selecting your records is easy:
Code:
sort -n sel|join -1 1 -2 2 - sorted.pol
you can get join to print a list of fields; see its info page.
|
|
|
All times are GMT -5. The time now is 06:49 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|