在定界符上分割标准输出并随机选择行?

BuckDancer

如何在定界符上分割命令的输出(在本例中为新行),然后随机选择一行?

我正在尝试做的一个例子是:

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.
G-Man说“恢复莫妮卡”

简单的答案是将命令传递到shuf -n1SHUF 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\nsed s命令中包含没有意义-一次sed处理一行输入,除非您告诉它另外做一次,并且行从不包含 \n

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在定界符上分割长行

重击:在定界符上分割,但保留定界符

在定界符上分割字符串,在分割前保留定界符

r在定界符上分割而不在括号中

如何在非转义定界符上正确分割?

从右在定界符4上分割数据框

在多个定界符上分割字符串

在定界符上分割,但后跟空格时除外

用Java中的定界符行分割文件

根据定界符分割文件,然后加入单独的行

读取文本文件,按定界符分割,从特定行获取数据并将输出保存到文件

Python在定界符上分割字符串,但不在此定界符的某些变体上

Bash:如何用定界符分割一行并在新行上打印每个元素?

Excel问题:空行上的行拆分/定界符

perl从文件逐行分割定界符

熊猫:如何分割多个定界符?

如何用定界符分割列

用定界符分割字节

如何用定界符划分输出?

在一行上的两个定界符之间(包括两个定界符)打印文本

在多个定界符上分割字符串,但不能单独在空格上

如何在读取标准输入时打印出定界符并允许用户编辑行?

如何用定界符分割熊猫列并选择首选元素作为替换

在keyevent上更改定界符

在两个定界符上分割字符串?

Excel:在定界符上分割字符串并确定结果数组的长度

如何在多个定界符上分割字符串(String或&str)?

在定界符上从字符串的末尾分割固定次数

在Haskell中任意长度的多个定界符上分割字符串