hello, the below is a script i was trying to make(beginner obviously)
for file_name in ` ls /home/jessie `
echo ">> opening /home/jessie" >> CountCheck.txt
echo ""
if [ $file_name -eq 0 ];then
echo ">> no files found...." >> CountCheck.txt
echo ""
else
do
basic_name=`echo /home/jessie/$file_name | cut -f1 -d "."`
echo ">> spliting files....." >> CountCheck.txt
echo ""
egrep -v " 00122 | 00123 | 00180 " $file_name >$basic_name"_1.DAT"
echo ">> file 1 done...." >> CountCheck.txt
echo ""
egrep " 00122 | 00123 | 00180 " $file_name >$basic_name"_shrtcode.DAT.tmp"
echo ">> file 2 done...." >> CountCheck.txt
echo ""
echo ">> preparing to replace text....." >> CountCheck.txt
echo ""
echo ">> replacing text ......."
sed -e "s/\ 00122\ /\ 00200122\ /g" -e "s/\ 00123\ /\ 00200123\ /g" -e "s/\ 00180\ /\ 00200180\ /g" $basic_name"_shrtcode.DAT.tmp" >$basic_name"_shrtcode.DAT"
echo ""
record_count = `wc -l /home/jessie/$file_name`
basic_count = `wc -l $basic_name"_1.DAT"`
shrtcde_count =`wc -l $basic_name"_shrtcode.DAT"`
if [ `egrep " 00122 | 00123 | 00180 " $basic_name"_1.DAT"|wc -l` -eq 0 ];then
echo ">> Basic File Count =" $basic_count "there are no records with 00122,00123,or 00180">>CountCheck.txt
echo ""
echo ">> records updated successfully ..........." >>CountCheck.txt
echo ""
else
echo ">> Basic File still contains records with short codes" >>CountCheck.txt
echo ""
fi;
myCount = expr ${basic_count} + ${shrtcde_count}
if [ ${myCount} -eq $record_count ]; then
echo ">> original count = sum of both = " $myCount "replacement successfull,total number of records are exact" >> CountCheck.txt
echo ""
echo ">> moving files to /app/bmd/rating/data ..................." >>CountCheck.txt
echo ""
mv /home/jessie/$basic_name"_1.DAT" /app/bmd/rating/data
echo ">> file 1 moved successfully...." >>CountCheck.txt
echo ""
mv /home/jessie/$basic_name"_shrtcode.DAT" /app/bmd/rating/data
echo ">> file 2 moved successfully...."
echo ""
else
echo ">> count not equal still records missing." >>CountCheck.txt
echo ""
echo ">> myCount = "$myCount "while original file count = " $record_count >>CountCheck.txt
echo ""
echo ">> check record counts..."
echo ""
fi;
rm $basic_name"_shrtcode.DAT.tmp"
done--------
i want to know am i going in the right direction?
please help
Your most glaring error is your placement of the echo and if statements after the for loop and between the do. It should be like this: for file_name in .... do . .Since you asked, I would like to discuss coding philosophy; first, I would also include the shell invocation at line 1, column 1:
#!/bin/sh
or whatever shell you are using.
Next, your use of the for loop is considered a useless use of cat - or UUOC. you can read about it here:http://partmaps.org/era/unix/award....
Which means you could do something like this:
cd /home/jessie for file_name in * do . .Next, keep in mind that using `ls /home/jessie` does not guarantee that all objects are listed one per line. Consider using the -1:
ls -1 /home/jessie # guarantees one object per line
One of the biggest problems with your original for loop is that:
for file_name in `ls ....`
dobreaks if the output of ls has more that one column. You might consider using a while loop instead of a for loop:
ls /home/jessie| while read file_name do . .Let me know if you have any questions or comments.
Good luck with your script.
Greeaaat thankss i have took some notes and came up with the following which works perfectly however began to notice another problem, the contents of my path /home/jessie are files in the form ABC_25102011.DAT when we cat the file i have records with two types, one starts with "O" and other starts with "I" i need only those that start with O as below
cd /home/jessie/
for file_name in `*.DAT`
do
{TODAY_DATE}=`date +"%Y%m%d"`;
echo "" >>/home/jessie/output/CountCheck_${TODAY_DATE}.txt
echo "Script Started at: `date`" >> /home/jessie/output/CountCheck_${TODAY_DATE}.txt
echo "" >> /home/jessie/output/CountCheck.txt
echo "opening /home/jessie" >> /home/jessie/output/CountCheck_${TODAY_DATE}.txt
if [ $file_name -eq 0 ];then
echo "no files found...." >> /home/jessie/output/CountCheck_${TODAY_DATE}.txt
else
basic_name=`echo $file_name | cut -f1 -d "."`
echo "spliting files....." >> /home/jessie/output/CountCheck_${TODAY_DATE}.txt
egrep -v " 00122 | 00123 | 00180 " $file_name >$basic_name"_1.DAT"
echo "file 1 done...." >> /home/jessie/output/CountCheck_${TODAY_DATE}.txt
egrep " 00122 | 00123 | 00180 " $file_name && grep -s ^O $file_name >$basic_name"_shrtcode.DAT.tmp" <== i tried this but it doesnt work...?
echo "file 2 done...." >> /home/jessie/output/CountCheck_${TODAY_DATE}.txt
echo "preparing to replace text....." >> /home/jessie/output/CountCheck_${TODAY_DATE}.txt
sed -e "s/\ 00122\ /\ 00200122\ /g" -e "s/\ 00123\ /\ 00200123\ /g" -e "s/\ 00180\ /\ 00200180\ /g" $basic_name"_shrtcode.DAT.tmp" >$basic_name"_shrtcode.DAT"
echo "records updated successfully ..........." >>/home/jessie/output/CountCheck_${TODAY_DATE}.txt
fi;
Try something like this: egrep "^O" $file_name |egrep " 00122 | 00123 | 00180 "
Get all the lines starting with uppercase "O" before searching for the other text.
yea actually it works fine, so the problem was the && ??
This seems to work, but I have had portability problems with & and egrep: egrep "^O&00122|00123|00180" test.txt
This is more efficient than piping a grep to grep as it spawns one less shell.
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |