如何在定界符上分割命令的输出(在本例中为新行),然后随机选择一行?
我正在尝试做的一个例子是:
curl -s http://www.compciv.org/files/pages/nyt-sample/ | pup 'article p text{}' \
| sed 's/^[ \r\n\t]*//'
输出:
The fine of $35 million in each of two civil penalties, for a total of $70 million, is a record for the National Highway Traffic Safety Administration.
After becoming a grandmaster at the tender age of 13, Sam Sevian is getting some help from the chess champion Garry Kasparov.
In a small stand against gentrification, the nonprofit Wildflowers Institute has helped San Francisco’s gritty Tenderloin neighborhood define its cultural assets.
The currency has fallen because investors fear that the eurozone is stuck in a quagmire and its leaders are not doing much to pull it out.
President Obama is facing opposition from his party to one of his top priorities: winning the power to negotiate international trade pacts and speed them through Congress.
我该如何在换行符上分割,然后只选择一个,所以我会得到类似
The currency has fallen because investors fear that the eurozone is stuck in a quagmire and its leaders are not doing much to pull it out.
简单的答案是将命令传递到shuf -n1
。此SHUF FLES输入(线)为随机顺序,然后输出它们中的一个(即,随机选择)。
但是您的(curl … | pup …
)命令输出空白行。我想您不想在最终输出中获得这些空白行之一。 shuf
似乎没有忽略空白行的选项;您必须先删除它们,然后才能 shuf
看到它们。通用方法是
curl -s http://www.compciv.org/files/pages/nyt-sample/ | 小狗“物品P文本{} \ | sed's / ^ [\ r \ n \ t] * //' | grep'。' | shuf -n1
但是,由于您现有的命令行已经以sed
命令结尾,因此我们可以背负一下:
curl -s http://www.compciv.org/files/pages/nyt-sample/ | 小狗“物品P文本{} \ | sed -e's / ^ [\ r \ n \ t] * //'-e'/ ^ $ / d' | shuf -n1
PS\n
在sed
s
命令中包含没有意义-一次sed
处理一行输入,除非您告诉它另外做一次,并且行从不包含 \n
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句