将包含多个单词的单行拆分为多行,每行包含x个单词

1 bash split sed xargs

我有一个大文本文件,只包含1行.它看起来像这样:

blaalibababla.ru text text text text what's the weather like tooday? blaazzabla.zu some_text blabewdwefla.au it is important not to be afraid of sed blabkrlqbla.ru wjenfkn lkwnef lkwnefl blarthrthbla.net 1234 e12edq 42wsdfg blablabla.com this should finally end
Run Code Online (Sandbox Code Playgroud)

我需要一种方法使它看起来像这样:

blaalibababla.ru text text text text what's the weather like tooday?
blaazzabla.zu some_text
blabewdwefla.au it is important not to be afraid of sed
blabkrlqbla.ru wjenfkn lkwnef lkwnefl
blarthrthbla.net 1234 e12edq 42wsdfg 
blablabla.com this should finally end
Run Code Online (Sandbox Code Playgroud)

我知道如何使用单个域名来做到这一点sed:

sed -i 's/blablabla.ru/\n&/g' file.txt
Run Code Online (Sandbox Code Playgroud)

"但事后没有补充文字." - 这不是我的意思.

如果sed不是最好的方式,请告诉我.

UPD:这是我的文本文件:

wsd.qwd.qwd.kjqnwk.ru PUPPETD CRITICAL 2017-01-13 00:09:52   lor notify-by-sms FILE_AGE CRITICAL:   /var/lib/puppet/state/state.yaml is 2438046 seconds old and 19459 bytes   zm-goas-04.asdg.net LOAD CRITICAL 2017-01-13 00:10:32   tech-lor notify-by-telegram CRITICAL - load average: 42.91,   49.91, 53.88   glas07.kvm.ext.asdg.ru PUPPETD CRITICAL 2017-01-13 00:28:02   lor notify-by-sms FILE_AGE CRITICAL:   /var/lib/puppet/state/state.yaml is 19821 seconds old and 26337 bytes    
Run Code Online (Sandbox Code Playgroud)

我需要它看起来像:

wsd.qwd.qwd.kjqnwk.ru PUPPETD CRITICAL 2017-01-13 00:09:52   lor notify-by-sms FILE_AGE CRITICAL:   /var/lib/puppet/state/state.yaml is 2438046 seconds old and 19459 bytes   
zm-goas-04.asdg.net LOAD CRITICAL 2017-01-13 00:10:32   tech-lor notify-by-telegram CRITICAL - load average: 42.91,   49.91, 53.88   
glas07.kvm.ext.asdg.ru PUPPETD CRITICAL 2017-01-13 00:28:02   lor notify-by-sms FILE_AGE CRITICAL:   /var/lib/puppet/state/state.yaml is 19821 seconds old and 26337 bytes    
Run Code Online (Sandbox Code Playgroud)

Ini*_*ian 5

用于一次xargs处理n记录的一种更简单的方法,在您的情况下就是这样2

xargs -n2 <file
blablabla.ru some_text
blablabla.zu some_text
blablabla.au some_text
blablabla.ru some_text
blablabla.net some_text
blablabla.com some_text
Run Code Online (Sandbox Code Playgroud)

-n根据man xargs页面的标志是,

-n max-args, --max-args=max-args
      Use at most max-args arguments per command line.  Fewer than max-args arguments 
      will be used if the size (see the -s option) is exceeded, unless the
      -x option is given, in which case xargs will exit.
Run Code Online (Sandbox Code Playgroud)

要替换回原始文件,请执行

xargs -n2 <file >tmpfile; mv tmpfile file
Run Code Online (Sandbox Code Playgroud)