본문으로 바로가기

: grep 명령어는 입력으로 전달된 파일의 내용에서 특정 문자열을
  찾고자할 때 사용하는 명령어입니다.

매뉴얼 페이지

 

 

 

 

 

 

 

0> 도표

option
내용
기타
-r
recursive (재귀적) : 모든폴더 검색
1-1번항목 참조
--include=GLOB
GLOB 이름의 파일 포함
1-2번항목 참조
--exclude=GLOB
GLOB 이름의 파일 제외
1-3번항목 참조
-v
invert-match : non-matching lines
1-4번항목 참조
-E
extended-regexp : multi patterns
1-5번항목 참조
-i
ignore-case : 대소문자 구분안함
1-6번항목 참조
-s
no-messages (에러만 출력)
1-7번항목 참조
-n
line-number
1-8번항목 참조
-h
no-filename
1-8번항목 참조
-w
--word-regexp (정확한 단어 검색)
1-9번항목 참조
-f
--file (패턴이 있는 파일에서 검색)
1-10번항목 참조
-x
--line-regexp (정확하게 한줄이 맞는 부분 검색)
1-11번 항목 참조
-l, --files-with-matches
패턴 검색시 파일이름만 표시 옵션
1-12번 항목

1> 옵션

1-1> -r 옵션 >> rgrep 과 동일합니다.

1-1-1> 현재 디렉토리 아래의 모든파일/폴더에서 main(void) 찾기 (-r)

-r : subdirectory 전부 검사 (recursive)

$ grep -r pattern

1-1-2> 특정 디렉토리 안의 모든 파일에서 main(void) 찾기

$ grep -r "main(void)" subdir/

$ grep -r "main" ../../components/

>> 상위디렉토리로 이동해서 검색도 됩니다.

1-1-3> 특정 디렉토리 안의 *.c/h 파일에서 BLE_GAP_EVT_AUTH_STATUS 찾기

$ grep -r BLE_GAP_EVT_AUTH_STATUS --include={*.h,*.c} ../../../components/ble/

 

