使用awk或sed,如何选择两个相同标记模式之间出现的行?可能有多个标有这些图案的部分。
例如:假设文件包含:
$$$
lines between dollar and AT
@@@
lines between first and second AT
@@@
lines between second and third AT
@@@
lines between third and fourth AT
@@@
使用
cat 2.txt | sed -n '/$$$/,/@@@/p'
我得到$$$和第一次出现@@@之间的内容。
我的疑问是,如何获得第一次和第三次出现@@@ 之间的内容
预期输出为:
lines between first and second AT
@@@
lines between second and third AT
答案 0 :(得分:2)
awk
似乎是这个工作的更好的工具,主要是因为它允许你比sed更容易在命令行上指定参数(也就是说,根本就是),并且因为它可以处理数字三立。
我使用
awk -v pattern='^@@@$' -v first=1 -v last=3 '$0 ~ pattern { ++count; if(count == first) next } count == last { exit } count >= first' 2.txt
其工作原理如下:
$0 ~ pattern { # When the delimiter pattern is found:
++count # increase counter.
if(count == first) { # If we found the starting pattern
next # skip to next line. This handles the fencepost.
}
}
count == last { # If we found the end pattern, stop processing.
exit
}
count >= first # Otherwise, if the line comes after the starting
# pattern, print the line.