LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 08-03-2010, 11:12 AM   #1
secondchanti
LQ Newbie
 
Registered: Jul 2010
Posts: 23

Rep: Reputation: 15
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
 
Old 08-03-2010, 11:21 AM   #2
aka_norm
LQ Newbie
 
Registered: Dec 2009
Location: San Jose Ca
Distribution: Kubuntu 9.10
Posts: 2

Rep: Reputation: 0
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.
Old 08-03-2010, 11:24 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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 druuna; 08-03-2010 at 11:34 AM.
 
1 members found this post helpful.
Old 08-03-2010, 11:26 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,
Quote:
Originally Posted by aka_norm View Post
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.
Old 08-03-2010, 11:30 AM   #5
aka_norm
LQ Newbie
 
Registered: Dec 2009
Location: San Jose Ca
Distribution: Kubuntu 9.10
Posts: 2

Rep: Reputation: 0
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.
Old 08-03-2010, 11:33 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@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 druuna; 08-03-2010 at 11:36 AM.
 
Old 08-03-2010, 12:12 PM   #7
secondchanti
LQ Newbie
 
Registered: Jul 2010
Posts: 23

Original Poster
Rep: Reputation: 15
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
 
Old 08-03-2010, 12:20 PM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.
 
Old 08-03-2010, 12:34 PM   #9
secondchanti
LQ Newbie
 
Registered: Jul 2010
Posts: 23

Original Poster
Rep: Reputation: 15
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
 
Old 08-03-2010, 12:46 PM   #10
secondchanti
LQ Newbie
 
Registered: Jul 2010
Posts: 23

Original Poster
Rep: Reputation: 15
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
 
Old 08-03-2010, 12:56 PM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.
Old 08-03-2010, 01:00 PM   #12
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,568

Rep: Reputation: 164Reputation: 164
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.
 
Old 08-04-2010, 01:12 AM   #13
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 211

Rep: Reputation: 67
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
open lock file /var/lib/postfix/master.lock: cannot open file: Permission denied gabsik Linux - Server 6 08-30-2012 10:39 PM
data from input file to be taken and send to output file grishu Linux - Newbie 18 08-03-2010 09:02 AM
GPG - encode file while manipulating output file name itmozart Linux - Newbie 2 10-03-2009 01:28 PM
How can I redirect the output of a file to another file within the file prakash.akumalla Linux - Newbie 28 10-30-2008 01:28 PM
why when redirecting output to a file any file extension seems to be fine? dr_zayus69 Linux - General 1 05-21-2005 05:09 AM


All times are GMT -5. The time now is 08:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration