shell script command -sed

sed command - stream editor

1. Works

  Specifies a text file, text file sequentially read the contents of each line, is read to the pattern space (pattern space), the matching text (use regular expressions) in the pattern space, matching the content of the modified (replaced , delete, print, edit and save)

2. Common options

  -r specify extended regular expressions
    -n to disable the automatic print mode space
    -e script statements access
    -f specify the script file
    -l length specified text wrap
    -i directly modify the contents of the file (requires careful use) 

3. Address delimitation

(1) Direct matching value
     Example: sed -n '5p' in the fifth row in the file name of the print file content #
(2) d ~ step       
     Example: sed -n '1 ~ 3p' # file name of the print file from the content line 1 starts at 3 increments printing, is to print the first line 1,4,7,11 .....

  Extended: sed -n; # print first line of the file name and file contents in the third line '1p 3p'

(3) $ specified end of line
     Example: sed -n '10, $ p 'filename # print file content from the line 10 to the last line
(4) / regular /
    / regular 1 /, / n is 2 /
    Example: sed - n '/ k \ {5, \} /, $ p' file name of the print file # k matches the character of at least 5, and outputs character

(. 5) 0, addr
   addr may be a digital / $ / regex
   example: sed -n '0, / k     \ {5, \} / p' abc

(6) addr, + N    
    arranged to addr line after line N addr
    Example: Sed -n '/ FD /, + 5P' ABC
4. Common Commands

 p print print (the default display on the screen) recommendations and -n together
    i the match on the line to add the specified content
        example: sed "/ aaa / i \ abc" file
   a specified content added in the next line of matching lines
       example : sed "/ aaa / a \ abc" file    
   c replace the line
      example: sed "/ aaaa / c \ A" file
   d delete
      example: sed "/ aaa / d" file
                 sed "1,3d" file
  n read / N pattern matching was added to the contents of the line of the next row, in its operation
  w save the contents of the file to a new file
     example: sed "/ pattren / w nEW" original file
               sed "/ k / w / tmp / pattern" abc
  Alternatively s text, replacing the default matching pattern space to the first
     syntax: s / pattern / string /
     middle / can be replaced by any one of special characters, for example: s s %%% ###
     --- the line g Alternatively global
     \ 1 \ 2 the same packet and regular expressions, implement the reverse application; designate a replacement to modify a match
     & calls --- front (pattern) matching contents
     example: sed "s / r ..t / & er / g "file

5. Practice

   (1) delete all the files in the beginning /boot/grub/grub.conf blank blank character of the first trekking;
        sed 'S / ^ [[: Space:]] \ + // G' / the Boot / GRUB / GRUB. conf 

   (2) delete the / etc / fstab file in the first line of all begin with #, followed by at least one blank character line # and whitespace characters;
        sed 'S / ^ # [[: Space:]] \ + // G '/ etc / fstab

   (3) Given a directory, which directory name remove
        echo "/ etc / passwd" | sed 's / [^ /] \ + $ //'

   (4) Given a directory, remove the file name
        echo "/ etc / passwd" | . (. * \) Sed 's # ^ / * / \ ##

Guess you like

Origin www.cnblogs.com/hmm01031007/p/11390805.html