0

我正在尝试将 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

参考资料可以在这里找到:教程

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

我在哪里做错了?

4

1 回答 1

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和original-awk(在Mac OS X上使用)检查了这个,最后一个似乎不理解大括号重复运算符,所以你需要写match($0,/[0-9]?[0-9]?[0-9]%/)。使用gawk,有一些可能的简化。)

这是一个使用 just 的简单解决方案grep,尽管它的行为几乎与您的原始文件完全相同(即它不会在每一行上倒回文件。我们会解决的。)

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

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

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
于 2013-11-12T19:39:25.073 回答