Bash:从输出中去除尾随换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12524308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:44:45  来源:igfitidea点击:

Bash: Strip trailing linebreak from output

bashnewlineline-breaks

提问by hexacyanide

When I execute commands in Bash (or to be specific, wc -l < log.txt), the output contains a linebreak after it. How do I get rid of it?

当我在 Bash 中执行命令(或者更具体地说,wc -l < log.txt)时,输出后面包含一个换行符。我该如何摆脱它?

回答by Steve

If your expected output is a single line, you can simply remove all newline characters from the output. It would not be uncommon to pipe to the 'tr' utility, or to Perl if preferred:

如果您的预期输出是单行,您可以简单地从输出中删除所有换行符。如果愿意,管道到“tr”实用程序或 Perl 并不少见:

wc -l < log.txt | tr -d '\n'

wc -l < log.txt | perl -pe 'chomp'

You can also use command substitution to remove the trailing newline:

您还可以使用命令替换来删除尾随的换行符:

echo -n "$(wc -l < log.txt)"

printf "%s" "$(wc -l < log.txt)"

Please note that I disagree with the OP's decision to choose the accepted answer. I believe one should avoid using 'xargs' where possible. Yes, it's a cool toy. No, you do not need it here.

请注意,我不同意 OP 选择已接受答案的决定。我相信应该尽可能避免使用“xargs”。是的,这是一个很酷的玩具。不,你在这里不需要它。



If your expected output may contain multiple lines, you have another decision to make:

如果您的预期输出可能包含多行,您需要做出另一个决定:

If you want to remove MULTIPLE newline characters from the end of the file, again use cmd substitution:

如果要从文件末尾删除多个换行符,请再次使用 cmd 替换:

printf "%s" "$(< log.txt)"

If you want to strictly remove THE LAST newline character from a file, use Perl:

如果要从文件中严格删除最后一个换行符,请使用 Perl:

perl -pe 'chomp if eof' log.txt




Note that if you are certain you have a trailing newline character you want to remove, you can use 'head' from GNU coreutils to select everything except the last byte. This should be quite quick:

请注意,如果您确定要删除尾随换行符,则可以使用 GNU coreutils 中的“head”来选择除最后一个字节之外的所有内容。这应该很快:

head -c -1 log.txt

Also, for completeness, you can quickly check where your newline (or other special) characters are in your file using 'cat' and the 'show-all' flag. The dollar sign character will indicate the end of each line:

此外,为了完整性,您可以使用“cat”和“show-all”标志快速检查换行符(或其他特殊)字符在文件中的位置。美元符号字符将指示每一行的结尾:

cat -A log.txt

回答by Satya

One way:

单程:

wc -l < log.txt | xargs echo -n

回答by Andrey Taranov

There is also direct support for white space removal in Bash variable substitution:

Bash 变量替换中也直接支持删除空格:

testvar=$(wc -l < log.txt)
trailing_space_removed=${testvar%%[[:space:]]}
leading_space_removed=${testvar##[[:space:]]}

回答by zero2cx

printfalready crops the trailing newline for you:

printf已经为您裁剪了尾随换行符:

$ printf '%s' $(wc -l < log.txt)

Detail:

细节:

  • printf will print your content in place of the %sstring place holder.
  • If you do not tell it to print a newline (%s\n), it won't.
  • printf 将打印您的内容来代替%s字符串占位符。
  • 如果您不告诉它打印换行符 ( %s\n),它就不会。

回答by nneonneo

If you assign its output to a variable, bashautomatically strips whitespace:

如果将其输出分配给变量,bash则会自动去除空格:

linecount=`wc -l < log.txt`

回答by Tom Hale

If you want to remove only the last newline, pipe through:

如果您只想删除最后一个换行符,请通过管道:

sed -z '$ s/\n$//'

sedwon't add a \0to then end of the stream if the delimiter is set to NULvia -z, whereas to create a POSIX text file (defined to end in a \n), it will always output a final \nwithout -z.

sed\0如果分隔符设置为NULvia -z,则不会在流的结尾添加 a ,而要创建 POSIX 文本文件(定义为以 a 结尾\n),它将始终输出\n不带-z.

Eg:

例如:

$ { echo foo; echo bar; } | sed -z '$ s/\n$//'; echo tender
foo
bartender

And to prove no NULadded:

并证明没有NUL添加:

$ { echo foo; echo bar; } | sed -z '$ s/\n$//' | xxd
00000000: 666f 6f0a 6261 72                        foo.bar


To remove multiple trailing newlines, pipe through:

删除多个尾随换行符,请通过管道:

sed -Ez '$ s/\n+$//'

回答by Rastislav Hasicek

If you want to print output of anything in Bash without end of line, you echo it with the -nswitch.

如果你想在没有行尾的情况下打印 Bash 中的任何输出,你可以用-nswitch回显它。

If you have it in a variable already, then echo it with the trailing newlinecropped:

如果您已经在一个变量中拥有它,那么用裁剪的尾随换行符来回显它:

    $ testvar=$(wc -l < log.txt)
    $ echo -n $testvar

Or you can do it in one line, instead:

或者你可以在一行中完成,而不是:

    $ echo -n $(wc -l < log.txt)