-2

I am processing a file content using a foeach loop in unix environment and I would like to print the percentage of task completed in a single line on the xterm. something like an increasing counter.

foreach xxx (`cat file1`)
 set total = `wc -l file1`
 # count definination 
 # print percentage completed => count/total
end

Content of file1 is some test in a single column

aaa
bbb
ccc
ddd

Output expected is

> 25%
> 50%

updated numbers overwrite the previous status on the same line. something like a live ticker

Cyrus
  • 84,225
  • 14
  • 89
  • 153
user1495523
  • 475
  • 2
  • 7
  • 17

1 Answers1

0

This is what I have discovered that satisfies my requirements

set ccc = 0
foreach xxx ( `cat file1 ` )
set nnn = ` wc -l file1`
set ccc = `expr $ccc + 1`
echo $ccc $nnn | awk '{printf ("\r%-5s completed out of %s",$1,$2)}'
end

The key element that worked for me is '\r' option in the print command, this option would replace the text on the current line.

user1495523
  • 475
  • 2
  • 7
  • 17
  • Why not just do everything inside awk, instead of doing harm? (See [this](https://www-uxsup.csx.cam.ac.uk/misc/csh.html) or [this](https://www.grymoire.com/Unix/CshTop10.txt) or [this](https://www.shlomifish.org/open-source/anti/csh/)). – ghoti May 29 '19 at 03:49