awk捕获两个模式之间重复的线组 [英] awk to capture repeating groups of lines between two patterns

查看:69
本文介绍了awk捕获两个模式之间重复的线组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,例如:

I have a text file like:

$ cat test.txt
one
two
--------------
three
four
--------------
five
six
--------------
seven
eight
--------------
nine
ten

我正在尝试使用awk捕获--------------的两行之间的文本.

I am trying to use awk to capture the text between the two lines of --------------.

我正在尝试的输出:

$ cat test.txt
three
four

seven
eight

这是我到目前为止所拥有的

Here is what I have so far

awk '/^--------------$/{flag=1;next}/^--------------$/{flag=0}flag' test.txt
three
four
five
six
seven
eight
nine
ten

推荐答案

$ awk '{x=sub(/^-+$/,"")} f; x{f=!f}' file
three
four

seven
eight

sub()用空格替换所有---行,并保存它在x中这样做的事实,以记住当前行是---行.对于每个---行,x(f=!f}在0和1之间切换f.当f为true时,f;打印当前行,因为{print}是给定条件的默认操作.

The sub() replaces all --- lines with blanks and saves the fact it did that in x to remember that the current line was a --- line. The x(f=!f} toggles f between 0 and 1 for every --- line. The f; prints the current line when f is true since {print} is the default action given a true condition.

这篇关于awk捕获两个模式之间重复的线组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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