Sed usage

and

  Common options

    -n: do not output the contents of the pattern space to the screen

    -e: multi-point editing (can be multiple commands)

    -f: editing commands, one per line

    -r: support for extended regular expressions

    -i: directly edit the original file

 

  Address delimitation

    1, an empty address: the full text processing

    2, a single address:

      #: The specified row

      / Pattern /: this mode are matched to each row

    3, address range

      #,#:

      #,+#:

      # / Pat1 /

      / Pat1 /, / pat2 /

    4, step: -

      1-2: all the odd rows

      2 ~ 2: all even lines

 

  Editing commands:

    d: Delete

    p: displays the contents of the pattern space

    a \ text: the line appends text to support the use of \ n additional multi-line

    i \ text: the text is inserted before the row, supports \ n multi-row insert

    c \ text: replace specified text here is to match lines

    w / PATH / TO / SOMEFILE: Space-saving mode to the matching line to the specified file

    r / PATH / FROM / SOMEFILE: reading the contents of the specified file is matched to the rear row of the pattern to the current file; document merger

    =: For the pattern matching to the line number of the line printing (sed '/ ^ UUID / =' / etc / fstab)

    !: Condition inverted (sed '/ ^ # / d!' / Etc / fstab)

    s ///: Find Alternatively, the separator may specify their own

      Replacement tags:

        g: global replace

        w / PATH / TO / SOMEFILE: replaces the successful results saved to the specified file

        p: replace successful line display

 

Exercise 1: All whitespace characters /boot/grub2/grub.cfg delete all files that start with a blank character of line

  sed 's/^[[:space:]]\+//g' /boot/grub2/grub.cfg

Exercise 2: All whitespace characters of all lines beginning with the # # # back and delete the / etc / fstab file

  sed 's/^#[[:space:]]*//g' /etc/fstab

Exercise 3: Output absolute path to a sed command, remove its catalog, its behavior is similar to dirname

  echo "/var/log/messages" | sed 's/[^/]\+$//'

Guess you like

Origin www.cnblogs.com/P-Z-W/p/11075022.html