(3) sed of the Three Musketeers

(1) Basic introduction

1) Workflow:
sed processes a line of content at a time. When processing, the currently processed line is stored in a temporary buffer area, called the pattern space, and then the sed command is used to process the content in the buffer. The content is sent to the screen until the content is processed
2) Syntax format:

sed [options] 'command' file
options:
    -n: suppress default output
    -i: inplace, edit in place
    -r: support extended metacharacters
Commands:
    a: add one or more lines after the current line
    c: use new text modify (replace) text on the current line
    d: delete line
    i: insert text before the current line
    s: replace one string with another
        s: replace flag
        g: make global replacement within line
        i: ignore case
    r: read from file
    w : write lines to file
   
Support basic regular expressions: ^ $ . * [] m\{n\} [^] \< \> ()
Support extended regular expressions: ? + {} | ()
Use extended regular expressions Expression in two ways: \+ sed -r

(2) Basic usage of sed

1) print p

sed -r "" /etc/passwd
sed -r "p" /etc/passwd print two lines
sed -rn "p" /etc/passwd print two lines, use -n to cancel the default output
sed -rn "/root/p " /etc/passwd

2) delete row d

sed -r "/root/d" /etc/passwd
sed -r "\#root#d" /etc/passwd
sed -r "1,3d" passwd #delete 1 to 3 lines
sed -r '3,$d ' passwd #delete from the third line to the last line
sed -r "/root/,5d" passwd #match the root line and delete it to the fifth line
sed -r "/^root/,+5d" passwd #match the line starting with root , delete 5 lines below

3) Replace the character s

sed -r "s/root/jack" /etc/passwd replaces the first one on a line, and the following does not replace
sed -r "s/root/jack/g" /etc/passwd single-line matching replaces all
sed -r "s/ ROOT/jack/gi" /etc/passwd ignore case
sed -r 's/(.*)/#&/g' passwd add # in front of all lines, & represents the matching content in the search string
sed -r 's/(.)(.)(.*)/a\1b\2c\3/g' passwd \1 \2 \3 refer to the previous ()

4) Precautions

# cat a.txt
/etc/abc/456
etc
sed -r "\#/etc/abc/456#d" a.txt #Delete the line /etc/abc/456, use # to look better, it needs to be in \#
sed -r "s#/etc/abc/456#/etc/sda1#" a.txt 

5) Write file: w

sed -r '/root/w /tmp/1.log' passwd Write the line matching root from the passwd file to the /tmp/1.log file
sed -r '3,$w /tmp/1.log ' passwd

6) Additional content: a

sed -r '2a\111111' /etc/hosts append the second line

7) Insert content: i

sed -r '2i\11111' /etc/hosts insert content on the second line

8) Modify the entire line: c

sed -r '2c\11111' /etc/hosts Change the second line to 11111

9) Reverse: d

sed -r '3d' /etc/passwd
sed -r '3!d' /etc/passwd delete all but the third line

10) Reference external variables

var=1111
sed -r "3a$var" /etc/passwd Append the value of the variable on the third line
sed -r "\$a$var" /etc/passwd Append the value of the variable on the last line

(3) Common examples
delete comment lines and blank lines:
sed -ri '/^[ \t]*(#|$)/d' 1.log
sed -ri '/^[ \t]*#|^[ \ t]*$/d' 1.log
sed -ri '/^[ \t]*$/d;/^[ \t]*#/d' 1.log

modify the configuration file:
sed -ri '$a\ 127.0.0.1 localhost' /etc/hosts \ last line append
sed -ri '/^SELINUX/cSELINUX=disabled' /etc/sysconfig/selinux
sed -ri '/UseDNS/cUseDNS=no' /etc/ssh/sshd_config
sed -ri '/GSSAPIAuthentication/cGSSAPIAuthentication no' /etc/ssh/sshd_config


Add comment lines to the file:
sed -ri 's/^/#/' passwd
sed -ri 's/^(.*)/#\1/' passwd
sed -r 's/^.*/#&/' passwd & means match what was looked up before

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324900176&siteId=291194637