How to use the various symbols in the sed command

When you use linux commands, you must not forget that a command document man introduces it in detail.
The sed command is very powerful, and basically all the repetitive tasks of the text can be handled by it. It supports regular expressions, multi-point editing, etc.

a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)
c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行)
p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!

We have a text file with the following content. At this time, I need to comment out the beginning of test. How to write the sed command at this time?

ubuntu@xxx:/tmp/sed$ cat test
test123
test234
hello
java
python
php
golang

After executing the following command, it is found that the beginning of test is commented

ubuntu@xxx:/tmp/sed$ sed -ri 's@(^test.*)@#\1@g' test
ubuntu@xxx:/tmp/sed$ cat test
#test123
#test234
hello
java
python
php
golang

So we have to resolve at this command sedin -rthe identification regular expressionsof regular expressions -ito identify edit files, parameters -iand actions inside iis not the same sense do not confuse (^test.*)this is the regular expression on behalf of testthe back just the beginning of what is now the one or more characters and grouped . The delimiter of the @symbol identification sedcommand can also be used #or /replaced. If the sentence you write contains different symbols, using another delimiter will avoid conflict with the text to be operated and avoid the trouble of escaping. It looks more concise. The final \1Representative front of the main values of the packet s@and @g sthe replacement command grepresents the global.
So we just inserted the #number in the front , what should I do if I need to insert it in the back?
Next, we will #kill the number first and then insert it in the back

ubuntu@xxx:/tmp/sed$ sed -ri 's@#@@g' test
ubuntu@xxx:/tmp/sed$ cat test
test123
test234
hello
java
python
php
golang

I'm sorry I just accidentally replaced the lines 1 and 2 with #

ubuntu@xxx:/tmp/sed$ cat test
#
#
hello
java
python
php
golang

Now replace the first line back to test123 and the second line back to test234

#这里仅替换第一行
ubuntu@xxx:/tmp/sed$ sed -i '1s@#@test123@g' test
ubuntu@xxx:/tmp/sed$ cat test
test123
#
hello
java
python
php
golang
#这里可以用,隔开表示范围内遇到# 就替换成test234
ubuntu@xxx:/tmp/sed$ sed -i '1,3s@#@test234@g' test
ubuntu@xxx:/tmp/sed$ cat test
test123
test234
hello
java
python
php
golang
#在test之后添加 可以使用&符号&可以理解为当前查找到的值
ubuntu@xxx:/tmp/sed$ sed -ri 's@(^test)@&#@g' test
ubuntu@xxx:/tmp/sed$ cat test
test#123
test#234
hello
java
python
php
golang

If you use & instead of the above regular grouping, you can write it like this

ubuntu@xxx:/tmp/sed$ sed -ri 's@(^test)@#&@g' test
ubuntu@xxx:/tmp/sed$ cat test
#test#123
#test#234
hello
java
python
php
golang

Next, the above two lines have been used and we can delete them using the following command d

ubuntu@yaozhongjie:/tmp/sed$ sed -ri '1,2d' test
ubuntu@yaozhongjie:/tmp/sed$ cat test
hello
java
python
php
golang

I just accidentally changed the text like this again, how do I restore it?

ubuntu@xxx:/tmp/sed$ cat test
@test123
hello
@test123
java
@test123
python
@test123
php
@test123
golang

It’s okay to perform match deletion.

ubuntu@yaozhongjie:/tmp/sed$ sed -i '/@test123/d' test
ubuntu@yaozhongjie:/tmp/sed$ cat test
hello
java
python
php
golang

Is it very powerful?
Next, add a blank line for each line

ubuntu@yaozhongjie:/tmp/sed$ sed -i 'G' test
ubuntu@yaozhongjie:/tmp/sed$ cat test
hello

java

python

php

golang

How to eliminate blank lines now? Found that there is no the same delete all the beginning of $ In linux, $ means line break

ubuntu@yaozhongjie:/tmp/sed$ sed -i '/^$/d' test
ubuntu@yaozhongjie:/tmp/sed$ cat test
hello
java
python
php
golang

Guess you like

Origin blog.csdn.net/a807719447/article/details/112861051