Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-30-2010
Registered User
 

Join Date: May 2010
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
File splitting issues

Hi,

I have to spilt around 120 files based on some id .These ids are provided by an input file here.For each of the 120 files, the script will read the list of ids from input file, grep and split the files .

1) I am using grep to split the files. But, it is creating 0 byte files if the id is not present in a file.

2)Also, the original file conatins table column names as the first row. This first row is different for all the 120 files. How do I add this heading row to the split files?

3) How can I use 'awk' instead of grep here? Which is faster?

The code is as below -


Code:
for pharmacyf in *
do
tablename=` cut –f3 -d'.' $pharmacyf `
 
while read pharmacyid
do
grep -w $pharmacyid $pharmacyf>> $POSOUT/ODS.POS.$pharmacyid.$tablename.$CURRENT_DATE
 
done<inputfile
done

Thanks
Maya

Last edited by pludi; 07-30-2010 at 01:04 AM..
Sponsored Links
    #2  
Old 07-30-2010
Registered User
 

Join Date: Mar 2010
Location: India
Posts: 9
Thanks: 1
Thanked 1 Time in 1 Post
could you please list the format of your pharmacy and pharmacy id files?
Sponsored Links
    #3  
Old 07-30-2010
Registered User
 

Join Date: May 2010
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Pharmacy id file is just a text file - each line contains one pharmacyid

Pharmacy files are created from different tables. Each file format is different. One sample is attached.


Thanks
Maya
Attached Files
File Type: txt pharmacy_ids.txt (372 Bytes, 8 views)
File Type: txt ODS.POS.00001.TR_ITM_CPN_TND.20100414_132445.txt (2.6 KB, 8 views)
    #4  
Old 07-30-2010
Registered User
 

Join Date: Dec 2007
Location: TamilNadu,INDIA
Posts: 77
Thanks: 0
Thanked 1 Time in 1 Post
I cannot understand your requirement clearly.. Instead of saying input file it would be better to mention the exact name of file you use. Also it would be better to have the few possible values of "*" in your "for pharmacyf in *".

If you dont want GREP to create a 0 byte if there is no match, then use like this

Code:
str=`grep -w $pharmacyid $pharmacyf`
if [ $? -eq 0 -a  ! -z ${str} ]
then 
  echo "${str}" >> $POSOUT/ODS.POS.$pharmacyid.$tablename.$CURRENT_DATE
fi

you can add this with in your while loop

Last edited by Franklin52; 07-30-2010 at 09:40 AM.. Reason: Correcting code tags
Sponsored Links
    #5  
Old 08-02-2010
Registered User
 

Join Date: May 2010
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Ramkrix.

I will explain the requirement here.

I need to split a list of files (around 120) in a folder based on pharmacy id. Each of these files (ODS.POS.$pharmacyid.$tablename.$CURRENT_DATE
)is created from different tables and contain table column name as header(the first line of the file). The pharmacyids are provided as an input file(in the code mentioned as inputfile). After splitting, each new file should also contain the table column name as header.

This code is not creating the header for the new files.



Code:
for pharmacyf in *
do
tablename=` cut –f3 -d'.' $pharmacyf ` 
while read pharmacyid
do
grep -w $pharmacyid $pharmacyf>> $POSOUT/ODS.POS.$pharmacyid.$tablename.$CURRENT_DATE 
 
done<inputfile
done


Hope the requirement is clear now. Performance is a major criteria here. We need to run a set of scripts in 20 mts before the next team pick it up.

Thanks
Maya

Last edited by Scott; 08-02-2010 at 05:14 AM.. Reason: Code tags, please...
Sponsored Links
    #6  
Old 08-02-2010
guruprasadpr's Avatar
Registered User
 

Join Date: Jun 2009
Location: Bangalore, India
Posts: 423
Thanks: 21
Thanked 123 Times in 122 Posts
Quote:
Originally Posted by Maya_Pillai View Post

This code is not creating the header for the new files.
Hi



Code:
for pharmacyf in *
do
tablename=` cut –f3 -d'.' $pharmacyf ` 
while read pharmacyid
do
FILE="$POSOUT/ODS.POS.$pharmacyid.$tablename.$CURRENT_DATE"
[ -f  $FILE ] || echo "---header info---"  >$FILE
grep -w $pharmacyid $pharmacyf>> $POSOUT/ODS.POS.$pharmacyid.$tablename.$CURRENT_DATE 
 
done<inputfile
done

Not tested though...

Guru.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help splitting file shabs1985 UNIX for Dummies Questions & Answers 3 12-18-2008 04:45 PM
Splitting a txt file mohdtausifsh UNIX for Dummies Questions & Answers 6 10-04-2006 02:19 AM
[Splitting file] Extracting group of segments from one file to others ozgurgul Shell Programming and Scripting 1 09-14-2006 12:17 PM
splitting file rkl1 UNIX for Dummies Questions & Answers 3 11-30-2005 02:39 PM
File splitting praveen.pinto UNIX for Dummies Questions & Answers 9 02-10-2005 03:32 PM



All times are GMT -4. The time now is 03:59 AM.