배우는 내용 : [ grep 으로 문자열 검색해보기]

 

문자열 검색

grep [옵션] <검색 패턴> <파일 이름>

 

검색패턴 bash 이용한 예제
( -n : 행 번호도 출력, -v : 검색패턴을 포함하지않는 행 출력)

$ grep bash /etc/passwd
root:x:0:0:root:/root:/bin/bash
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash

$ grep -n bash /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
34:ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash

$ grep -nv bash /etc/passwd
2:daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
3:bin:x:2:2:bin:/bin:/usr/sbin/nologin
...

 

대소문자를 구별하지 않고 검색

$ grep -i system /etc/passwd
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
systemd-network:x:100:102:systemd Network Management,,,:/run/systemd:/usr/sbin/nologin

 

조합

/etc 디렉터리에 ne 이라는 글자를 포함하는 파일을 검색

$ ls /etc | grep ne
issue.net
kernel
needrestart
netconfig
...

 

/etc 디렉터리에 ne 이라는 글자를 정규표현식 ^ 을 이용하여 검색

$ ls /etc | grep '^ne'
needrestart
netconfig
netplan
...

tail : 마지막 부분 출력하기, diff: 두 파일의 차이점 출력하기

tail

$ tail -n 5 <file> # 파일의 마지막 5행 출력
$ tail <file>      # 파일의 마지막 10행 출력

 

 

실시간으로 내용을 출력하여 파일을 모니터링 -> 주로 로그를 모니터링

tail -f <파일 이름>

 

$ tail -f output.log

diff

diff [옵션] <비교 파일 1> <비교 파일 2>

 

diff 명령어 테스트

$ diff temp_list.txt temp_listcp.txt
2c2
< lrwxrwxrwx   1 root root     7 Mar  1 02:07 bin -> usr/bin
---
> lrwxrwxrwx   1 root root     7 Mar  1 02:07 bin -> change test
7,10d6
< lrwxrwxrwx   1 root root     7 Mar  1 02:07 lib -> usr/lib
< lrwxrwxrwx   1 root root     9 Mar  1 02:07 lib32 -> usr/lib32
< lrwxrwxrwx   1 root root     9 Mar  1 02:07 lib64 -> usr/lib64
< lrwxrwxrwx   1 root root    10 Mar  1 02:07 libx32 -> usr/libx32
24a21
> ---diff command test

# 첫번째 파일의 2번째 행이 두번째 파일의 2번째 행으로 변경 :  기호  c

2c2
< lrwxrwxrwx   1 root root     7 Mar  1 02:07 bin -> usr/bin
---
> lrwxrwxrwx   1 root root     7 Mar  1 02:07 bin -> change test

 

# 첫번째 파일의 7~10행이 삭제됨 :  기호  d

7,10d6
< lrwxrwxrwx   1 root root     7 Mar  1 02:07 lib -> usr/lib
< lrwxrwxrwx   1 root root     9 Mar  1 02:07 lib32 -> usr/lib32
< lrwxrwxrwx   1 root root     9 Mar  1 02:07 lib64 -> usr/lib64
< lrwxrwxrwx   1 root root    10 Mar  1 02:07 libx32 -> usr/libx32

 

# 첫번째 파일의 24행 뒤에 두번째 파일의 21번행에 있는 내용이 추가됨: 기호 a
24a21
> ---diff command test

 

통일포맷 옵션 -u : 변화를 더 쉽게 확인 하는 방법

$diff -u temp_list.txt temp_listcp.
--- temp_list.txt	2024-03-31 10:01:39.331756376 +0000
+++ tmpe_listcp.txt	2024-04-03 04:33:22.347779185 +0000
@@ -1,13 +1,9 @@
 total 64
-lrwxrwxrwx   1 root root     7 Mar  1 02:07 bin -> usr/bin
+lrwxrwxrwx   1 root root     7 Mar  1 02:07 bin -> change test
 drwxr-xr-x   4 root root  4096 Mar 27 06:50 boot
 drwxr-xr-x  16 root root  3260 Mar 26 17:32 dev
 drwxr-xr-x  96 root root  4096 Mar 29 06:15 etc
 drwxr-xr-x   3 root root  4096 Mar 15 23:42 home
-lrwxrwxrwx   1 root root     7 Mar  1 02:07 lib -> usr/lib
-lrwxrwxrwx   1 root root     9 Mar  1 02:07 lib32 -> usr/lib32
-lrwxrwxrwx   1 root root     9 Mar  1 02:07 lib64 -> usr/lib64
-lrwxrwxrwx   1 root root    10 Mar  1 02:07 libx32 -> usr/libx32
 drwx------   2 root root 16384 Mar  1 02:10 lost+found
 drwxr-xr-x   2 root root  4096 Mar  1 02:07 media
 drwxr-xr-x   2 root root  4096 Mar  1 02:07 mnt
@@ -22,3 +18,4 @@
 drwxrwxrwt  12 root root  4096 Mar 31 05:26 tmp
 drwxr-xr-x  14 root root  4096 Mar  1 02:07 usr
 drwxr-xr-x  14 root root  4096 Mar 16 02:30 var
+---diff command test

깃허브에서 보여주는 방식과 유사하게 두 파일의 차이점을 보여준다.

@@ -<첫 번째 파일의 시작 행>,<시작행으로 부터 출력된 행의 수> +<두번째 파일의 시작 행><시작 행으로 부터 출력된 행의 수> @@

@@ -1,13 +1,9 @@

-> 첫 번째 파일의  1 행 부터 시작해서  13 개의 행을 출력

-> 두 번째 파일의  1 행 부터 시작해서  9 개의 행을 출력

 

