Naveen Gamage
Naveen Gamage

Reputation: 1884

can't overwrite to a file

I'm trying to overwrite output of a unix command to a file but it keep appending the output to the file.

wget --progress=dot http://test.com/example.zip 2>&1 | grep --line-buffered "%" | \ 
 sed -u -e "s,\.,,g" | awk '{printf(" %4s", $2)}' > log.txt

References can be found here: Tutorial

This is the output I'm getting - log.txt

   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   0%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   1%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   2%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%   3%  

It should be just one percentage

10%

where am I doing wrong?

Upvotes: 0

Views: 185

Answers (1)

rici
rici

Reputation: 241771

Using three tools (grep, sed and awk) to achieve what any one of them could do is a sign that whoever wrote the original script didn't really understand any of those tools.

Using just awk (and only posix awk features, afaik):

wget --progress=dot http://test.com/example.zip 2>&1 |
awk 'match($0,/[0-9]{1,3}%/){printf "%4s",substr($0,RSTART,RLENGTH) >"log.txt";
                             close("log.txt")}'

(I just checked this with gawk, mawk, and the original-awk (used on Mac OS X), and the last one doesn't seem to understand the brace-repetition operator, so you need to write match($0,/[0-9]?[0-9]?[0-9]%/). With gawk, there are some possible simplifications.)

And here's a simple solution using just grep, although it will behave almost exactly as your original (i.e. it won't rewind the file on each line. We'll get to that.)

wget --progress=dot http://test.com/example.zip 2>&1 |
  grep --line-buffered -oE '[0-9]{1,3}%'

There are various ways to get that to overwrite a file on every line; here's a simple one:

for PCT in $(wget --progress=dot http://test.com/example.zip 2>&1 |
             grep --line-buffered -oE '[0-9]{1,3}%'); do
  printf '%4s\n' $PCT > log.txt
done

Upvotes: 1

Related Questions