gpt4 book ai didi

linux - cat 和 more 有不同的输出

转载 作者:太空宇宙 更新时间:2023-11-04 12:13:10 24 4
gpt4 key购买 nike

当我尝试执行以下操作时,两个输出都不同:

$ cat /var/log/log

BACKUP-2017_12_30_03-35-02.ta 100% 5330MB 14.2MB/s 06:16

BACKUP-2018_01_09_03-35-02.ta 100% 5342MB 14.7MB/s 06:03

BACKUP-2018_01_02_03-35-02.ta 100% 5312MB 14.6MB/s 06:03

BACKUP-2018_01_06_03-35-02.ta 100% 5328MB 14.4MB/s 06:11

more/var/log/log 具有与上述相同的输出,但范围从 1% 到 100%。

我必须在下面的脚本中使用 cat 的输出,但是当我这样做时,输出将与 more 的输出相同。

echo "Below are the files copied during RUN" && echo -e "\n----- Contents -----\n" && cat /var/log/log) | tr -dc '[[:print:]]' | mailx -v \

最佳答案

控制台上使用了一些特殊字符和序列,这些特殊字符用于各种目的,例如移动光标位置,还可以产生声音和设置各种终端选项。这些称为转义序列,例如,您可以在此处阅读有关它们的信息 https://en.wikipedia.org/wiki/ANSI_escape_code .
cat <file>在(正常的)控制台终端上将/应该解析大多数/一些转义序列。您可能会看到命令的输出保存到一个文件中,就像这个命令将在终端上运行一样(噗,我不知道如何解释它,请参阅下面的示例)。 more解析转义序列有些智能,因此输出对用户更友好。
让我们用一个例子来展示它。假设您正在使用 wget 下载文件:

$ wget http://www.ac6-tools.com/downloads/SW4STM32/install_sw4stm32_linux_64bits-v2.3.run
--2018-01-12 14:10:37-- http://www.ac6-tools.com/downloads/SW4STM32/install_sw4stm32_linux_64bits-v2.3.run
Resolving www.ac6-tools.com... 37.59.46.135
Connecting to www.ac6-tools.com|37.59.46.135|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 433081872 (413M) [application/octet-stream]
Saving to: ‘install_sw4stm32_linux_64bits-v2.3.run.5’

install_sw4stm32_linux_ 0%[ ] 2.91M 1.42MB/s

最后一行'更改/刷新自身',我的意思是,wget 生成转义序列以将光标位置移动到行的开头(可能是'\r')并再次打印该行,从而刷新它。现在我们将欺骗 wget 认为它的输出是控制台并将它的 stderr 和 stdout 输出保存到文件/tmp/1。

 unbuffer wget http://www.ac6-tools.com/downloads/SW4STM32/install_sw4stm32_linux_64bits-v2.3.run >&/tmp/1

现在让我们:

$ cat /tmp/1
--2018-01-12 14:10:27-- http://www.ac6-tools.com/downloads/SW4STM32/install_sw4stm32_linux_64bits-v2.3.run
Resolving www.ac6-tools.com... 37.59.46.135
Connecting to www.ac6-tools.com|37.59.46.135|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 433081872 (413M) [application/octet-stream]
Saving to: ‘install_sw4stm32_linux_64bits-v2.3.run.4’

install_sw4stm32_linux_64b 1%[ ] 5.87M 2.63MB/s

然而,这并不是文件的确切内容。例如在 vim 中你可能会看到:

$ vim /tmp/1
--2018-01-12 14:10:27-- http://www.ac6-tools.com/downloads/SW4STM32/install_sw4stm32_linux_64bits-v2.3.run
Resolving www.ac6-tools.com... 37.59.46.135
Connecting to www.ac6-tools.com|37.59.46.135|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 433081872 (413M) [application/octet-stream]
Saving to: ‘install_sw4stm32_linux_64bits-v2.3.run.4’

^M install_sw4stm3 0%[ ] 0 --.-KB/s ^M install_sw4stm32 0%[ ] 596.40K 2.91MB/s ^M install_sw4stm32_ 0%[ ] 1.66M 4.14MB/s ^M install_sw4stm32_l 0%[ ] 2.41M 4.01MB/s ^M install_sw4stm32_li 0%[ ] 3.11M 3.87MB/s ^M install_sw4stm32_lin 0%[ ] 3.60M 3.57MB/s ^M install_sw4stm32_linu 0%[ ] 3.94M 3.23MB/s ^M install_sw4stm32_linux 1%[ ] 4.30M 3.02MB/s ^M install_sw4stm32_linux_ 1%[ ] 4.69M 2.88MB/s ^M install_sw4stm32_linux_6 1%[ ] 5.08M 2.78MB/s ^M install_sw4stm32_linux_64 1%[ ] 5.48M 2.70MB/s ^M install_sw4stm32_linux_64b 1%[ ] 5.87M 2.63MB/s

那些^M在输出中签名这是 vim 告诉你的方式,那里有一个特殊字符(不是字符 ^M ,而是一个特殊的转义序列/字符)。在这种情况下,它是用于将光标移动到文件开头的转义序列。more 在解析转义序列时有些智能:

$ more /tmp/1
--2018-01-12 14:10:27-- http://www.ac6-tools.com/downloads/SW4STM32/install_sw4stm32_linux_64bits-v2.3.run
Resolving www.ac6-tools.com... 37.59.46.135
Connecting to www.ac6-tools.com|37.59.46.135|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 433081872 (413M) [application/octet-stream]
Saving to: ‘install_sw4stm32_linux_64bits-v2.3.run.4’

install_sw4stm32_l 0%[ ] 2.41M 4.01MB/s
install_sw4stm32_linux 1%[ ] 4.30M 3.02MB/s
install_sw4stm32_linux_64b 1%[ ] 5.87M 2.63MB/s

希望这能澄清您与 cat 和更多输出的不一致。

关于linux - cat 和 more 有不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48226042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com