Linux

리눅스 파일 편집 관련 명령어(grep, sed)

Zin0_0 2021. 6. 4. 14:01
반응형

문자열 찾기

  • grep

    • 텍스트 검색 기능을 가진 명령어

    • grep [Options..] PATTERN [File...]

    • 옵션

      • 명령어 설명
        -E PATTERN을 확장 정규 표현식(Extended RegEx)으로 해석.
        -F PATTERN을 정규 표현식(RegEx)이 아닌 일반 문자열로 해석.
        -G PATTERN을 기본 정규 표현식(Basic RegEx)으로 해석.
        -P PATTERN을 Perl 정규 표현식(Perl RegEx)으로 해석.
        -e 매칭을 위한 PATTERN 전달.
        -f 파일에 기록된 내용을 PATTERN으로 사용.
        -i 대/소문자 무시.
        -v 매칭되는 PATTERN이 존재하지 않는 라인 선택.
        -w 단어(word) 단위로 매칭.
        -x 라인(line) 단위로 매칭.
        -z 라인을 newline(\n)이 아닌 NULL(\0)로 구분.
        -m 최대 검색 결과 갯수 제한.
        -b 패턴이 매치된 각 라인(-o 사용 시 문자열)의 바이트 옵셋 출력.
        -n 검색 결과 출력 라인 앞에 라인 번호 출력.
        -H 검색 결과 출력 라인 앞에 파일 이름 표시.
        -h 검색 결과 출력 시, 파일 이름 무시.
        -o 매치되는 문자열만 표시.
        -q 검색 결과 출력하지 않음.
        -a 바이너리 파일을 텍스트 파일처럼 처리.
        -I 바이너리 파일은 검사하지 않음.
        -d 디렉토리 처리 방식 지정. (read, recurse, skip)
        -D 장치 파일 처리 방식 지정. (read, skip)
        -r 하위 디렉토리 탐색.
        -R 심볼릭 링크를 따라가며 모든 하위 디렉토리 탐색.
        -L PATTERN이 존재하지 않는 파일 이름만 표시.
        -l 패턴이 존재하는 파일 이름만 표시.
        -c 파일 당 패턴이 일치하는 라인의 갯수 출력.

문자열 수정

  • sed

    • 텍스트 분해 & 변환 기능을 가진 명령어

    • sed Options.. [명령어] [FILE...]

    • sed -i 's/찾을 문자열/바꿀 문자열 /g' [File...]

    • 옵션

      • -n, --quiet, --silent
        • suppress automatic printing of pattern space
      • -e script, --expression=script
        • add the script to the commands to be executed
      • -f script-file, --file=script-file
        • add the contents of script-file to the commands to be executed
      • --follow-symlinks
        • follow symlinks when processing in place; hard links will still be broken.
      • -i[SUFFIX], --in-place[=SUFFIX]
        • edit files in place (makes backup if extension supplied). The default operation mode is to break symbolic and hard links. This can be changed with --follow-symlinks and --copy.
      • -c, --copy
        • use copy instead of rename when shuffling files in -i mode. While this will avoid breaking links (symbolic or hard), the resulting editing operation is not atomic. This is rarely the desired mode; --follow-symlinks is usually enough, and it is both faster and more secure.
      • -l N, --line-length=N
        • specify the desired line-wrap length for the `l' command
      • --posix
        • disable all GNU extensions.
      • -r, --regexp-extended
        • use extended regular expressions in the script.
      • -s, --separate
        • consider files as separate rather than as a single continuous long stream.
      • -u, --unbuffered
        • load minimal amounts of data from the input files and flush the output buffers more often
        • --help display this help and exit
        • --version output version information and exit
    • 연산자

      • 연산자 설명 예시
        p 출력
        모든 줄을 출력하고, 패턴과 일치하는 줄을 한번 더 출력
        기본 출력을 금지하고, 패턴과 일치하는 줄만 출력
        sed '/earth/p' myfile
        sed -n '/earth/p' myfile
        d 삭제
        3번째 줄을 삭제하고 나머지 모든 줄을 출력
        3번째 ~ 마지막까지 삭제, 나머지 모든 줄 출력
        마지막 줄 삭제, 나머지 모든 줄 출력
        패턴과 일치하는 줄을 삭제, 나머지 모든 줄 출력
        sed '3d' myfile
        sed '3, $d' myfile
        sed '$d' myfile
        sed '/earth/d' myfile
        s 치환
        g 플래그는 전역을 의미

        west를 north로 치환
        west로 시작하는 줄을 north로 치환하고 그 줄만 출력
        sed 's/west/north/g' myfile
        sed -n 's/^west/north/g' myfile
        sed -n 's/west/north/gp' myfile
        ... ... ...

Reference

https://recipes4dev.tistory.com/157

https://www.hahwul.com/2019/02/10/multi-file-replace-string-with-grep-sed/

https://www.computerhope.com/unix/used.htm

참고하면 좋을 글
반응형