$ grep -r BLE_GAP_EVT_AUTH_STATUS --include={*.h,*.c} ../../../components/ble/
../../../components/ble/ble_services/ble_lls/ble_lls.c:        case BLE_GAP_EVT_AUTH_STATUS:
../../../components/ble/common/ble_conn_state.c:        case BLE_GAP_EVT_AUTH_STATUS:
../../../components/ble/peer_manager/peer_manager.h: *          If the peer ignores the request, a @ref BLE_GAP_EVT_AUTH_STATUS event occurs
../../../components/ble/peer_manager/security_dispatcher.c:/**@brief Function for processing the @ref BLE_GAP_EVT_AUTH_STATUS event from the SoftDevice, when
../../../components/ble/peer_manager/security_dispatcher.c:/**@brief Function for processing the @ref BLE_GAP_EVT_AUTH_STATUS event from the SoftDevice, when
../../../components/ble/peer_manager/security_dispatcher.c:/**@brief Function for processing the @ref BLE_GAP_EVT_AUTH_STATUS event from the SoftDevice.
../../../components/ble/peer_manager/security_dispatcher.c:        case BLE_GAP_EVT_AUTH_STATUS:

 

1-2> --include 옵션

1-2-1> .h 파일에서만 검색하기

$ grep -r "GATTC_SVC" --include="*.h"

>> 현재디렉토리 아래의 모든 .h 파일에서 검색합니다.

1-3> --exclude 옵션

1-3-1> .h 파일 제외하고 검색하기

$ grep -r "GATTC_READ" --exclude="*.h"

1-3-2> *.o 파일과 *.map 은 검색안하게 하기

$ grep -r InitializeUsb

>> 위의 검색 내용중 .o 나 .map 파일 은 검색 안하게 하기

$ grep -r --exclude={*.o,*.map} InitializeUsb

{}를 사용한 한개 검색시는 --exclude={*.o,}

>> InitializeUsb 를 검색하는데 *.o 파일과 *.map 파일은 제외합니다.

>> 위의 멀티패턴 검색시 사용되는 {,} 는 include 옵션에도 사용가능합니다.

1-4> -v 옵션 (--invert-match)

: 매칭되는 패턴 제외 옵션

1-4-1> RAM_START 단어를 찾는데 Binary 라는 글자가 있는 부분은 제외 하기

$ grep -r RAM_START

Binary 라는 글자의 불필요한 부분까지 검색이 되네요.

$ grep -r RAM_START | grep -v Binary

-v option manual page

1-5> -E 옵션 >> egrep 과 동일

1-5-1> 2개의 패턴을 한번에 검색하고 싶을때

pattern1 or pattern2 가 들어가는 라인

$ grep -E "pattern1|pattern2"

>> pattern1 과 pattern2 사이에 space 넣으면 안됩니다.

$ grep -r -E "APDU|BUFF "

>> APDU 가 있거나 BUFF 가 있는 행을 표시해 줍니다.

1-5-2> 2개의 패턴을 제외하고 검색

$ find -name *.txt | head
./netdriver/README.txt
./staging/usbip/usbip_protocol.txt
./staging/speakup/spkguide.txt
./staging/wlags49_h25/README.txt
./staging/wlags49_h2/WARNING.txt
./staging/csr/LICENSE.txt
./staging/zram/zram.txt
./staging/panel/lcd-panel-cgram.txt
./staging/media/davinci_vpfe/davinci-vpfe-mc.txt
./staging/media/go7007/go7007.txt

>> .txt로 끝나는 단어 중 media, wlags 가 포함이 안된 파일 찾기
$ find -name *.txt |grep -v -E "media|wlags"| head
./netdriver/README.txt
./staging/usbip/usbip_protocol.txt
./staging/speakup/spkguide.txt
./staging/csr/LICENSE.txt
./staging/zram/zram.txt
./staging/panel/lcd-panel-cgram.txt
./staging/asus_oled/linux.txt
./staging/asus_oled/tux.txt
./staging/asus_oled/zig.txt
./staging/asus_oled/tux_r2.txt

 

manual page 첨부합니다.

>> 정규표현식 확장 표현에 -E option 이 사용됩니다.

1-6> -i 옵션

: 대소문자 구분 안하고 검색하기

$ grep -ri  "tick" --include="*.c"
main.c:#define BATTERY_LEVEL_MEAS_INTERVAL     APP_TIMER_TICKS(2000)                       /**< Battery level measurement interval (ticks). */
main.c:#define MOUSE_TIMER_1ST_INTERVAL                 APP_TIMER_TICKS(1000)                       /**<  */
main.c:#define MOUSE_TIMER_REP_INTERVAL                 APP_TIMER_TICKS(50)                        /**<  */
main.c:#define FIRST_CONN_PARAMS_UPDATE_DELAY  APP_TIMER_TICKS(5000)                       /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */
main.c:#define NEXT_CONN_PARAMS_UPDATE_DELAY   APP_TIMER_TICKS(30000)                      /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */
main.c: unsigned int tick_count;                // unit is one second.
main.c: unsigned int prev_tick;
main.c: tick_count = prev_tick= 0;
main.c:                 prev_tick = tick_count;
main.c:                 if((tick_count - prev_tick)>= 10){
main.c:                         tick_count = prev_tick=0;

 

 

매뉴얼 페이지

1-7> -s 옵션

: 에러메시지 출력제외 옵션

매뉴얼 페이지

1-7-1>directory 제외하고 파일만 검색하기 (디렉토리 warning 안나오게 하기)

$ grep -s tick *
main.c:#define BATTERY_LEVEL_MEAS_INTERVAL     APP_TIMER_TICKS(2000)                       /**< Battery level measurement interval (ticks). */
main.c: unsigned int tick_count;                // unit is one second.
main.c: unsigned int prev_tick;
main.c: tick_count = prev_tick= 0;
main.c:                 prev_tick = tick_count;
main.c:                 if((tick_count - prev_tick)>= 10){
main.c:                         tick_count = prev_tick=0;


$ grep tick *
main.c:#define BATTERY_LEVEL_MEAS_INTERVAL     APP_TIMER_TICKS(2000)                       /**< Battery level measurement interval (ticks). */
main.c: unsigned int tick_count;                // unit is one second.
main.c: unsigned int prev_tick;
main.c: tick_count = prev_tick= 0;
main.c:                 prev_tick = tick_count;
main.c:                 if((tick_count - prev_tick)>= 10){
main.c:                         tick_count = prev_tick=0;
grep: pca10040: Is a directory
grep: pca10040e: Is a directory
grep: pca10056: Is a directory
grep: pca10056e: Is a directory
grep: pca10100e: Is a directory

 

 

 

-s 제거하고 사용시 아래처럼 directory 관련 문구가 출력됩니다.

1-8> -n 옵션 && -h 옵션

: 옵션 미사용시

1-8-1> n 옵션 (--line-number)

: 라인넘버 표시 옵션

>> main.c 의 2946 라인

 

1-8-2> h 옵션 (--no-filename)

: 파일명 미표시

1-9> -w 옵션

: 정확한 단어검색시 사용

>> 아래 예제는 *.h 파일중에서 NRF_CLOCK 이라는 단어가 들어가는 패턴을 검색합니다.

NRF_CLOCK_ or NRF_CLOCKxxx >> 검색안됨

 

$ grep -r -w NRF_CLOCK --include="*.h"
modules/nrfx/drivers/include/nrfx_power_clock.h:    if (!NRFX_IRQ_IS_ENABLED(nrfx_get_irq_number(NRF_CLOCK)))
modules/nrfx/drivers/include/nrfx_power_clock.h:        NRFX_IRQ_PRIORITY_SET(nrfx_get_irq_number(NRF_CLOCK), priority);
modules/nrfx/drivers/include/nrfx_power_clock.h:        NRFX_IRQ_ENABLE(nrfx_get_irq_number(NRF_CLOCK));
modules/nrfx/hal/nrf_clock.h:    NRF_CLOCK->INTENSET = int_mask;
modules/nrfx/hal/nrf_clock.h:    NRF_CLOCK->INTENCLR = int_mask;
modules/nrfx/hal/nrf_clock.h:    return (bool)(NRF_CLOCK->INTENCLR & int_mask);
modules/nrfx/hal/nrf_clock.h:    return ((uint32_t )NRF_CLOCK + task);
modules/nrfx/hal/nrf_clock.h:    *((volatile uint32_t *)((uint8_t *)NRF_CLOCK + task)) = 0x1UL;
modules/nrfx/hal/nrf_clock.h:    return ((uint32_t)NRF_CLOCK + event);
modules/nrfx/hal/nrf_clock.h:    *((volatile uint32_t *)((uint8_t *)NRF_CLOCK + event)) = 0x0UL;
modules/nrfx/hal/nrf_clock.h:    volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)NRF_CLOCK + (uint32_t)event));
modules/nrfx/hal/nrf_clock.h:    return (bool)*((volatile uint32_t *)((uint8_t *)NRF_CLOCK + event));
modules/nrfx/hal/nrf_clock.h:    NRF_CLOCK->LFCLKSRC = (uint32_t)(source);
modules/nrfx/hal/nrf_clock.h:    return (nrf_clock_lfclk_t)(NRF_CLOCK->LFCLKSRC);
modules/nrfx/hal/nrf_clock.h:    return (nrf_clock_lfclk_t)((NRF_CLOCK->LFCLKSTAT &
modules/nrfx/hal/nrf_clock.h:    return (nrf_clock_lfclk_t)((NRF_CLOCK->LFCLKSRCCOPY &
modules/nrfx/hal/nrf_clock.h:    return ((NRF_CLOCK->LFCLKSTAT &
modules/nrfx/hal/nrf_clock.h:    return (nrf_clock_start_task_status_t)((NRF_CLOCK->LFCLKRUN &
modules/nrfx/hal/nrf_clock.h:    return (nrf_clock_hfclk_t)((NRF_CLOCK->HFCLKSTAT &
modules/nrfx/hal/nrf_clock.h:    return (NRF_CLOCK->HFCLKSTAT & (CLOCK_HFCLKSTAT_STATE_Msk | CLOCK_HFCLKSTAT_SRC_Msk)) ==
modules/nrfx/hal/nrf_clock.h:    return (nrf_clock_start_task_status_t)((NRF_CLOCK->HFCLKRUN &
modules/nrfx/hal/nrf_clock.h:    NRF_CLOCK->CTIV = ((interval << CLOCK_CTIV_CTIV_Pos) & CLOCK_CTIV_CTIV_Msk);
modules/nrfx/hal/nrf_clock.h:    *((volatile uint32_t *) ((uint8_t *) NRF_CLOCK + (uint32_t) task + 0x80uL)) =
modules/nrfx/hal/nrf_clock.h:    *((volatile uint32_t *) ((uint8_t *) NRF_CLOCK + (uint32_t) task + 0x80uL)) = 0;
modules/nrfx/hal/nrf_clock.h:    *((volatile uint32_t *) ((uint8_t *) NRF_CLOCK + (uint32_t) event + 0x80uL)) =
modules/nrfx/hal/nrf_clock.h:    *((volatile uint32_t *) ((uint8_t *) NRF_CLOCK + (uint32_t) event + 0x80uL)) = 0;
modules/nrfx/mdk/nrf51.h:#define NRF_CLOCK                   ((NRF_CLOCK_Type*)         NRF_CLOCK_BASE)
modules/nrfx/mdk/nrf52.h:#define NRF_CLOCK                   ((NRF_CLOCK_Type*)         NRF_CLOCK_BASE)
modules/nrfx/mdk/nrf52805.h:#define NRF_CLOCK                   ((NRF_CLOCK_Type*)         NRF_CLOCK_BASE)
modules/nrfx/mdk/nrf52810.h:#define NRF_CLOCK                   ((NRF_CLOCK_Type*)         NRF_CLOCK_BASE)
modules/nrfx/mdk/nrf52811.h:#define NRF_CLOCK                   ((NRF_CLOCK_Type*)         NRF_CLOCK_BASE)
modules/nrfx/mdk/nrf52820.h:#define NRF_CLOCK                   ((NRF_CLOCK_Type*)         NRF_CLOCK_BASE)
modules/nrfx/mdk/nrf52833.h:#define NRF_CLOCK                   ((NRF_CLOCK_Type*)         NRF_CLOCK_BASE)
modules/nrfx/mdk/nrf52840.h:#define NRF_CLOCK                   ((NRF_CLOCK_Type*)         NRF_CLOCK_BASE)
modules/nrfx/templates/nRF9160/nrfx_config.h:#define NRF_CLOCK      NRF_CLOCK_S

 

 

1-10> -f 옵션

: 파일로 부터 패턴을 얻어서 검색시 사용합니다.

 

test1.txt 파일을 패턴으로 test2.txt 파일에서 검색을 하면

좌측 : test1.txt 우측 : test2.txt
 
 
$ grep -f test1.txt test2.txt
package: com.android.essential
hello
package:com.sec.android.service.health

 

< 주의 사항 >

간헐적으로 문자열이 동일한데 검색이 안되는 경우가 있는데 그럴경우에는 hexa view로

확인을 해보시기 바랍니다. 간헐적으로 뒤에 붙는 개행문자가 다를수도 있습니다.

( ex : \a or \d\a )

 

1-11> -x 옵션

: 정확하게 한줄이 일치하는 라인 검색시 사용

SYNOPSIS : grep -x PATTERN [FILE...]

test1.txt

 

위 test1.txt 파일 기준 설명입니다.

 

$ grep -x "100" test1.txt
100
$ grep -x "10" test1.txt
t$ grep "10" test1.txt
100
101
102
103
104
105

$ grep -x "package: com.android.essential" test1.txt
package: com.android.essential

 

 

1-12> -ㅣ 옵션 (--files-with-matches)

: 정상적인 일치하는 패턴 출력이 아닌 파일이름 출력 옵션

첫 매칭후 동일파일 검색을 멈춥니다.

>> 불필요한 내용이 너무 많이 출력이 될때 사용옵션

 
$ grep -r   CC2640R2_LAUNCHXL
FlashROM_StackLibrary/TOOLS/subdir_rules.mk:    "C:/ti/xdctools_3_51_03_28_core/xs" --xdcpath="C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source;C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/kernel/tirtos/packages;C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack;" xdc.tools.configuro -o configPkg -t ti.targets.arm.elf.M3 -p ti.platforms.simplelink:CC2640R2R -r release -c "C:/ti/ccs910/ccs/tools/compiler/ti-cgt-arm_18.12.2.LTS" --compileOptions "-mv7M3 --code_state=16 -me -O4 --opt_for_speed=0 --include_path=\"C:/Users/leevisual/workspace_v9/hid_emu_kbd_cc2640r2lp_app\" --include_path=\"C:/Users/leevisual/workspace_v9/hid_emu_kbd_cc2640r2lp_app/Application\" --include_path=\"C:/Users/leevisual/workspace_v9/hid_emu_kbd_cc2640r2lp_app/Startup\" --include_path=\"C:/Users/leevisual/workspace_v9/hid_emu_kbd_cc2640r2lp_app/PROFILES\" --include_path=\"C:/Users/leevisual/workspace_v9/hid_emu_kbd_cc2640r2lp_app/Include\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/controller/cc26xx_r2/inc\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/inc\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/common/cc26xx\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/icall/inc\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/profiles/batt/cc26xx\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/profiles/dev_info\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/profiles/hid_dev/cc26xx\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/profiles/hid_dev_kbd\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/profiles/hid_dev_kbd/cc26xx\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/profiles/roles\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/profiles/roles/cc26xx\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/profiles/scan_param\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/profiles/scan_param/cc26xx\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/target\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/hal/src/inc\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/hal/src/target/_common\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/hal/src/target/_common/cc26xx\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/heapmgr\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/icall/src/inc\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/osal/src/inc\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/services/src/saddr\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/blestack/services/src/sdata\" --include_path=\"C:/ti/simplelink_cc2640r2_sdk_5_30_00_03/source/ti/devices/cc26x0r2\" --include_path=\"C:/ti/ccs910/ccs/tools/compiler/ti-cgt-arm_18.12.2.LTS/include\" --define=BOARD_DISPLAY_USE_LCD=0 --define=BOARD_DISPLAY_USE_UART_ANSI=0 --define=CC2640R2_LAUNCHXL --define=CC26XX --define=CC26XX_R2 --define=DeviceFamily_CC26X0R2 --define=Display_DISABLE_ALL --define=ICALL_EVENTS --define=ICALL_JT --define=ICALL_LITE --define=ICALL_MAX_NUM_ENTITIES=6 --define=ICALL_MAX_NUM_TASKS=4 --define=ICALL_STACK0_ADDR --define=POWER_SAVING --define=RF_SINGLEMODE --define=STACK_LIBRARY --define=USE_ICALL --define=xdc_runtime_Assert_DISABLE_ALL --define=xdc_runtime_Log_DISABLE_ALL -g --c99 --gcc --diag_warning=225 --diag_wrap=off --display_error_number --gen_func_subsections=on --abi=eabi " "$<"
TOOLS/src/sysbios/makefile:BIOS_DEFS =  -Dti_sysbios_BIOS_swiEnabled__D=TRUE -Dti_sysbios_BIOS_taskEnabled__D=TRUE -Dti_s
~~~~ 중략 ~~~~

 >> CC2640R2_LAUNCHXL 패턴이 들어가는 파일이름만 표시
$ grep -rl --exclude={*.d,}  CC2640R2_LAUNCHXL
FlashROM_StackLibrary/PROFILES/subdir_rules.mk
FlashROM_StackLibrary/Startup/board.obj
FlashROM_StackLibrary/Startup/main.obj
FlashROM_StackLibrary/Startup/subdir_rules.mk
FlashROM_StackLibrary/TOOLS/subdir_rules.mk
TOOLS/src/sysbios/makefile

 

 

1-13>  --exclude-dir 옵션

   : 제외하고자 하는 디렉토리 지정옵션 (서브디렉토리 안됨.)

$ grep -r logd --exclude-dir=FS
dumpstate.txt:!@Boot_EBS_F: processing action (fs) from (/system/etc/init/logd.rc:28)                      8350   3613    150 1950 2400 2080
grep: dumpstate.txt: binary file matches
dumpstate_log.txt:Adding dir /data/misc/logd (recursive: 0)

 grep -r logd
dumpstate.txt:!@Boot_EBS_F: processing action (fs) from (/system/etc/init/logd.rc:28)                      8350   3613    150 1950 2400 2080
grep: dumpstate.txt: binary file matches
grep: FS/data/tombstones/tombstone_14.pb: binary file matches
grep: FS/data/tombstones/tombstone_11.pb: binary file matches
FS/data/tombstones/tombstone_11:    0000006f'85ade000-0000006f'85b0dfff r--         0     30000  /dev/__properties__/u:object_r:logd_prop:s0
FS/data/tombstones/tombstone_14:      #01 pc 00004ad1  /system/lib/liblog.so (__android_log_logd_logger+44) (BuildId: 6acd67b8d424d657bdad02f3b8aeb521)
~~ 중략 ~~
FS/cache/recovery/last_log:[    0.658528][Hj-A][6oo1] ro.logd.auditd=false
FS/cache/recovery/last_kmsg.8:<12>[    4.182316]  [5:       recovery:  394] [    0.670743][bL-A][6oo1] ro.logd.size.stats=64K
FS/cache/recovery/last_kmsg.8:<12>[    4.182325]  [5:       recovery:  394] [    0.670751][bL-A][6oo1] ro.logd.auditd=false
dumpstate_log.txt:Adding dir /data/misc/logd (recursive: 0)

 

 

<기타 >

  A. git branch list 가 너무 많아 cherry 란 이름이 들어간 파일 찾기

$ git branch | grep cherry or $ git branch | grep "cherry*"

 

 

 

 

 

 

 

 

 

 

 

반응형