printf "\nClosing stats:\n" >> data.txt
echo >> data.txt
sed 's/^ \t*//;/^#/d;/^$/d' $stats | while read line
do
close=$(grep -w "^$line" $datafile | sed -e 's/\(.*\),\(.*\),\(.*\)/\2/')
if [ "$close" == "" ]; then
printf "%5d. %-s was not found in file\n" $counter $line
else
printf "%5d. %-7s : $ %6.2f\n" $counter $line $close
fi
counter=$(( counter + 1 ))
done >> data.txt
echo >> data.txt
usrname -srvio | awk '{print $1, $2, $3, $4, $11, $12}' >> data.txt
echo "Last Updated "$time >> data.txt
}
i have this portion of my script that i need help reducing
i tired several methods but it ended up the same length
Got rid of the echo command after the first printf and added another '\n' to the first printf. Otherwise, this is challenging to do w/o seeing the data going into the line variable.
printf "\nClosing stats:\n\n" >> data.txt
sed 's/^ \t*//;/^#/d;/^$/d' $stats | while read line
do
close=$(grep -w "^$line" $datafile | sed -e 's/\(.*\),\(.*\),\(.*\)/\2/')
if [ "$close" == "" ]; then
printf "%5d. %-s was not found in file\n" $counter $line
else
printf "%5d. %-7s : $ %6.2f\n" $counter $line $close
fi
counter=$(( counter + 1 ))
done >> data.txt
echo >> data.txt
usrname -srvio | awk '{print $1, $2, $3, $4, $11, $12}' >> data.txt
echo "Last Updated "$time >> data.txt
}
Hi,
close=$(grep -w "^$line" $datafile | sed -e 's/\(.*\),\(.*\),\(.*\)/\2/')
can be reduced to:
close=$(sed -n "/\<${line}\>/{s/\(.*\),\(.*\),\(.*\)/\2/")} $datafile)
Which will save you the calling of one external process in each loop.
HTH Chris
You didn't mention your shell.
if [ "$close" == "" ]; then
replace by:
if [ -z "$close" ]; then
echo >> data.txt
usrname -srvio | awk '{print $1, $2, $3, $4, $11, $12}' >> data.txt
replace by:
usrname -srvio | awk '{print;print $1, $2, $3, $4, $11, $12}' >> data.txt