OS_Linux&Unix

[Linux/Unix] grep 명령어 / -H, -v, -i, -E, -W

최선을 다하자! 2022. 12. 2. 11:03

# grep -E (= egrep) 

 

-E 옵션 + ' | '는 OR 이다.

 

### grep -E '9000' ubbsimple     

= grep '9000' ubbsimple  (grep -E 와 차이가 없다)

 

=> ubbsimple 파일에서 9000으로 된 내용 출력

 

### grep -E '9000|5000' ubbsimple

=> ubbsimple 파일에서 9000 OR 5000으로 된 내용 출력

 

# grep -E '9000' ubbsimple
               CLOPT="-A -- -n //111.110.82.222:9000 -w JSH -m 3 -M 5 -x 3"
# grep -E '9000|5500' ubbsimple
               CLOPT="-A -- -n //111.110.82.222:9000 -w JSH -m 3 -M 5 -x 3"
               CLOPT="-A -- -n //111.110.82.222:5500 -w WSH -t 15 -m 3 -M 10 -x 10"

 

 

# grep -v 

 

-v  : 해당되는 내용을 제외한 나머지를 모두 출력하는 옵션

 

### grep -E -v '9000|5500' ubbsimple

=> ubbsimple 파일 내에 9000 or 5500으로된 내용을 제외한 나머지를 모두 출력

 

grep -E -v '9000|5500' ubbsimple

# JOLT --------------------------------------------------------------------------------------------------------

JSL             SRVGRP=JOLTGRP SRVID=600 MAXGEN=255

JREPSVR         SRVGRP=JOLTGRP SRVID=700 MAXGEN=255
                CLOPT="-A -- -W -P /ofm/jwchoi/sw/tp/tux103/samples/atmi/simpapp/jrepository"


# Workstation Listener Server-----------------------------------------------------------------------------------

WSL            SRVGRP=WSLGRP SRVID=2000
               MAXGEN=255      GRACE=86400

 

 

# grep -H 

 

-h :  해당 파일들의 이름은 출력하지 않는다.

 

-H : 해당 내용을 가지고 있는 파일들의 이름과 해당 내용을 함께 출력한다.

 

### grep -E -H 'TOUPPER|TOLOWER' *

=> TOUPPER or TOLOWER 를 가진 파일들을 (*)모두 출력하고 내용도 함께 출력.

이때 해당 내용을 가진 라인이 모두 출력된다.

# grep -E -H 'TOUPPER|TOLOWER' *
README:service.  The service is called TOUPPER.  The client is run with
README: buildserver -o simpserv -f simpserv.c -s TOUPPER
README: suspending the TOUPPER service, and then running the client.)
infile:service=TOUPPER
infile:service=TOLOWER
jrepository:#BK#TOUPPER▒WxTOLOWER▒▒xTRANSFER▒Gsv:7:TOUPPER:st:7:service:ex:1:Y:bt:6:STRING:BT:6:STRING:tsv:7:TOUPPER:pn:6:STRING:po:1:1:ro:1:1:pt:6:string:pa:5:inout:sv:7:TOLOWER:st:7:service:ex:1:Y:bt:6:STRING:BT:6:STRING:tsv:7:TOLOWER:pn:6:STRING:po:1:1:ro:1:1:pt:6:string:pa:5:inout:sv:8:TRANSFER:st:7:service:ex:1:Y:bt:3:FML:BT:3:FML:sd:56:This service transfers money from one account to another:tsv:8:TRANSFER:pn:10:ACCOUNT_ID:po:1:2:ro:1:2:pt:7:integer:pa:2:in:pd:62:The withdrawal account is 1st, and the deposit account is 2nd.:pn:7:SAMOUNT:po:1:1:ro:1:1:pt:6:string:pa:2:in:pd:61:This is the amount to transfer. It must be greater than zero.:pn:8:SBALANCE:po:1:2:ro:1:2:pt:6:string:pa:3:out:pd:62:The withdrawal account is 1st, and the deposit account is 2nd.:pn:7:STATLIN:po:1:1:ro:1:1:pt:6:string:pa:3:out:
simpcl.c:       /* Request the service TOUPPER, waiting for a reply */
simpcl.c:       ret = tpcall("TOUPPER", (char *)sendbuf, 0, (char **)&rcvbuf, &rcvlen, (long)0);
simpcl.c:               (void) fprintf(stderr, "Can't send request to service TOUPPER\n");
simpserv.c:TOUPPER(TPSVCINFO *rqst)
simpserv.c:TOUPPER(rqst)
ubbsimple:#TOUPPER SVCTIMEOUT=21
ubbsimple:#TOLOWER SVCTIMEOUT=21

 

### grep -E -H 'TOUPPER|TOLOWER|service' ubbsimple simpcl.c infile

=> ubbsimple, simpcl.c infile 파일에서 TOUPPER|TOLOWER|service 로 된 내용을 출력하고 파일들의 이름도 함께 출력

 

# grep -E -H 'TOUPPER|TOLOWER|service' ubbsimple simpcl.c infile
ubbsimple:#TOUPPER SVCTIMEOUT=21
ubbsimple:#TOLOWER SVCTIMEOUT=21
simpcl.c:       /* Request the service TOUPPER, waiting for a reply */
simpcl.c:       ret = tpcall("TOUPPER", (char *)sendbuf, 0, (char **)&rcvbuf, &rcvlen, (long)0);
simpcl.c:               (void) fprintf(stderr, "Can't send request to service TOUPPER\n");
infile:service=TOUPPER
infile:service=TOLOWER
infile:service=TRANSFER
infile:svcdescription=This service transfers money from one account to another

 

 

# grep -i 

 

-i : 출력하고자하는 문자의 대소문자를 구분없이 출력한다.

 

### grep -E -H -i 'toupper|tolower|service' ubbsimple infile simpcl.c simpserv.c

=> ubbsimple infile simpcl.c simpserv.c 파일들에서 대소문자 구문 없이 OR 옵션 추가하여 파일이름과 내용들을 출력한다.

# grep -E -H -i 'toupper|tolower|service' ubbsimple infile simpcl.c simpserv.c
ubbsimple:MAXSERVICES     500
ubbsimple:*SERVICES
ubbsimple:#TOUPPER SVCTIMEOUT=21
ubbsimple:#TOLOWER SVCTIMEOUT=21
infile:service=TOUPPER
infile:service=TOLOWER
infile:service=TRANSFER
infile:svcdescription=This service transfers money from one account to another
simpcl.c:       /* Request the service TOUPPER, waiting for a reply */
simpcl.c:       ret = tpcall("TOUPPER", (char *)sendbuf, 0, (char **)&rcvbuf, &rcvlen, (long)0);
simpcl.c:               (void) fprintf(stderr, "Can't send request to service TOUPPER\n");
simpserv.c:/* This function performs the actual service requested by the client.
simpserv.c:TOUPPER(TPSVCINFO *rqst)
simpserv.c:TOUPPER(rqst)
simpserv.c:             rqst->data[i] = toupper(rqst->data[i]);

 

 

 

 

# grep -W

 

-W : 단어가 아닌 전체 문장이 맞아야 출력된다. 당연히 공백 하나하나도 출력되기 때문에, 공백도 맞아야한다.