Text processor sed


Sed is a line processor. Whenever a line is processed, the currently processed line is stored in a temporary buffer, called Pattern Space. Then, the contents of the buffer are processed with the SED command. After the processing is completed, the contents of the buffer are sent to the screen. Therefore, the execution speed is also very fast.

format

sed [option]... 'script;script;...' [inputfile...]

Common options

- n do not output mode space content to the screen, i.e. do not print automatically     - R, - e uses extended regular expressions
    - i directly modify the read file content instead of output from the screen
- i.bak back up the file and edit it in place
- f Fife reads the edit script from the specified file
- e multipoint editing
- s treats multiple files as separate files

script format:

Address command

Address format:

  1. No address:                           Process the full text
  2. Step:#~#                             Start with # and skip # one; For example, 1 ~ 2 are odd rows
  3. Single address:
    1. $                               Last line;
    2. /pattern/                     Every line that can be matched by the pattern here
  4. Address range:
    1. #,#                         From # line to # line;
    2. #,+#                       From # to + # lines, for example, 3, + 4 indicates 3 to 7 lines
    3. /patten1/,/pattern2/  
    4. /patterm/,#
    5. #,/pattern/

command

  • p                  Print the current mode space content and append it to the default output
  • Ip                 Ignore case output
  • d                 Delete rows that match the pattern space and immediately enable the next cycle  
  • a[\]text         Append text after the specified line. Multi line append is supported using \ n
  • i[\]text          Insert text before line
  • c[\]text         Replace line one or more lines of text
  • =                 Print line numbers for lines in pattern space
  • !                 Inverse processing of matching rows in pattern space
  • q                 End or exit sed
  • w   file         Save pattern matching lines to the specified file
  • r     file         After reading the text of the specified file to the matching line in the pattern space

Find replace

  • s/pattern/string/     Modifier search and replace. Other separators are supported. They can be in other forms: s @ @ @, s### replace modifier:
  • g                             Intra row global replacement
  • p                             Displays the rows that have been replaced successfully
  • w    / PATH/FILE       Save the successfully replaced line to a file
  • I,i                             ignore case

 

c

 1 [16:29:36 root@cen2 ~]#sed '/root/c\centos' /etc/passwd |head -10
 2 centos
 3 bin:x:1:1:bin:/bin:/sbin/nologin
 4 daemon:x:2:2:daemon:/sbin:/sbin/nologin
 5 adm:x:3:4:adm:/var/adm:/sbin/nologin
 6 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
 7 sync:x:5:0:sync:/sbin:/bin/sync
 8 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
 9 halt:x:7:0:halt:/sbin:/sbin/halt
10 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
11 centos

i

 1 [16:30:08 root@cen2[ ~]#sed '/root/i\centos' /etc/passwd |head -10
 2 centos
 3 root:x:0:0:boss,quee,1234991,12211:/root:/bin/bash
 4 bin:x:1:1:bin:/bin:/sbin/nologin
 5 daemon:x:2:2:daemon:/sbin:/sbin/nologin
 6 adm:x:3:4:adm:/var/adm:/sbin/nologin
 7 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
 8 sync:x:5:0:sync:/sbin:/bin/sync
 9 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
10 halt:x:7:0:halt:/sbin:/sbin/halt
11 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

a

 1 [16:41:25 root@cen2[ ~]#sed '/root/a\centos' /etc/passwd |head -10
 2 root:x:0:0:boss,quee,1234991,12211:/root:/bin/bash
 3 centos
 4 bin:x:1:1:bin:/bin:/sbin/nologin
 5 daemon:x:2:2:daemon:/sbin:/sbin/nologin
 6 adm:x:3:4:adm:/var/adm:/sbin/nologin
 7 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
 8 sync:x:5:0:sync:/sbin:/bin/sync
 9 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
10 halt:x:7:0:halt:/sbin:/sbin/halt
11 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

 

Posted by thoand on Sat, 06 Nov 2021 18:56:29 -0700