Cloud Engineering/Linux 🐧

[Linux] grep λͺ…λ Ήμ–΄λ‘œ 파일 문자 νŒ¨ν„΄ κ²€μƒ‰ν•˜κΈ°

minjiwoo 2022. 12. 28. 18:05
728x90

μ•Œμ•„λ‘λ©΄ νŽΈν•œ grep λͺ…λ Ήμ–΄!! 

grep 은 ν•˜λ‚˜ μ΄μƒμ˜ νŒŒμΌμ—μ„œ 문자 νŒ¨ν„΄μ„ 검색할 λ•Œ μ‚¬μš©ν•œλ‹€. 그리고 νŒ¨ν„΄μ„ κ²€μƒ‰ν•œ κ²°κ³Όλ₯Ό μ‰˜ 화면에 좜λ ₯ν•œλ‹€. 

grep option pattern filename 

κ³Ό 같은 ν˜•μ‹μœΌλ‘œ μ“΄λ‹€. 

grep linux ccc 

ccc λΌλŠ” fileμ—μ„œ linux λΌλŠ” νŒ¨ν„΄μ„ κ²€μƒ‰ν•˜λŠ” λͺ…령어이닀. κ²°κ³ΌλŠ” λ‹€μŒκ³Ό 같이 화면에 좜λ ₯ν•΄μ€€λ‹€. 

 

grep -v pattern filename
νŒ¨ν„΄μ„ μ œμ™Έν•˜κ³  κ²€μƒ‰ν•œλ‹€. 

grep -c pattern filename
검색 νŒ¨ν„΄κ³Ό λ§€μΉ­λ˜λŠ” 쀄 개수λ₯Ό 좜λ ₯ν•œλ‹€. 

grep -w pattern filename
단어 λ‹¨μœ„λ‘œ κ²€μƒ‰ν•œλ‹€. 

grep λͺ…λ Ήμ–΄μ—μ„œ μ •κ·œν‘œν˜„μ‹ λ©”νƒ€λ¬Έμžλ„ ν™œμš©ν•  수 μž‡λ‹€. 

^ : μ€„μ˜ μ‹œμž‘ μ§€μ • 

root@user-Aspire-E5-576:/home/user# grep ^hello ccc
hello hello world 
hello my name is linux

$ : μ€„μ˜ λ§ˆμ§€λ§‰ μ§€μ • 

root@user-Aspire-E5-576:/home/user# grep linux$ ccc
hello my name is linux

[] : ν•œ 문자 λŒ€μΉ˜ ν•˜κ³  검색 -> [hH]ello 이런 μ‹μœΌλ‘œ ν‘œκΈ°ν•œλ‹€. 

root@user-Aspire-E5-576:/home/user# grep [hH]ello ccc
hello hello world 
hello my name is linux
Hello my name is Linux

[^] νŒ¨ν„΄ 쀑 μ œμ™Έν•  문자 
root@user-Aspire-E5-576:/home/user# grep ^[a-z] /etc/passwd 

μ†Œλ¬Έμžλ₯Ό μ œμ™Έμ‹œμΌ°μœΌλ―€λ‘œ, λŒ€λ¬Έμžμ™€ μˆ«μžκ°€ ν¬ν•¨λœ νŒ¨ν„΄λ“€μ„ ν‘œμ‹œν•΄μ€€λ‹€. 

728x90