如何合并bash提示符中的两行?

如何合并bash提示符中的两行?,bash,curl,paste,lines,Bash,Curl,Paste,Lines,我正在尝试编写一个脚本,在Web服务器上执行curl请求,并解析出“服务器”和“位置”。这样,我就可以轻松地将其导入excel表,而无需重新格式化 我当前的脚本: curl -sD - -o /dev/null -A "Mozilla/4.0" http://site/ | sed -e '/Server/p' -e '/Location/!d' | paste - - 预期/期望输出: Server: Apache Location: http://www.site 电流输出: Serve

我正在尝试编写一个脚本,在Web服务器上执行curl请求,并解析出“服务器”和“位置”。这样,我就可以轻松地将其导入excel表,而无需重新格式化

我当前的脚本:

curl -sD - -o /dev/null -A "Mozilla/4.0" http://site/ | sed -e '/Server/p' -e '/Location/!d' | paste - -
预期/期望输出:

Server: Apache Location: http://www.site
电流输出:

Server: Apache Location: http://www.site
从curl开始:

HTTP/1.1 301 Moved permanently
Date: Sun, 16 Nov 2014 20:14:01 GMT
Server: Apache
Set-Cookie: USERNAME=;path=/
Set-Cookie: CFID=16581239;path=/
Set-Cookie: CFTOKEN=32126621;path=/
Location: http://www.site
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
插入“sed”:

Server: Apache
Location: http://www.site
通过管道插入“粘贴”:

Server: Location: http://www.site
为什么在第一个空格后立即“粘贴”?如何使其正确格式化?我对其他方法持开放态度,但请记住,“curl”请求的响应长度不同

谢谢,

curl”的输出包含“return”,即会导致该行为的\r字符

curl -sD - -o /dev/null -A "Mozilla/4.0" http://site/ | tr -d '\r'| sed -e '/Server/p' -e '/Location/!d' | paste - -
tr-d'\r'过滤掉所有回车字符

关于线端 虽然Linux/Unix使用“LF”(换行符,\n)换行符,但许多其他系统使用“CR LF”(回车换行符\r\n)换行符。除非你做好准备,否则这可能会导致你看起来很疲惫。让我们看一些不带\r的示例,以及带\r的示例

串接:

a=$(echo -e "Please notice don't delete your files in /<config_dir> ")
b=$(echo -e "without hesitation ")
echo "$a""$b"
结果:

Please notice don't delete your files in /<config_dir> without hesitation
without hesitation  delete your files in /<config_dir> 
Stackoverflow is fun
Stackoverflow is funny
相同的wirh CR LF结束线:

echo -e "Stackoverflow is funny\r" | sed 's/ny$//g'
结果:

Please notice don't delete your files in /<config_dir> without hesitation
without hesitation  delete your files in /<config_dir> 
Stackoverflow is fun
Stackoverflow is funny
sed按设计工作,因为该行不是以“ny”结尾,而是以“ny CR”结尾

所有这些的教学都是为意外的输入数据做准备的。在大多数情况下,从数据中完全过滤掉\r可能是一个好主意,因为BASH脚本中很少需要有用的东西。使用“tr”可以简单地过滤掉不需要的字符:


请提供
sed
的输出。很抱歉,添加了编辑/附加信息。这非常有效,谢谢!我只是不太明白这是怎么回事。那么您是说“curl”的输出是这样的格式的:\rquick\rbrown\fox输出以流的形式返回,带回车符?好的……但是为什么“粘贴”决定覆盖“sed”输出?它是在最后一次返程时取的吗?只是试着去理解。谢谢你的帮助。我制作了一些关于这个主题的例子。