无法覆盖到文件

时间:2013-11-12 17:43:00

标签: linux bash unix sh

我正在尝试将unix命令的输出覆盖到文件中,但它会继续将输出附加到文件中。

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

参考资料可在此处找到:Tutorial

这是我得到的输出--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%  

应该只有一个百分点

10%

我在哪里做错了?

1 个答案:

答案 0 :(得分:1)

使用三个工具(grepsedawk)来实现他们中的任何一个可以做的事情,这表明编写原始脚本的人并不真正理解任何那些工具。

仅使用awk(并且只使用posix awk功能,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")}'

(我刚用gawk,mawk和原始awk(在Mac OS X上使用)检查过,最后一个似乎不理解大括号重复操作符,所以你需要写{{1有了gawk,有一些可能的简化。)

这是一个简单的解决方案,仅使用match($0,/[0-9]?[0-9]?[0-9]%/),虽然它的行为几乎与原始行为完全相同(即它不会在每行上回放文件。我们将会这样做。)

grep

有各种方法可以覆盖每一行的文件;这是一个简单的:

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