bash - 如何从文件中读取前 n 行和后 n 行?

标签 bash awk sed head tail

如何读取文件的前n行和后n行?

对于 n=2,我读取了 online (head -n2 && tail -n2) 会起作用,但它不会。

$ cat x
1
2
3
4
5
$ cat x | (head -n2 && tail -n2)
1
2

n=2 的预期输出为:

1
2
4
5

最佳答案

head -n2 file && tail -n2 file

关于bash - 如何从文件中读取前 n 行和后 n 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28615961/

相关文章:

arrays - Bash shell 脚本 : How to replace characters at specific byte offsets

linux - 如何在 Bash 中取消设置以特定前缀开头的所有变量?

json - 将 bash 变量写入 JSON 模板文件

bash - 希望在 BASH 脚本中获取 PID 的环境变量

bash - 如何删除分隔文件中标签之间的空格?

regex - Shell:如何用保存的变量值替换文本的某些内容

unix - 在除 1 、 2、 3 和最后一行之外的所有行末尾添加逗号

shell - 如何使用 sed 从文件中删除一些字符?

perl - 使用 sed、awk 或 perl 从行中提取特定模式

sed - 我可以更改列之间有多少个空格 'column -t' 输出吗?