logo

예제가 포함된 Linux/Unix의 Sed 명령

UNIX의 SED 명령은 스트림 편집기를 나타내며 검색, 찾기 및 바꾸기, 삽입 또는 삭제와 같은 파일에 대한 많은 기능을 수행할 수 있습니다. UNIX에서 SED 명령의 가장 일반적인 사용은 대체 또는 찾기 및 바꾸기입니다. SED를 사용하면 파일을 열지 않고도 편집할 수 있습니다. 이는 먼저 VI 편집기에서 해당 파일을 열고 변경하는 것보다 파일에서 항목을 찾아 바꾸는 것이 훨씬 더 빠른 방법입니다.

  • SED는 강력한 텍스트 스트림 편집기입니다. 삽입, 삭제, 검색, 바꾸기(대체)가 가능합니다.
  • Unix의 SED 명령은 복잡한 패턴 일치를 수행할 수 있는 정규식을 지원합니다.

통사론:



자바 목록 정렬
 sed OPTIONS... [SCRIPT] [INPUTFILE...]>

예:
아래 텍스트 파일을 입력으로 고려하십시오.

 $cat>긱파일.txt>
 unix is great os. unix is opensource. unix is free os. learn operating system. unix linux which one you choose. unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.>

샘플 명령

    문자열 바꾸기 또는 대체 : Sed 명령은 주로 파일의 텍스트를 바꾸는 데 사용됩니다. 아래의 간단한 sed 명령은 파일에서 unix라는 단어를 linux로 바꿉니다.
     $sed 's/unix/linux/' geekfile.txt>

    출력 :



     linux is great os. unix is opensource. unix is free os. learn operating system. linux linux which one you choose. linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.>

    여기서 s는 대체 작업을 지정합니다. /는 구분 기호입니다. 유닉스는 검색 패턴이고 리눅스는 대체 문자열입니다.

    기본적으로 sed 명령은 각 줄에서 패턴의 첫 번째 발생을 대체하지만 두 번째, 세 번째 발생은 대체하지 않습니다. 한 줄에서 n번째 발생하는 패턴 바꾸기: /1, /2 등 플래그를 사용하여 한 줄에서 첫 번째, 두 번째 발생하는 패턴을 바꿉니다. 아래 명령은 한 줄에서 두 번째로 나오는 unix라는 단어를 linux로 바꿉니다.

     $sed 's/unix/linux/2' geekfile.txt>

    산출:



     unix is great os. linux is opensource. unix is free os. learn operating system. unix linux which one you choose. unix is easy to learn.linux is a multiuser os.Learn unix .unix is a powerful.>
    한 줄에 있는 모든 패턴 발생 바꾸기: 대체 플래그 /g(전역 대체)는 줄에 있는 모든 문자열 발생을 바꾸도록 sed 명령을 지정합니다.
     $sed 's/unix/linux/g' geekfile.txt>

    출력 :

     linux is great os. linux is opensource. linux is free os. learn operating system. linux linux which one you choose. linux is easy to learn.linux is a multiuser os.Learn linux .linux is a powerful.>
    한 줄에서 n번째 발생부터 모든 발생으로 바꾸기: /1, /2 등 및 /g의 조합을 사용하여 한 줄에서 n 번째 발생 패턴부터 모든 패턴을 바꿉니다. 다음 sed 명령은 한 줄에서 세 번째, 네 번째, 다섯 번째… unix 단어를 linux 단어로 바꿉니다.
     $sed 's/unix/linux/3g' geekfile.txt>

    산출:

     unix is great os. unix is opensource. linux is free os. learn operating system. unix linux which one you choose. unix is easy to learn.unix is a multiuser os.Learn linux .linux is a powerful.>
    각 단어의 첫 번째 문자를 괄호로 묶습니다. 이 sed 예제는 괄호 안에 있는 모든 단어의 첫 번째 문자를 인쇄합니다.
     $ echo 'Welcome To The Geek Stuff' | sed 's/([A-Z])/(1)/g'>

    산출:

    자바를 인쇄하는 방법
     (W)elcome (T)o (T)he (G)eek (S)tuff>
    특정 줄 번호의 문자열 바꾸기 : 특정 줄 번호의 문자열을 바꾸도록 sed 명령을 제한할 수 있습니다. 예는 다음과 같습니다
     $sed '3 s/unix/linux/' geekfile.txt>

    산출:

     unix is great os. unix is opensource. unix is free os. learn operating system. linux linux which one you choose. unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.>

    위의 sed 명령은 세 번째 줄의 문자열만 바꿉니다. /p 플래그를 사용하여 대체된 행 복제: /p 인쇄 플래그는 대체된 행을 터미널에 두 번 인쇄합니다. 행에 검색 패턴이 없고 대체되지 않은 경우 /p는 해당 행을 한 번만 인쇄합니다.

     $sed 's/unix/linux/p' geekfile.txt>

    산출:

     linux is great os. unix is opensource. unix is free os. linux is great os. unix is opensource. unix is free os. learn operating system. linux linux which one you choose. linux linux which one you choose. linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful. linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.>
    대체된 행만 인쇄: 대체된 행만 표시하려면 /p 인쇄 플래그와 함께 -n 옵션을 사용하십시오. 여기서 -n 옵션은 /p 플래그에 의해 생성된 중복 행을 억제하고 대체된 행을 한 번만 인쇄합니다.
     $sed -n 's/unix/linux/p' geekfile.txt>

    산출:

     linux is great os. unix is opensource. unix is free os. linux linux which one you choose. linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.>

    /p 없이 -n만 사용하면 sed는 아무 것도 인쇄하지 않습니다. 줄 범위에서 문자열 바꾸기 : 문자열을 바꾸기 위해 sed 명령에 줄 번호 범위를 지정할 수 있습니다.

     $sed '1,3 s/unix/linux/' geekfile.txt>

    산출:

    Java에 설정된 경로
     linux is great os. unix is opensource. unix is free os. learn operating system. linux linux which one you choose. unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.>

    여기서 sed 명령은 행을 1에서 3 사이의 범위로 바꿉니다. 또 다른 예는 다음과 같습니다.

     $sed '2,$ s/unix/linux/' geekfile.txt>

    산출:

     unix is great os. unix is opensource. unix is free os. learn operating system. linux linux which one you choose. linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful>

    여기서 $는 파일의 마지막 줄을 나타냅니다. 따라서 sed 명령은 파일의 두 번째 줄부터 마지막 ​​줄까지의 텍스트를 바꿉니다. 특정 파일에서 줄 삭제 : SED 명령은 특정 파일에서 줄을 삭제하는 데에도 사용할 수 있습니다. SED 명령은 파일을 열지 않고도 삭제 작업을 수행하는 데 사용됩니다.
    예:
    1. 특정 줄을 삭제하려면 이 예에서 n이라고 말하세요.

     Syntax: $ sed 'nd' filename.txt Example: $ sed '5d' filename.txt>

    2. 마지막 줄을 삭제하려면

     Syntax: $ sed '$d' filename.txt>

    3. x에서 y 범위의 줄을 삭제하려면

     Syntax: $ sed 'x,yd' filename.txt Example: $ sed '3,6d' filename.txt>

    4. n번째부터 마지막 ​​줄까지 삭제하려면

     Syntax: $ sed 'nth,$d' filename.txt Example: $ sed '12,$d' filename.txt>

    5. 패턴 일치 라인을 삭제하려면

     Syntax: $ sed '/pattern/d' filename.txt Example: $ sed '/abc/d' filename.txt>


Linux의 SED 명령 | 세트 2