sed text processing tool

catalogue 1, sed editor summary Workflow read implement d...

catalogue

1, sed editor

summary

Workflow

read

implement

display

Command format

Common options

Common operation

Example

print contents

Use address

Delete row

Replace tag

insert

1, sed editor summary
  • sed is a flow editor, which edits the data flow based on a set of rules provided in advance before the editor processes the data
  • The sed editor can process the data in the data stream according to commands, which are either input from the command line or stored in a command text file
  • The working process of the flow editor can be understood as: it is executed from top to bottom, just like running water, line by line
Workflow read
  • sed reads a line from the input stream (file, pipe, standard input) and stores it in a temporary buffer (also known as pattern space)
implement
  • By default, all sed commands are executed sequentially in the pattern space
  • Unless the address of the line is specified, the sed command will be executed sequentially on all lines
display
  • Send the modified content to the output stream
  • After sending data, the mode space will be cleared
  • Before all file contents are processed, the above process will be repeated until all contents are processed
Command format
#Example: sed -e 'operation' File 1 file 2 ... sed -n -e 'operation' File 1 file 2 ... sed -f Script file 1 file 2 ... sed -i -e 'operation' File 1 file 2 ... sed -e 'n{ Operation 1 Operation 2 ... }' File 1 file 2 ...
Common options optionexplain-e or – expressionIndicates that the input text file is processed with the specified command. It can be omitted when there is only one operation command. It is generally used when executing multiple operation commands-f or – fileIndicates that the input text file is processed with the specified script file-h or – helpShow help-n. – quiet or silentThe sed editor output is disabled, but can be used with the p command to complete the output-iDirectly modify the target text file Common operation optionexplainsReplace: replaces the specified characterdDelete: deletes the selected rowaAdd: add a line of specified content under the current lineiInsert: inserts a row of specified content on the selected rowcReplace: replaces the selected row with the specified contentyCharacter conversion; Note that the character length before and after conversion must be the samepPrinting; If a row is specified at the same time, it means that the specified row is printed; If no line is specified, it means that all contents are printed; If there are non printing characters, they are output in ASCII code; And it is usually used in conjunction with the "- n" option=Print line numberlPrint text and non printable ASCII characters in the data stream (such as terminator $, tab \ t) Example print contents
sed -ne 'p' zxc1

sed -n '=' zxc1

sed -n 'l' zxc1
sed -n '=;p' zxc1
[root@localhost shell]# sed -n ' > p > = > ' zxc1

Use address
  • sed editor has two addressing modes:
    • The row interval is expressed numerically
    • Use text mode to filter travel
sed -n 'p;n' zxc1 sed -n 'n;p' zxc1 sed -n '2,$' zxc1 sed -n '/user/p' /etc/passwd sed -n '/^a/p' /etc/passwd sed -n '/bash$/p' /etc/passwd sed -n '/ftp\|root/p' /etc/passwd sed -n '2,/nobody/p' /etc/passwd sed -n '2,/nobody/=' /etc/passwd sed -n '2,/nobody/=;2,/nobody/p' /etc/passwd
Delete row
sed 'd' zxc1 sed '3d' zxc1 sed '2,4d' zxc1 sed -n 'p' zxc2 sed '/2/,/3/d' zxc2
Replace tag
##Format: Row range s/Old string/New string/Replace tag
  • Number: indicates where the new string will replace the first match
  • g: Indicates that the new string will replace all matches
  • p: Print lines that match the replace command, used with - n
  • w file: write the replacement results to a file
  • Example:
sed -n 's/root/admin/p' /etc/passwd sed -n 's/root/admin/2p' /etc/passwd sed -n 's/root/admin/gp' /etc/passwd sed -n 's/root//gp' /etc/passwd sed '1,20 s/^/#/' /etc/passwd sed '/^root/ s/$/#/' /etc/passwd cat zxc2 sed -f zxc3 zxc2 sed '1,20w, out.txt' /etc/passwd > dev/null sed '1,20w out.txt' /etc/passwd > /dev/null cat out.txt
insert
vim 123.txt sed '1,3a ABC' 123.txt sed '1i ABC' 123.txt sed '5r /etc/resolv.conf' 123.txt sed '/root/;$G' /etc/passwd ##Cut the row containing root to the end, H means copy to the clipboard, and G means paste to the specified row

14 September 2021, 17:33 | Views: 3763

Add new comment

For adding a comment, please log in
or create account

0 comments