bash - 提取 shell 中两个行号之间的行

标签 bash shell sed

这在 shell 中如何使用 sed 或任何其他过滤器实现

假设我在两个变量 $line1$line2 中有两个特定的行号,我想像这样提取这两行之间的行

cat my_file_path | command $line1 $line2

例如我的文件是:

1:bb

2:cccc

3:dddd

4:eeeeee

如果我这样做,我的输出应该是:

cat my_file_path | command 1 3

输出

bbb
cccc

最佳答案

使用 sed:

$ cat my_file_path | sed -n "${line1},${line2}p"

或者,甚至更好(cat 在某种程度上是多余的):

$ sed -n "${line1},${line2}p" my_file_path

关于bash - 提取 shell 中两个行号之间的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21759562/

相关文章:

Linux 狂欢。如果字段完全匹配则删除行

bash - 如何在 Bash 的复杂条件表达式中使用逻辑非运算符?

shell - 如何将浮点表达式的输出分配给shell脚本中的变量?

Linux Bash 脚本

http - 如何从 shell 脚本中使用 pastebin?

linux - 查看路径的所有中间目录的权限?

bash - 如何将base64编码的内容传递给sed?

linux - 我如何在 linux 上对字符串进行 base64 编码,使其与 windows "Unicode.GetBytes.ToBase64String"匹配?

bash - 在进行 ffmpeg 转换时将元数据值转换为字符串

regex - 使用正则表达式通过 Bash 检查范围