Shell Print text between specified lines or patterns

awk,grep,sed,head,tail 都可以根据条件打印部分行。最简单的方法是使用 grep 打印匹配模式的行。不过,最全能的工具还是 awk.

Example 1: 打印从 M 行到 N 行之间的文本

$ awk 'NR==M,NR==N' filename

也可以从stdin读取输入:
cat filename | awk 'NR==M, NR==N'

Example 2: 打印位于模式 start_pattern 与 end_pattern 之间的文本

awk '/start_pattern/,/end_pattern/' filename

awk中使用的模式为正则表达式

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2019-2024 John Doe
  • Visitors: | Views:

请我喝杯咖啡吧~