File splitting issues


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File splitting issues
# 1  
Old 07-30-2010
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 02:04 AM..
# 2  
Old 07-30-2010
could you please list the format of your pharmacy and pharmacy id files?
# 3  
Old 07-30-2010
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
# 4  
Old 07-30-2010
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 10:40 AM.. Reason: Correcting code tags
# 5  
Old 08-02-2010
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 06:14 AM.. Reason: Code tags, please...
# 6  
Old 08-02-2010
Quote:
Originally Posted by Maya_Pillai

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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Splitting the file based on two fields - Fixed length file

Hi , I am having a scenario where I need to split the file based on two field values. The file is a fixed length file. ex: AA0998703000000000000190510095350019500010005101980301 K 0998703000000000000190510095351019500020005101480 ... (4 Replies)
Discussion started by: saj
4 Replies

2. Shell Programming and Scripting

Splitting a text file into smaller files with awk, how to create a different name for each new file

Hello, I have some large text files that look like, putrescine Mrv1583 01041713302D 6 5 0 0 0 0 999 V2000 2.0928 -0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 5.6650 0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 3.5217 ... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

3. Shell Programming and Scripting

Execution of loop :Splitting a single file into multiple .dat file

hdr=$(cut -c1 $path$file|head -1)#extract header”H” trl=$(cut -c|path$file|tail -1)#extract trailer “T” SplitFile=$(cut -c 50-250 $path 1$newfile |sed'$/ *$//' head -1')# to trim white space and extract table name If; then # start loop if it is a header While read I #read file Do... (4 Replies)
Discussion started by: SwagatikaP1
4 Replies

4. Shell Programming and Scripting

Splitting XML file on basis of line number into multiple file

Hi All, I have more than half million lines of XML file , wanted to split in four files in a such a way that top 7 lines should be present in each file on top and bottom line of should be present in each file at bottom. from the 8th line actual record starts and each record contains 15 lines... (14 Replies)
Discussion started by: ajju
14 Replies

5. UNIX for Dummies Questions & Answers

Extracting data from one file, based on another file (splitting)

Dear All, I have two files but want to extract data from one based on another... can you please help me file 1 David Tom Ellen and file 2 David|0010|testnamez|resultsz David|0004|testnamex|resultsx Tom|0010|testnamez|resultsz Tom|0004|testnamex|resultsx Ellen|0010|testnamez|resultsz... (12 Replies)
Discussion started by: A-V
12 Replies

6. Shell Programming and Scripting

Splitting a file in to multiple files and passing each individual file to a command

I have an input file with contents like: MainFile.dat: 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 12247689|7896|77698080 16768900|hh78|78959390 ... (4 Replies)
Discussion started by: rkrish
4 Replies

7. Shell Programming and Scripting

File splitting, naming file according to internal field

Hi All, I have a rather stange set of requirements that I'm hoping someone here could help me with. We receive a file that is actually a concatenation of 4 files (don't believe this would change, but ideally the solution would handle n files). The super-file looks like:... (7 Replies)
Discussion started by: Leedor
7 Replies

8. UNIX for Dummies Questions & Answers

Help splitting file

hi, i was wondering how do i split a large text output file from a keyword, e.g. as soon as 'saveabc.dat' appears in the file. any answers would be much appreciated. thanks:confused: (3 Replies)
Discussion started by: shabs1985
3 Replies

9. UNIX for Dummies Questions & Answers

Splitting a file based on record sin another file

All, We receive a file with a large no of records (records can vary) and we have to split it into two files based on another file. e.g. File1: UHDR 2008112 "25187","00000022","00",21-APR-1991,"" ,"D",-000000519,+0000000000,"C", ,+000000000,+000000000,000000000,"2","" ,21-APR-1991... (7 Replies)
Discussion started by: er_ashu
7 Replies

10. Shell Programming and Scripting

[Splitting file] Extracting group of segments from one file to others

Hi there, I need to split one huge file into separate files if the condition is fulfilled according to that the position between 97 and 98 matches with “IT” at the segment MAS. There is no delimiter file is fix-width with varous line length. Could you please help me how I do split the file... (1 Reply)
Discussion started by: ozgurgul
1 Replies
Login or Register to Ask a Question