will bring
real 0m41.111s
user 0m1.400s
sys 0m4.134s
So far it is working properly, but i want to speed it up.
How can i change the script to make it more fast?
The commands with grep and awk combination can be done with awk only, like:
Instead of
WV=`grep WV120B $i | awk '{print $2}'`
Use
WV=`awk '/WV120B/{print $2}' $i`
This will get rid of those 4 grep commands.
The date command is used twice in the beginning +"as many files you have" times, whereas just one date would have done the thing for you. also the one inside the loop is not needed since the result is fixed. Instead of these 3 commands, use the below at the very beginning of the script:
Like guruprasadr suggests, it is important to limit the number of external programs as much as possible, especially within the loop. Try:
#!/bin/bash
fpat=$(date +%Y%m)*
home=$HOME/METE
cd $home
for i in $fpat
do
if test -f "$i"
then
Dy=${i:2:2} Dm=${i:4:2} Dd=${i:6:2}
UZh=${i:8:2} UZm=${i:10:2}
res=$(awk '{A[$1]=$2}END{print A["WV120B"], A["WR120B"], A["NSCHLB"], A["TLUFT12B"]}' OFS='\t' "$i")
printf "%s/%s/%s-%s:%s\t%s\n" $Dm $Dd $Dy $UZh $UZm "$res"
else
DAT=$(date +%Y%m%d)
printf "$DAT - Datei $i is not here" >> "$HOME/Logfiles/Log_$(date +%Y%m%d).log"
fi
done
--
Guru: these suggestions could be further improved by leaving out some of the echo + back tick combinations.
Thanks a lot!
With the advancement of @guruprasadpr the script takes only half the time, bud with @Scrutinizer improvement it only needs 5.4 sec.
really great!!