Linux use in sed

Sed is a stream editor (stream editor) is operating, filtered and powerful tool to convert text content. Common CRUD function, filter, take the line.

increase:

Append text to the specified line after a

Insert text i

Examples: single increase

[root@localhost ~]# sed '2a 107,dandan' test.txt

124567668

dfgjkllkjhvkl

107, dandan

Adsfkadsjf

[root@localhost ~]# sed '2i 107,dandan' test.txt

124567668

107, dandan

dfgjkllkjhvkl

adsfkadsjf

One is added, is to insert a

Multi-line increase \ n newline

[root@localhost ~]# sed '2i 107,dandan\naasdfasdf' test.txt

124567668

107, dandan

aasdfasdf

dfgjkllkjhvkl

adsfkadsjf

Business Case:

In our learning system optimization, optimization has a point: Change ssh remote login service configuration. Five major operations are added in the following text profile ssh

  1. Port 52113
  2. PermintRootLogin no
  3. PermitEmptyPasswords not
  4. UseDNS no
  5. GSSAPIAuthentication no

delete

Delete the specified line d

Do not specify a few lines, delete all default

change

Replace row

C replace the old line with a new line

Text Replacement

Sed -I 's ### g' # is the delimiter

If there is no g, is not a global replacement, g is global will change

Sed -r extended regular expression (. *) \ 1

& Do the replacement batch rename files

[root@localhost ~]# cd /test
[root@localhost test]# ls
[root@localhost test]# touch stu_102999_{1..5}_finished.jpg
[root@localhost test]# ls
stu_102999_1_finished.jpg  stu_102999_3_finished.jpg  stu_102999_5_finished.jpg
stu_102999_2_finished.jpg  stu_102999_4_finished.jpg
[root@localhost test]# ls  *.jpg
stu_102999_1_finished.jpg  stu_102999_3_finished.jpg  stu_102999_5_finished.jpg
stu_102999_2_finished.jpg  stu_102999_4_finished.jpg
[root@localhost test]# ls  *.jpg | sed -r 's#(^.*)_finished.*#mv & \1.jpg#g'
mv stu_102999_1_finished.jpg stu_102999_1.jpg
mv stu_102999_2_finished.jpg stu_102999_2.jpg
mv stu_102999_3_finished.jpg stu_102999_3.jpg
mv stu_102999_4_finished.jpg stu_102999_4.jpg
mv stu_102999_5_finished.jpg stu_102999_5.jpg
[root@localhost test]# ls  *.jpg | sed -r 's#(^.*)_finished.*#mv & \1.jpg#g' |bash
[root@localhost test]# ls
stu_102999_1.jpg  stu_102999_2.jpg  stu_102999_3.jpg  stu_102999_4.jpg  stu_102999_5.jpg

check:

But the contents of the specified output p 2 default output matching, the use of n cancel the default output

[root@localhost ~]# sed '2p' test.txt 
124567668
dfgjkllkjhvkl
dfgjkllkjhvkl
adsfkadsjf
[root@localhost ~]# sed -n '2p' test.txt 
Dfgjkllkjhvkl
2-3行
[root@localhost ~]# sed -n '2,3p' test.txt 
dfgjkllkjhvkl
adsfkadsjf

1 ~ 2p is to take 1,3,5, all odd lines of odd rows show

Execute the command df -h == echo 'df -h' | bash

 

Guess you like

Origin www.cnblogs.com/huangchuan/p/11550437.html