sed workflow
Sed is an online, non-interactive editor that processes one line of content at a time. When processing, the currently processed rows are stored in a temporary buffer, called pattern space, and then the contents of the buffer are processed with sed command. After processing, the contents of the buffer are sent to the screen. Then proceed to the next line and repeat until the end of the file. The content of the file remains unchanged unless you use redirection to store the output.
Sed is mainly used to edit one or more files automatically; simplify the repeated operation of files; write conversion programs, etc.
II. Command Format
sed [options] 'command' file(s) sed [options] -f scriptfile file(s)
Note:
sed is different from grep in that its exit status is 0 regardless of whether the specified mode is found or not.
The exit status of sed is non-zero only if there is a syntax error in the command
3. Regular expression support
Like grep, sed can also use regular expressions (REs) and various metacharacters to find patterns in files. Regular expressions are patterns enclosed between slashes for finding and replacing. Here are metacharacters supported by sed.
Use the basic meta-character set ^, $,...,*, [], [^], \ <\>, \(\), \\\\\\\
Using Extended Meta Character Set?, +, {}, |, ()
Using extended metacharacters:
\+
sed -r
IV. Basic usage of sed
Common command options
- e. Allowing multiple editors - f. Specify sed script file name - n. Cancel the default output and display only the processed results - i. inplace, in-place editing - r. Support for extended metacharacters - h. Display help
Common operations
A: Increase by adding a specified line below the current line c: Replace, replacing the selected row with the specified content d: Delete, delete selected rows i: Insert, insert a specified line above the selected line, ignore case p: Print, if you specify rows at the same time, that means print specified rows, if you do not specify rows, that means print all content; if there are non-print characters, then output with ASCII code. s: Replace, replace the specified character y: Character conversion G: Remove the contents of the temporary buffer, copy it to the schema space, and append it to the original content g: Remove the contents of the temporary buffer, copy it to the schema space, and overwrite the original content there. x: Exchange the contents of temporary buffer and mode space r: read from file w: Write rows to files
5. Examples of sed commands
Delete command:d
# sed -r '3d' datafile # sed -r '3{d;}' datafile # sed -r '3{d}' datafile # sed -r '3,$d' datafile # sed -r '$d' datafile # sed -r '/north/d' datafile # sed -r '/sout/d' datafile
Replace commands:s
# sed -r 's/west/north/g' datafile # sed -r 's/^west/north/' datafile #sed-r's/[0-9] [0-9]$/& 5/'datafile/& represents the matched content in the lookup string # sed -r 's/Hemenway/Jones/g' datafile # sed -r 's/(Mar)got/\1ianne/g' datafile # sed -r 's#3#88#g' datafile
Read command:r
# sed -r '/Suan/r /etc/newfile' datafile # sed -r '2r /etc/hosts' a.txt # sed -r '/2/r /etc/hosts' a.txt
Write file command: w
# sed -r '/north/w newfile' datafile # sed -r '3,$w /new1.txt' datafile
Additional orders:a
# sed -r '2a\1111111111111' /etc/hosts # sed -r '2a\1111111111111\ > 222222222222\ > 333333333333' /etc/hosts
Insert command:i
# sed -r '2i\1111111111111' /etc/hosts # sed -r '2i111111111\ > 2222222222\ > 3333333333' /etc/hosts
Modification command:c
# sed -r '2c\1111111111111' /etc/hosts # sed -r '2c\111111111111\ > 22222222222\ > 33333333333' /etc/hosts
Get the next line of command:n
# sed -r '/eastern/{ n; d }' datafile # sed -r '/eastern/{ n; s/AM/Archile/ }' datafile
Temporary joint area command: h H g G
# sed -r '1h;$G' /etc/hosts # sed -r '1{h;d};$G' /etc/hosts # sed -r '1h; 2,$g' /etc/hosts # sed -r '1h; 2,3H; $G' /etc/hosts
Scratch and mode space swap command: x
# sed -r '4h; 5x; 6G' /etc/hosts
Reverse selection:!
# sed -r '3d' /etc/hosts # sed -r '3!d' /etc/hosts
Multiple Edit Options:e
# sed -r -e '1,3d' -e 's/Hemenway/Jones/' datafile # sed -r '1,3d; s/Hemenway/Jones/' datafile # sed -r '2s/WE/UPLOOKING/g; 2s/Gray/YYY/g' datafile # sed -r '2{s/WE/UPLOOKING/g; s/Gray/YYY/g}' datafile
Sixth, sed common operation:
Delete the # comment line and blank line in the configuration file:
# sed -ri '/^[ \t]*#/d; /^[ \t]*$/d' /etc/vsftpd/vsftpd.conf # sed -ri '/^[ \t]*#|^[ \t]*$/d' /etc/vsftpd/vsftpd.conf # sed -ri '/^[ \t]*($|#)/d' /etc/vsftpd/vsftpd.conf
Modify the document:
# sed -ri '$a\chroot_local_user=YES' /etc/vsftpd/vsftpd.conf # sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config # sed -ri '/UseDNS/cUseDNS no' /etc/ssh/sshd_config # sed -ri '/GSSAPIAuthentication/cGSSAPIAuthentication no' /etc/ssh/sshd_config
Add comments to the file line:
# sed -r '2,6s/^/#/' a.txt # sed -r '2,6s/(.*)/#\1/' a.txt # sed -r '2,6s/.*/#&/'a.txt & matches what you looked for earlier