![AWK의 역정규식?](https://rvso.com/image/97228/AWK%EC%9D%98%20%EC%97%AD%EC%A0%95%EA%B7%9C%EC%8B%9D%3F.png)
특정 단어가 포함된 줄을 필터링하려고 합니다. 정규식은 스크립트에 대한 명령줄 입력입니다.
$0 ~ regex {
//Do something.
}
샘플 입력은 다음과 같습니다.
**String** **number**
domain 1
domain 2
bla 3
따라서 위 입력에서 사용자는 "domain"이라는 단어가 있는 행을 필터링한다고 말할 수 있습니다.
내가 시도한 것:
regex = "\?\\!domain"
(부정적 예측).
하지만 이 정규식은 모든 행을 필터링합니다. "도메인"이라는 단어가 포함된 행만이 아닙니다.
답변1
input
다음을 포함하는 특정 입력 파일의 경우 :
domain
demesne
다음을 포함하는 행을 필터링하려면 domain
:
$ awk '/domain/ { print }' input
domain
라인을 필터링하려면~ 아니다다음을 포함 domain
:
$ awk '!/domain/ {print }' input
demesne
필터링 기준으로필드전체 라인 대신 새로운 주어진 input
파일에 대해 다음을 시도해 볼 수 있습니다.
example www.example.com
exemplar www.example.net
첫 번째 필드가 있는 행을 필터링하려면포함 example
:
$ awk '$1 !~ /example/ { print }' input
exemplar www.example.net
$0
귀하의 질문에서는 첫 번째 필드가 아닌 전체 줄을 사용했습니다 .
답변2
라인을 필터링하는 더 유연하고 강력한 또 다른 방법은 다음과 같습니다 {next}
.
- 모든 줄을 인쇄하려면하지 마라주어진 와 일치하려면
regex
다음을 수행하십시오.awk '/regex/ {next} {print}' inputfile
다음과 같이 이 방법을 사용하면 특정 두 줄 사이의 모든 줄을 필터링할 수도 있습니다.
다음 줄을 모두 인쇄하려면아니다일치하는 줄
regex1
과 일치하는 첫 번째 다음 줄 사이에서regex2
다음을 수행하십시오.awk '/regex1/,/regex2/ {next} {print}' inputfile
이 방법으로는 불가능합니다
awk '!/regex/'
(내가 착각하지 않은 경우).
예를 들어, inputfile
의 내용이 다음과 같은 경우:
hello, here is my confidential information
SECRET INFO BEGIN
xx
x
xxxxx
xxxx
xxxx
xxxxx
xs
sdf
sdfsdfw
wefwe
SECRET INFO END
This is the end of my message
그러면 이 명령은 다음과 awk '/SECRET INFO BEGIN/,/SECRET INFO END/ {next} {print}' inputfile
같이 인쇄됩니다.
hello, here is my confidential information
This is the end of my message
답변3
echo 'hello, here is my confidential information
SECRET INFO BEGIN
xx
x
xxxxx
xxxx
xxxx
xxxxx
xs
sdf
sdfsdfw
wefwe
SECRET INFO END
This is the end of my message' |
mawk 'BEGIN { _^= FS = RS } ! /SECRET INFO/ ? _ : _ * (_=!_)'
gawk 'BEGIN { _^= FS = "SECRET INFO" } _*(NF <= _^_ || _=!_)'
hello, here is my confidential information
This is the end of my message