区切り文字で標準出力を分割し、行をランダムに選択しますか?

区切り文字で標準出力を分割し、行をランダムに選択しますか?

コマンド出力を区切り文字(この場合は新しい行)に分割し、ランダムに行を選択するにはどうすればよいですか?

私がやりたいことの例は次のとおりです。

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.

改行文字を分割して1つだけを選択して次の結果を得るにはどうすればよいですか?

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.

ベストアンサー1

簡単な答えはコマンドをshuf -n1シェン入力(行)を任意の順序に変換し、そのうちの1つ(つまりランダムに選択)を出力します。

ただし、(curl … | pup …)コマンドは空行を出力します。私はあなたが最終出力から空白行の1つを取得したくないと思います。  shuf 空行を無視するオプションはないようです。空白行を表示するには、まず削除する必要があります  shuf。一般的なアプローチは

curl -s http://www.compciv.org/files/pages/nyt-sample/ pup 'article p text{}' \
        | sed 's/^[ \r\n\t]*//'| grep '.'    |スープ-n1

ただし、既存のコマンドラインはすでにcommandで終わっているため、sedこれを利用できます。

curl -s http://www.compciv.org/files/pages/nyt-sample/ pup 'article p text{}' \
        |sed -e 's/^[ \r\n\t]*//' -e '/^$/d'|スープ-n1

\nPS:あなたのウェブサイトにそれを含めることは意味がありません。sed sコマンド -sed特に指定しない限り、一度に1行ずつ入力を処理し、行は決して処理しません。含む \n

おすすめ記事