我想打破一个长字符串的字数,然后继续显示和打破一旦达到一定的字数。
例如。
我有一根绳子:
value="Aug 04 03:49:00.082205 ALERT IPX-NG dropped -- total: 4693845, count: 39254, rate: 1.88% ; OUTPUT QUEUE frampedd: active=1, delivered=73265000210 Aug 04 09:43:00.795817 ALERT IPX-NG dropped -- total: 4765909, count: 72064, rate: 1.91% ; OUTPUT QUEUE frampedd: active=0, delivered=74220627600"
我的预期产出是:
Aug 04 03:49:00.082205 ALERT IPX-NG dropped -- total: 4693845, count: 39254, rate: 1.88% ; OUTPUT QUEUE frampedd: active=1, delivered=73265000210
Aug 04 09:43:00.795817 ALERT IPX-NG dropped -- total: 4765909, count: 72064, rate: 1.91% ; OUTPUT QUEUE frampedd: active=0, delivered=74220627600"
我不能用字符计数,因为数字会有所不同。所以最好的选择是使用单词计数。
编辑:
嗨,伙计们,…我试着使用sed命令,它看起来很有用!
sed的/([^[:space:]{1,}[[:space:]{1,}){19}/&\n/'
谢谢那些帮助我的人。如果有的话,你可以给我更好的建议。希望使用纯bash命令,因为我无法在服务器上安装任何其他扩展。
最佳答案
如果所有字符串计数都没有改变,则可以使用xargs
$ value="Aug 04 03:49:00.082205 ALERT IPX-NG dropped -- total: 4693845, count: 39254, rate: 1.88% ; OUTPUT QUEUE frampedd: active=1, delivered=73265000210 Aug 04 09:43:00.795817 ALERT IPX-NG dropped -- total: 4765909, count: 72064, rate: 1.91% ; OUTPUT QUEUE frampedd: active=0, delivered=74220627600"
$ xargs -n 19 <<< "$value"
Aug 04 03:49:00.082205 ALERT IPX-NG dropped -- total: 4693845, count: 39254, rate: 1.88% ; OUTPUT QUEUE frampedd: active=1, delivered=73265000210
Aug 04 09:43:00.795817 ALERT IPX-NG dropped -- total: 4765909, count: 72064, rate: 1.91% ; OUTPUT QUEUE frampedd: active=0, delivered=74220627600
xargs man
页上写着这个关于标志-n
-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.