Programing

awk / sed로 여러 번 나타날 수있는 두 마커 패턴 사이에서 선을 선택하는 방법

crosscheck 2020. 7. 25. 10:32
반응형

awk / sed로 여러 번 나타날 수있는 두 마커 패턴 사이에서 선을 선택하는 방법


두 개의 다른 마커 패턴 사이에서 발생하는 선을 사용 awk하거나 sed어떻게 선택할 수 있습니까? 이러한 패턴으로 표시된 여러 섹션이있을 수 있습니다.

예를 들어, 파일에 다음이 포함되어 있다고 가정하십시오.

abc
def1
ghi1
jkl1
mno
abc
def2
ghi2
jkl2
mno
pqr
stu

그리고 시작 패턴 abc과 끝 패턴은 다음 mno같이 출력이 필요합니다.

def1
ghi1
jkl1
def2
ghi2
jkl2

sed를 사용하여 패턴을 한 번 일치시킵니다.

sed -e '1,/abc/d' -e '/mno/,$d' <FILE>

어떠한 방법이 sedawk파일이 끝날 때까지 반복적으로 수행하는가?


awk필요할 때 플래그를 사용 하여 인쇄를 시작하십시오.

$ awk '/abc/{flag=1;next}/mno/{flag=0}flag' file
def1
ghi1
jkl1
def2
ghi2
jkl2

어떻게 작동합니까?

  • /abc/이 텍스트가있는 행과 일치합니다 /mno/.
  • /abc/{flag=1;next}flag텍스트 abc가 발견되는 시간을 설정합니다 . 그런 다음 줄을 건너 뜁니다.
  • /mno/{flag=0}flag텍스트 mno를 찾을 설정을 해제합니다 .
  • 마지막이 flag되는 기본 동작과 패턴 인 print $0경우 : flagIS는 줄이 인쇄된다 (1)과 동일.

패턴이 표시되거나 표시되지 않는 경우에 대한 자세한 설명 및 예 는 두 패턴 사이에서 선을 선택하는 방법을 참조하십시오 . .


사용 sed:

sed -n -e '/^abc$/,/^mno$/{ /^abc$/d; /^mno$/d; p; }'

-n옵션은 기본적으로 인쇄하지 않음을 의미합니다.

The pattern looks for lines containing just abc to just mno, and then executes the actions in the { ... }. The first action deletes the abc line; the second the mno line; and the p prints the remaining lines. You can relax the regexes as required. Any lines outside the range of abc..mno are simply not printed.


This might work for you (GNU sed):

sed '/^abc$/,/^mno$/{//!b};d' file

Delete all lines except for those between lines starting abc and mno


sed '/^abc$/,/^mno$/!d;//d' file

golfs two characters better than ppotong's {//!b};d

The empty forward slashes // mean: "reuse the last regular expression used". and the command does the same as the more understandable:

sed '/^abc$/,/^mno$/!d;/^abc$/d;/^mno$/d' file

This seems to be POSIX:

If an RE is empty (that is, no pattern is specified) sed shall behave as if the last RE used in the last command applied (either as an address or as part of a substitute command) was specified.


From the previous response's links, the one that did it for me, running ksh on Solaris, was this:

sed '1,/firstmatch/d;/secondmatch/,$d'

perl -lne 'print if((/abc/../mno/) && !(/abc/||/mno/))' your_file

Don_crissti's answer from Show only text between 2 matching pattern?

firstmatch="abc"
secondmatch="cdf"
sed "/$firstmatch/,/$secondmatch/!d;//d" infile

which is much more efficient than AWK's application, see here.


something like this works for me:

file.awk:

BEGIN {
    record=0
}

/^abc$/ {
    record=1
}

/^mno$/ {
    record=0;
    print "s="s;
    s=""
}

!/^abc|mno$/ {
    if (record==1) {
        s = s"\n"$0
    }   
}

using: awk -f file.awk data...

edit: O_o fedorqui solution is way better/prettier than mine.


I tried to use awk to print lines between two patterns while pattern2 also match pattern1. And the pattern1 line should also be printed.

e.g. source

package AAA
aaa
bbb
ccc
package BBB
ddd
eee
package CCC
fff
ggg
hhh
iii
package DDD
jjj

should has an ouput of

package BBB
ddd
eee

Where pattern1 is package BBB, pattern2 is package \w*. Note that CCC isn't a known value so can't be literally matched.

In this case, neither @scai 's awk '/abc/{a=1}/mno/{print;a=0}a' file nor @fedorqui 's awk '/abc/{a=1} a; /mno/{a=0}' file works for me.

Finally, I managed to solve it by awk '/package BBB/{flag=1;print;next}/package \w*/{flag=0}flag' file, haha

A little more effort result in awk '/package BBB/{flag=1;print;next}flag;/package \w*/{flag=0}' file, to print pattern2 line also, that is,

package BBB
ddd
eee
package CCC

참고URL : https://stackoverflow.com/questions/17988756/how-to-select-lines-between-two-marker-patterns-which-may-occur-multiple-times-w

반응형