cut : 입력의 일부를 추출하여 출력하는 명령어, tr: 문자 치환과 삭제

cut

$ cut -d <구분자> -f <필드 번호> [<파일 이름>]

 

<구분자>로 지정한 문자를 기준으로 입력 데이터를 분할하여 그중에서 <필드 번호> 로 지정한 필드만 출력

(ex) cut -d , -f 3 file.csv  :: 구분자 , 를 기준으로 분할하여 3 번째 필드만 출력

 

실전

$ cat etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
...
$ cut -d : -f 7 /etc/passwd
/bin/bash
/usr/sbin/nologin
/usr/sbin/nologin
....

 

응용

$ cut -d : -f 1,6,7 /etc/passwd
root:/root:/bin/bash
daemon:/usr/sbin:/usr/sbin/nologin
bin:/bin:/usr/sbin/nologin
..

tr

표준 입력을 읽고 주어면 명령어에 따라 문자를 치환

파일을 지정불가 

 

문자 치환

$ tr <치환 전 문자> <치환 후 문자>

 

$ cat /etc/passwd | tr : ,       # : -> ,
$ cat /etc/passwd | tr adc ABC   # a->A, b->B, c->C
$ cat /etc/passwd | tr a-z A-Z   # 소문자를 대문자로 치환

 

 

문자 삭제

tr -d <삭제할 문자>
$ cat /etc/passwd | tr -d "\n"

unique 명령어: 중복 제거하기

- 연속된 중복 데이터를 하나만 출력하는 명령어

- 같은 내용이 연속되어 있는 경우에만 중복을 없애는 명령어

 

예시: file1.txt :: 

Seoul Seoul
Seoul
Busan
Busan
busan
1
1
Seoul

 

$ uniqu file1.txt
Seoul Seoul
Seoul
Busan
busan
1
Seoul

 

sort 를 이용하여 중복을 제거하는 방법

$ sort file1.txt | uniq
1
Busan
Seoul
Seoul Seoul
busan

$ sort -u file.txt
1
Busan
Seoul
Seoul Seoul
busan

 

중복 데이터의 개수 세기

$ sort file1.txt | uniq -c
      2 1
      2 Busan
      2 Seoul
      1 Seoul Seoul
      1 busan
      
# uniq -c 결과를 정렬하여 중복이 많은 순으로 출력
$ sort file1.txt | uniq -c | sort -rn
      2 Seoul
      2 Busan
      2 1
      1 busan
      1 Seoul Seoul

# uniq -c 결과를 역 순으로 출력
$ sort file1.txt | uniq -c | sort -n
      1 Seoul Seoul
      1 busan
      2 1
      2 Busan
      2 Seoul

배우는 내용 : [ "wc" : 바이트 수, 단어 수, 행 수 세기 "sort" : 행 단위로 정렬  "ps" 프로세스 아이디(PID)순으로 출력]

 

wc 명령어

출력 내용 예시

$ wc result.txt
# (행 수) (단어 수) (바이트 수) (파일 이름)
    23     206      1136   result.txt

 

- 옵션 사용

$ wc -l result.txt
23   result.txt     # 행 수 표시

$ wc -w result.txt
206  result.txt    # 단어 수 표시

$ wc -c result.txt
1136   result.txt  # 바이트 수 표시

 

- 조합 

$ ls / | wc -l
23               # 루트 디렉터리에 있는 파일과 디렉터리 수 출력 

$ ls / | wc -l -
23 -             # - 을 지정하여 표준입력을 읽음

 

sort 명령어

출력 내용 예시

- 옵션이 없으면 알파벳순으로 정렬 : 이때 ASCII 코드를 기준으로 정렬

-  a : 97   A:65  로 인식

예시 :  sort-test.txt  ->  Busan,  Incheon, Seoul, 10, 5, 1, apple

$ sort sort-test.txt
1
10
5
Busan
Incheon
Seoul
apple

$ sort -r sort-test.txt # -r 역순으로 정렬
apple
Seoul
Incheon
Busan
5
10
1

$ sort -n sort-test.txt # -n 문자를 숫자로 인식하여 정렬
Busan
Incheon
Seoul
apple
1
5
10

$ sort -nr sort-test.txt 
10
5
1
apple
Seoul
Incheon
Busan

 

 

- 파이프를 이용하여 응용 가능

ps  : 프로세스 ID PID 순으로 출력

$ ps x
PID TTY      STAT   TIME COMMAND
   1201 ?        Ss     0:00 /lib/systemd/systemd --user
   1202 ?        S      0:00 (sd-pam)
   4961 ?        Ssl    6:18 PM2 v5.3.1: God Daemon (/home/ubuntu/.pm2)
....

$ ps x | sort -k 5  # 5번째에 있는 COMMAND 필드를 기준으로 정렬
   1202 ?        S      0:00 (sd-pam)
  50248 pts/0    Ss     0:00 -bash
   1201 ?        Ss     0:00 /lib/systemd/systemd --user
    PID TTY      STAT   TIME COMMAND
   4961 ?        Ssl    6:18 PM2 v5.3.1: God Daemon (/home/ubuntu/.pm2)
....

 

ls 명령어로 나온 결과를 파일 크기가 큰 순으로  최상위 3개만 출력 

$ ls -l /bin/ | sort -rn -k 5 | head -n 3
-rwxr-xr-x 1 root root    91859064 Feb 13 21:33 node
-rwxr-xr-x 1 root root    16138392 May 29  2023 snap
-rwxr-xr-x 1 root root     5904904 Nov 20 15:14 python3.10

 

+ Recent posts