programing tip

awk 부분적으로 문자열 일치 (열 / 단어가 부분적으로 일치하는 경우)

itbloger 2020. 11. 10. 07:54
반응형

awk 부분적으로 문자열 일치 (열 / 단어가 부분적으로 일치하는 경우)


내 더미 파일은 다음과 같습니다.

C1    C2    C3    
1     a     snow   
2     b     snowman 
snow     c     sowman

snow$ 3에 문자열이 있으면 줄을 얻고 싶습니다 . 다음과 같이 할 수 있습니다.

awk '($3=="snow" || $3=="snowman") {print}' dummy_file

그러나 더 간단한 방법이 있어야합니다.


awk '$3 ~ /snow/ { print }' dummy_file 

index () 함수로 하위 문자열을 찾아서도 가능합니다.

awk '(index($3, "snow") != 0) {print}' dummy_file

더 짧은 버전 :

awk 'index($3, "snow")' dummy_file

아마도 이것이 도움이 될 것입니다

http://www.math.utah.edu/docs/info/gawk_5.html

awk '$3 ~ /snow|snowman/' dummy_file

세 번째 필드가 다음 중 하나 snow이거나 snowman전용 인 줄을 인쇄 합니다.

awk '$3~/^snow(man)?$/' file

GNU sed

sed '/\s*\(\S\+\s\+\)\{2\}\bsnow\(man\)\?\b/!d' file

입력:

C1 C2 C3    
1 눈   
2b 눈사람 
눈 c sowman
      눈 눈 snowmanx

..산출:

1 눈
2b 눈사람

참고 URL : https://stackoverflow.com/questions/17001849/awk-partly-string-match-if-column-word-partly-matches

반응형