Linux “cat”没有换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8551171/
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-08-06 03:42:29  来源:igfitidea点击:

No line breaks with "cat"

linuxbashubuntuecho

提问by Crazy_Bash

This code should read from wget2.htmland output the links found. But it gives me output without line breaks. How can I force cat to add line breaks?

此代码应读取wget2.html并输出找到的链接。但它给了我没有换行符的输出。如何强制 cat 添加换行符?

chksitename=$(cat wget2.html | grep -e "$sitename" | sed -e "s/^.*\("$sitename".*jpg\).*$//g" | sort | uniq)
echo $chksitename

采纳答案by Mat

You could try:

你可以试试:

echo $chksitename | tr ' ' '\n'

回答by uzsolt

I think you can replace your cat/grep/sedwith one sed:

我想你可以cat/grep/sed用一个替换你的sed

sed -e -n "/$sitename/ s@^.*\("$sitename".*jpg\).*$@@pg" wget.html

And you can replace sort | uniqto sort -u.

您可以替换sort | uniqsort -u.

回答by Jonathan Leffler

The problem is not in the catline but in the echoline. To get the line breaks, you need to use:

问题不cat在线路上,而echo在线路上。要获得换行符,您需要使用:

echo "$chksitename"

See also Capturing Multiple Line Output to a Bash Variable.

另请参阅将多行输出捕获到 Bash 变量