截断管道文本中,在一个命令代替省略号 [英] Truncate middle of piped text and replace with ellipsis in one command

查看:96
本文介绍了截断管道文本中,在一个命令代替省略号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很长的文本流,这是开始发送到标准输出。在bash中,我可以通过运行显示的前几行:

I've got a very long text stream, which is begin sent to stdout. In bash I can show the first few lines by running:

$ seq 1 5010 | head 5
1
2
3
4
5

或年末

$ seq 1 5010 | tail 5
5006
5007
5008
5009
5010

不过,我需要显示前5个和最后5和中间的省略号......所以输出可能类似于

However I need to show the first 5 and last 5 and an elipsis in the middle ... So the output might look like

1
2
3
4
5
...
5006
5007
5008
5009
5010

我要做到这一点,在股票的bash一个班轮。那可能吗?怎么样?

I need to do this in a one-liner in stock bash. Is that possible? How?

推荐答案

这里有一个庆典(V4)解决方案:

Here's one bash (v4) solution:

seq 1 5010 | { mapfile -tn 5 a; printf "%s\n" "${a[@]}" ...; tail -n 5; }

在V3,下面应该工作(尽管这是一个更长的时间线):

On v3, the following should work (although it's a much longer line):

seq 1 5010 | {
  for x in 1 2 3 4 5; do IFS= read -r; echo "$REPLY"; done;
  echo ...;
  tail -n 5;
}

这篇关于截断管道文本中,在一个命令代替省略号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