" I do not want to manually change the configuration .-- programming three minutes ."
Outline
sed command is used to bulk edit text content, such as a batch replace ip configuration.
sed command when processing will first read a row after the row currently being processed is stored in a temporary buffer, processed contents of the buffer, printed to the screen. Then read the next row, the next execution cycle. Constant repetition until the end of the file.
grammar:
sed [参数] [文本或文件]
复制代码
Because without -i
parameters will not be written to the console output to a file, so the following examples of default plus-i
insert
- Insert a row in front of a row
$ sed -i "1a insert after" file.txt
$ cat file.txt
1
insert after
2
3
复制代码
Wherein 1a
represents (After) inserted after the first row
- Inserting a row line at the end
$ sed -i "1i insert before" file.txt
$ cat file.txt
insert before
1
2
3
复制代码
Wherein 1i
represents inserted before the first line
delete
$ sed -i '2,3d' file.txt
$ cat file.txt
1
复制代码
Delete Row can delete a row ( 3d
delete the third row), you can also write a scope ( 2,3d
delete rows 2-3, inclusive), $
the symbol represents the end
only drawback is that many times in a row to delete rows, not one-off match to delete rows, As a regular delete ( /^2/d
on behalf delete everything beginning with 2 lines)
Replace the line
$ sed -i '2c replace' file.txt
$ cat file.txt
1
replace
3
复制代码
2c replace
Represents replaces the second row for the replace
only drawback is to replace multiple lines, can not be replaced all at once matched to the line, you can replace it with a positive ( /^2/c replace
on behalf replace all behavior begins with 2 replace
)
Alternatively only matched string
For demonstration modify the contents of the file
$ cat -n config.txt
1 name=coding3min
2 age=0
3 [email protected]
4 name=coding3min
5 age=0
6 [email protected]
复制代码
Use Batch Replace command 3-4
between the rows coding3min
stringtom
$ sed -i '3,4s/coding3min/tom/g' config.txt
$ config.txt
name=coding3min
age=0
[email protected]
name=tom
age=0
[email protected]
复制代码
s/coding3min/top/g
Representative matching text line is not limited, to remove g
the representative replace only as a match to the firsts/coding3min/top
Find output
Line output contents 3-4
sed -n 3,4p config.txt
[email protected]
name=coding3min
复制代码
Find all lines with name beginning with
sed -n '/^name/p' config.txt
name=coding3min
name=coding3min
复制代码
As long as you can see with the -n
parameter p + pattern matching can find and output sj
Automatically creates a backup file
Of course, direct sed -i
likely to result in the replacement error, cry no way to cry! It is necessary to advance with -n+p
that is to say on a way to check the next result. But each check is obviously not practical. It can be used sed -i备份文件后缀的方式
, for example, sed -i.bak
orsed -i.backup
$ sed -i.bak 's/coding3min/kitty/g' config.txt
$ ls
config.txt config.txt.bak
$ cat config.txt
name=kitty
age=0
[email protected]
$ cat config.txt.bak
name=coding3min
age=0
[email protected]
复制代码
Used in conjunction with grep
And grep
use it in conjunction with succulent points can check and batch replace, improve efficiency and fault tolerance in advance, will not hurry Get up
sed -i 's/coding/kitty/g' `grep -rl coding *`
$cat config.txt
name=conding3min
age=0
[email protected]
$cat test/config.txt
name=conding3min
age=0
[email protected]
复制代码
do you understand? On one of said grep -rl
recursive find the matching file and output file name, plus before and after the `backtick, is the number 1 top left corner of the keyboard to the left of that symbol, code execution in advance.
Then use the file to replace the contents.
Other tips
Sed to convert the file using DOS format to Unix format sed 's/.$//' filename
Match all lines that contain mailboxes, ( -n
option lets sed is only output after processing of those lines)
sed -n '/[A-Za-z0-9]\+\@[a-zA-Z0-9_-]\+\(\.[a-zA-Z0-9_-]\+\)/p' config.txt
[email protected]
[email protected]
复制代码
Remove all html tags
$ cat html.txt
<b>hi!</b><span>I'm</span>
$ sed 's/<[^>]*>//g' html.txt
hi!I'm father复制代码
Recommended Reading
(Click on the title to jump to read)
Three Musketeers youngest grep linux
How old my server so slow, it is said to be a mining? linux boot entry self-examination
I secretly dug a tunnel network, the company came close to being activated
If it helps do not forget to share with friends Oh ~