Sed is a tool for filtering and converting text content. Generally used to batch replace, delete a line of files
- Sed command usage and example · github
- Series: What did I do when I had a server · Github
Sed command description
Each sed command can be basically done with options, matching, and corresponding actions
#Print lines 3 through 5 of the document
#-n: indicates printing
#3-5: matches, representing the third to fifth rows
#P: operation, which indicates printing
$ sed -n '3, 5 p' file
#Delete the second line of the file
#-i: indicates the option to directly replace the file
#2: matches, representing the second row
#D: operation, which indicates deletion
$ sed -i '2d' file
Copy the code
options
-n: prints matched content. -I: directly replaces text content. -f: specifies a sed script file that contains a series of sed commands
matching
/reg/
: matching re3
: The number represents the line$
: Last line1, 3
: Lines 1 through 31, + 3
: first line, and three more lines down (print lines 1 through 4)1, /reg/
First line, and to the matching string line
operation
a
: append, insert content in the next linei
: insert, insert content in the previous rowp
: print, usually used to print certain lines of a document, usually with-n
Together withs
: replace, replace, same as vim
sed examples
See the manual
$ man sed
Copy the code
Print a specific line
P refers to the print
#1p means to print the first line
$ ps -ef | sed -n 1p
UID PID PPID C STIME TTY TIME CMD
#2,5p means to print lines 2-5
$Ps - ef | sed - n 2, 5 p
root 1 0 0 Sep29 ? 00:03:42 /usr/lib/systemd/systemd --system --deserialize 15
root 2 0 0 Sep29 ? 00:00:00 [kthreadd]
root 3 2 0 Sep29 ? 00:00:51 [ksoftirqd/0]
root 5 2 0 Sep29 ? 00:00:00 [kworker/0:0H]
Copy the code
Print the last line
$refers to the last line
Note that single quotation marks are required
$ ps -ef | sed -n '$p'
Copy the code
Delete a specific row
D refers to delete
$ cat hello.txt
hello, one
hello, two
hello, three
#Delete the third line
$ sed '3d' hello.txt
hello, one
hello, two
Copy the code
Filter string
This is similar to grep, except that grep can highlight keywords
$ ps -ef | sed -n /ssh/p
root 1188 1 0 Sep29 ? 00:00:00 /usr/sbin/sshd -D
root 9291 1188 0 20:00 ? 00:00:00 sshd: root@pts/0
root 9687 1188 0 20:02 ? 00:00:00 sshd: root@pts/2
root 11502 9689 0 20:08 pts/2 00:00:00 sed -n /ssh/p
root 14766 1 0 Sep30 ? 00:00:00 ssh-agent -s
$ ps -ef | grep ssh
root 1188 1 0 Sep29 ? 00:00:00 /usr/sbin/sshd -D
root 9291 1188 0 20:00 ? 00:00:00 sshd: root@pts/0
root 9687 1188 0 20:02 ? 00:00:00 sshd: root@pts/2
root 12200 9689 0 20:10 pts/2 00:00:00 grep --color=auto ssh
root 14766 1 0 Sep30 ? 00:00:00 ssh-agent -s
Copy the code
Deletes the line that matches the string
$ cat hello.txt
hello, one
hello, two
hello, three
$ sed /one/d hello.txt
hello, two
hello, three
Copy the code
Alternative content
S stands for replacement, similar to VIm
$ echo hello | sed s/hello/world/
world
Copy the code
Add content
A and I represent adding content to a new line, similar to vim
#I specifies the previous row
#A specifies the next row
# -eSpecify the script
$ echo hello | sed -e '/hello/i hello insert' -e '/hello/a hello append'
hello insert
hello
hello append
Copy the code
Replace file contents
$ cat hello.txt
hello, world
hello, world
hello, world
#Replace Hello with world
$ sed -i s/hello/world/g hello.txt
$ cat hello.txt
world, world
world, world
world, world
Copy the code
Matters needing attention
If you want to use sed on the MAC, use gsed instead, otherwise you won’t be able to fully extend the re or some of the formats.
Use Brew install GNU -sed to install
$ echo "hello" | sed "s/\bhello\b/world/g"
hello
$ brew install gnu-sed
$ echo "hello" | gsed "s/\bhello\b/world/g"
world
Copy the code
reference
- Linux Sed Command