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
No line breaks with "cat"
提问by Crazy_Bash
This code should read from wget2.html
and 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/sed
with one sed
:
我想你可以cat/grep/sed
用一个替换你的sed
:
sed -e -n "/$sitename/ s@^.*\("$sitename".*jpg\).*$@@pg" wget.html
And you can replace sort | uniq
to sort -u
.
您可以替换sort | uniq
为sort -u
.
回答by Jonathan Leffler
The problem is not in the cat
line but in the echo
line. To get the line breaks, you need to use:
问题不cat
在线路上,而echo
在线路上。要获得换行符,您需要使用:
echo "$chksitename"
See also Capturing Multiple Line Output to a Bash Variable.
另请参阅将多行输出捕获到 Bash 变量。