Hi,
I am writing a script which processes large number of files in a directory. I wanto display a counter which increment after processing each file. I am processing each file in a for loop. If I echo a variable with its value increasing with each file, I will get around 5000 lines as output. Instead I wish to display a count - like a digital clock- which increases at the same position and line.
Hi,
Thanks for the quick replies.
here is the code I wrote.
ls -lrt *.txt |awk '{print $9}' > $HOME/file.list
for file in `cat $HOME/file.list`
do
echo " Processing $file" >>$HOME/file_process.log
$EXEC/xxx.exe $file >>$HOME/file_process.log
done
In the file.list, there will be around 5000 entries. If I put a normal print or echo command inside the loop, the output will be displayed 5000 times. I dont want this to happen. Still I want to know th eprogress of the activity.
If I use a normal counter by using a variable increment by one on each execution of the loop, output will be like this
1
2
3
4
5
6
7
.
.
.
I want to know if it is possible to display 1, then 2 at the position of 1, then 3 at the position of 2 and so on... I mean like a digital clock.
Did you read the above posts? Did you try some of the things posted there? If you want a read-to-go solution without having to do anything yourself, say so, but this will probably put people off from helping you.
cnt=0
# if you need only files which names are *.txt, then no need ls+awk
for file in *.txt
do
cnt=$((cnt+1))
# -n = no newline and -e needed if your echo not accept \r
# \r carriage return - cursor to the position 1
echo -n -e "\r$cnt $file "
$EXEC/xxx.exe $file >>$HOME/file_process.log
done
echo
echo "The End"
Just use printf and you won't have to worry about whether your echo supports -e \r -- all printf supports \r. You can also specify %3d to keep the number nicely aligned.