Results 1 to 3 of 3
I am splitting a file based on the values read from an input file. The below one is the script.
1)How do I add the header which is present in ...
- 07-27-2010 #1Just Joined!
- Join Date
- Jul 2010
- Posts
- 16
File splitting issues
I am splitting a file based on the values read from an input file. The below one is the script.
1)How do I add the header which is present in the original file to the new split files created?(For eg. pharmacyf conatins header as table column names. The new files created (ODS.POS.$pharmacyid.$tablename.$CURRENT_DATE.dat) are without the header).
2) Also the script is creating 0 byte files for the pharmacyids which are not available in the intial file? Can this be avoided?
for pharmacyf in *
do
tablename=`echo $pharmacyf |cut -f4 -d'.' `
while read pharmacyid
do
grep -w $pharmacyid $pharmacyf >> $OUT/ODS.POS.$pharmacyid.$tablename.$CURRENT_DATE.dat
done< inputfile
done
Thanks
Maya
- 07-31-2010 #2
It would have been helpful to post your code in [CODE ][/CODE] tags, and to have shown enough of the input file(s) data to know hw to parse it (also in [code ] tags; very important).
If you have data which is constant through each iteration of a loop, then define it before entering the loop (the header), then use it on each iteration of the loop.Code:# # Variables not defined here: $OUT $pharmacyid $CURRENT_DATE # Variables possibly undefined here: $inputfile # for pharmacyf in *; do tablename=`echo $pharmacyf |cut -f4 -d'.' ` # Read header into $header header = "grep/cut/etc header content from $pharmacyf here" while read pharmacyid; do filename=${OUT}/ODS.POS.${pharmacyid}.${tablename}.${CURRENT_DATE}.dat echo $header > $filename grep -w $pharmacyid $pharmacyf >> $filename # If grep failed to find something, delete the file if [ $? ]; then rm $filename fi # Is "inputfile" a constant, or an as-yet-undefined variable? done< inputfile done
You haven't show enough information to determine how the header is found/parsed.
--- rod.Last edited by theNbomr; 07-31-2010 at 04:40 PM.
Stuff happens. Then stays happened.
- 08-02-2010 #3Just Joined!
- Join Date
- Jul 2010
- Posts
- 16
Thank you so much! You are great!
Maya


Reply With Quote