【问题标题】:Why is the console output returning to the beginning of the line? OR Why is my concatenation not concatenating?为什么控制台输出返回到行首?或者为什么我的串联没有串联?
【发布时间】:2009-07-14 23:10:38
【问题描述】:

我受到另一个问题的启发,写了一个脚本(或者更确切地说是一个单行)来抓取随机的 Wikipedia 页面。

这是我目前所得到的:

# Grab the HTTP header response from Wikipedia's random page link
curl 'http://en.wikipedia.org/wiki/Special:Random' -sI

# Search STDIN for the Location header and grab its content
perl -wnl -e '/Location: (.*)/ and print $1;'

这很有效。它向控制台输出一个随机的 Wikipedia URL。但我需要在该 url 上附加“?printable=yes”才能获得没有所有非文章内容的维基百科页面。

但是,运行:

curl 'http://en.wikipedia.org/wiki/Special:Random' -sI | perl -wnl -e '/Location: (.*)/ and print $1 . "?printable=yes";'

输出: ?printable=yespedia.org/wiki/James_Keene_(footballer)

为什么我的串联没有串联?

更新:

对于好奇的人,这里是完成的单行:

curl `curl 'http://en.wikipedia.org/wiki/Special:Random' -sI | perl -wnl -e '/Location: ([^\r]*)/ and print $1 . "?printable=yes";'`

【问题讨论】:

  • 请将标题改写成更有意义的内容。
  • 我愿意接受建议。

标签: perl shell curl


【解决方案1】:
curl 'http://en.wikipedia.org/wiki/Special:Random' -sI | perl -wnl -e '/Location: (.*)/ and chomp($1) and print $1 . "?printable=yes";'

未经测试,但这应该可以。返回到行首是由 Location 行末尾的流氓 '\r' 字符引起的。该脚本正在打印 Wikipedia URL,并带有“\r”,它返回到行首,然后继续打印?printable=yes。 Chomp 将删除那个 '\r' 字符。

【讨论】:

  • 不知何故,所有设法做的就是在输出前添加一个 0。但是,您对原因是正确的,将正则表达式更改为 /Location: ([^\r]*)/ 就可以了。谢谢。
  • 好吧,0 是因为我没有费心去阅读 chomp 在 Perl 中是如何使用的;最近一直在使用 Ruby,所以我认为它是一样的。它所做的是打印 $1,什么都不吃(​​它返回 0,因为它删除了 0 个字符)并连接并打印它,然后打印其余的。我已经更新了我的答案以正确使用 chomp,尽管您所做的也同样有效。 :)
  • 该死的,我讨厌 Windows 中的 CRLF 方法。由于 '\r's 就在昨天,我花了将近半个小时来调试一个多行正则表达式。
  • @Martinho:响应行末尾的 CRLF 与 Windows 无关...见w3.org/Protocols/rfc2616/rfc2616-sec4.html
  • 锚定正则表达式并仅捕获您需要的内容可以避免问题。
猜你喜欢
  • 1970-01-01
  • 2014-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-16
  • 1970-01-01
相关资源
最近更新 更多