Results 1 to 3 of 3
1) I have to create 3 scripts which create different output files. Script one creates datafiles, script2 creates control files
and script3 creates flg files. The business wants the same ...
- 07-26-2010 #1Just Joined!
- Join Date
- Jul 2010
- Posts
- 16
Few doubts on Linux Scripting
1) I have to create 3 scripts which create different output files. Script one creates datafiles, script2 creates control files
and script3 creates flg files. The business wants the same timestamp to be attached to all the files created by these scripts.
Script1- Create data files - Data files name format-> xxxx.01122010_223048.dat
Script2- Create Control files - Control files name format -> yyyy.01122010_223048.ctl
Script3 - Create flg files- flg files name format-> zzzz.01122010_223048.flg
How can I define a variable which gives the timestamp once and then can be used in all the three scripts?
These scripts are called from an intial script as follows
./$POS_SCRIPT/dw_postodw_pharmacysplit.ksh
./$POS_SCRIPT/dw_postodw_controlfile.ksh
./$POS_SCRIPT/dw_postodw_cpfiles.ksh
How do I check the return status of the previous script before starting the next
If the return status is not success, I need to write that to a logfile and exit in the succeeding scripts.
-----------------------------------------
2)I have an input file(named common_files) which contains a set of table names. I need to search the current directory to
check if the filename contain the table names from the input file. Move all those files to another directory.
For example the filename is of the format XXXX_<tablename>_timestamp. If the tablename matches from input file,
those files should be moved to another directory.
Can I do the grep and move with a single command in my scripting? I got stuckup as below.
if [[ -s common_files]]
then
while read line
do
'grep -l $line * | mv
done <common_files
fi
--------------------------------------
3)I have another input file which contains a list of pharmacyids(This is an input file contains around 450 pharmacy ids).
I need to search a set of flatfiles(119 files) to split them based on the 450 ids.Means each of these 119 files need to be
split into 450 files based on the presence of the pharmacy ids. The postion of pharmacy id in the 119 files vary from file
to file.I need to search for the id as a whole word and copy the matching lines to outputfiles. So a maximum of 119 * 450
output files may be generated.
INPUT FILE -> a file contains a list of 450 pharmacyids
flat FILE -> 119 files whcih may conatin the 450 pharmacy ids , but position of ids vary
OUTPUT FILES->the files created from flat files by copying the matching lines for each id.
The below one is my script. Can somebody review and give suggestions on logic and performance?
(I heard 'cat' and grep used together will slow down the performance especially this may be executed 119 * 450 times).
for pharmacyf in `ls`
do
$tablename=` $pharmacyf | cut –f3 -d'.' `
while read pharmacyid
do
cat $pharmacyf | grep -w $pharmacyid >> $POSOUT/ODS.POS.$pharmacyid.$tablename.$CURRENT_DATE
done<inputfile
done
Thanks
Maya
- 07-26-2010 #2
Wow, a lot of questions. Would have been nice to have code listings in [CODE ][/CODE] tags.
To create filenames representing a single instant in time:
The second part of your question, I cannot understand. Rephrase it, and maybe I'll take a shot at it.Code:# First, read the date manpage: man date # # Create a time/date stamp & assign it to a variable NOW=$(date "+%d%m%Y_%H%M%S") # Create three variables containing filenames with embedded timestamps DATFILENAME=xxxx.${NOW}.dat CTLFILENAME=yyyy.${NOW}.ctl FLGFILENAME=zzzz.${NOW}.flg
You don't need to use ls in backticks:
for pharmacyf in `ls`
do
$tablename=` $pharmacyf | cut –f3 -d'.' `
while read pharmacyid
do
cat $pharmacyf | grep -w $pharmacyid >> $POSOUT/ODS.POS.$pharmacyid.$tablename.$CURRENT_DATE
done<inputfile
done
Your code assumes every file in the directory will be executable, but you probably intend to pipe the contents to cut, and you don't create variables with a "$" prefix.Code:for pharmacyf in *
You don't need to cat a file and pipe it to grep; grep can read files specified on the commandline:Code:tablename=`cut –f3 -d'.' < $pharmacyf`
--- rod.Code:grep -w $pharmacyid $pharmacyf
Stuff happens. Then stays happened.
- 07-27-2010 #3Just Joined!
- Join Date
- Jul 2010
- Posts
- 16
Thank you so much.
Maya


Reply With Quote