简体   繁体   English

将wget输出覆盖到文本文件

[英]Overwriting wget output to a text file

I'm trying to write wget command's filtered output to a file, but it always appends to the text file and display some unrecognizable characters. 我正在尝试将wget命令的过滤后的输出写入文件,但是它总是附加到文本文件中并显示一些无法识别的字符。

Following script will output on CLI like 1% , 2% and so on nicely but when I redirect the output to a text file, it shows some wired characters and all get appended instead of getting overwritten. 以下脚本将在CLI上很好地输出1%,2%等,但是当我将输出重定向到文本文件时,它显示了一些有线字符,并且所有字符都被追加而不是被覆盖。

#!/bin/sh
download()
{
    local url=$1
    wget --progress=dot $url 2>&1 | grep --line-buffered "%" | \
        sed -u -e "s,\.,,g" | awk '{printf("\b\b\b\b%4s", $2)}'
    echo " DONE"
}

file="$1"
echo -n "Downloading $file:"
download "$file" > file.log

I tried using > won't work, where am I doing wrong? 我尝试使用>无法正常工作,我在哪里做错了?

This is what I found on the file. 这是我在文件上找到的。

  1%  2%  3%  5%  6%  7%  9% 9% 10% 11% 13% 13% 13% 14%

but it should be 但这应该是

14%

Just the current progress. 只是当前的进展。

All I'm trying to do is write current file progress ( like 10% ) so I can just read the file and check file progress regularly for fronted purposes. 我要做的就是写当前文件进度(例如10% ),因此我可以读取文件并定期检查文件进度以达到前端目的。

wget (and many other command line tools) use shell escape sequences to display their progress. wget (和许多其他命令行工具)使用外壳转义序列显示其进度。

This means they print "0%", then do something, then tell the terminal "overwrite '0%' with '5%'", etc. 这意味着他们打印“ 0%”,然后执行某些操作,然后告诉终端“用'5%'覆盖'0%'”,依此类推。

When you redirect the output to a file, you see these "overwrite" codes. 当您将输出重定向到文件时,您会看到这些“覆盖”代码。

Using --progress=dot should solve this; 使用--progress=dot应该可以解决这个问题; then you don't see any percentages (nor any erase code). 那么您将看不到任何百分比(也没有任何擦除代码)。 Instead, it will print one dot per 1K downloaded and you have to count dots. 相反,它每下载1K将打印一个点,您必须计算点数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM