Linux sed Three Musketeers second child

" 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 -iparameters will not be written to the console output to a file, so the following examples of default plus-i

insert

  1. Insert a row in front of a row
$ sed -i "1a insert after" file.txt
$ cat file.txt
1
insert after
2
3
复制代码

Wherein 1arepresents (After) inserted after the first row

  1. Inserting a row line at the end
$ sed -i "1i insert before" file.txt
$ cat file.txt
insert before
1
2
3
复制代码

Wherein 1irepresents inserted before the first line

delete

$ sed -i '2,3d' file.txt
$ cat file.txt
1
复制代码

Delete Row can delete a row ( 3ddelete the third row), you can also write a scope ( 2,3ddelete 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/don behalf delete everything beginning with 2 lines)

Replace the line

$ sed -i '2c replace' file.txt
$ cat file.txt
1
replace
3
复制代码

2c replaceRepresents 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 replaceon 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-4between the rows coding3minstringtom

$ 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/gRepresentative matching text line is not limited, to remove gthe 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 -nparameter p + pattern matching can find and output sj

Automatically creates a backup file

Of course, direct sed -ilikely to result in the replacement error, cry no way to cry! It is necessary to advance with -n+pthat 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.bakorsed -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 grepuse 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 -rlrecursive 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, ( -noption 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 ~


Guess you like

Origin blog.csdn.net/weixin_34289454/article/details/91388297