Linux sed command

Source: Internet
Author: User

Action:
P Print Output
D Delete operation
A adds a line following the specified line
I add a line before the specified line

1. View operation

123 sed-n ‘5p‘file//只查看文件的第5行sed-n ‘3,5p‘file//查看文件的第3到第5行sed-n ‘5,$ p‘file//打印第5到最后一行

  

2. Delete operation

123 sed‘3 d‘1.txt                                //删除文件的第3行 ,然后将内容进行输出,不是直接操作的文件。sed ‘3,5 d‘1.txt                            //删除第三行到五行。sed‘3,$ d‘1.txt                           //删除第三行到最后一行。

  


3. Add operation

123 sed‘2a word‘ file                          //在第二行后面添加一行,内容为word sed ‘3i word‘ file//在第三行前面添加一行,内容为word

  

3, Find operations

123 sed  -n   '/root/p '  file                            //  sed   '/root/d '  < Code class= "Bash functions" >file                              // Delete the row containing the root, then output, plus-I can manipulate the original file

  


4. Replace operation

123 sed‘s/root/new/‘file//将文件中的root替换成new,每行只替换一次sed‘s/root/new/g‘file//将每行的所有root全部替换成new        g替换多次sed‘3,4s/root/new/g‘1.txt                  //只替换第3到第4行的root为new

  

Integrated use of SED

*****************************************************

The contents of the document are as follows:

19-Lo Jinhui: http://www.cnblogs.com/jw1519
20-Honeydew Autumn: Http://www.cnblogs.com/bazhahei
21-Zhang Jing: http://www.cnblogs.com/zj21
22-Huang Tinghui: http://www.cnblogs.com/1796440575hth
23-Liu Bo: http://www.cnblogs.com/oppppo
24-Jie: Http://www.cnblogs.com/duolaameng
25-Houbao:

123456789101112131415161718192021 1、只输出学生姓名catjw |sed‘s/[0-9 a-Z ://.\-]/ /g‘2、只输出每个学生的urlcatjw |sed‘s/.*:\/.//g‘|sed‘/.*:/d‘ 3、只输出个人博客里的学号catjw |sed‘s/-.*/ /g‘4、只输出个人博客中,两个字姓名的学生名catjw |sed‘s/[0-9 a-Z ://.\-]//g‘|grep-v‘...‘ 5、只输出每个人博客地址的最后一段,即.com/后面的一段内容catjw |sed‘s/.*\// /g‘|sed‘s/.*://g‘6、只输出最后一段全部是字母的catjw |sed‘s/.*\// /g‘|sed ‘s/.*[0-9].*/ /‘|7、只输出最后一段含有大写字母的catjw |sed‘s/.*\// /g‘|sed-n ‘/[A-Z]/p‘

Change the last line of/etc/passwd to/bin/bash

1 cat/etc/passwd|sed ‘s/\(.*:\)\(.*\)/\1\/bin\/bash/‘                       //(.*:) 第一部分  (.*)第二部分        1表示第一部分

2. Add the user name in front of/etc/passwd per line to the last paragraph
such as: Root:x:0:0:root:/root:/bin/bash
Should be processed after the
Root:x:0:0:root:/root:/bin/bash:root

1 catpasswd |sed ‘s/\(^[a-Z]*\)\(.*\)/&:\1/‘                       //&表示全部

 

3. A text reads as follows:
192.168.100.2
172.16.5.4
10.101.2.5
133.37.25.21

It is processed by command to the following effect (converted to a line, separated by commas, each line of content enclosed in single quotation marks)
' 192.168.100.2 ', ' 172.16.5.4 ', ' 10.101.2.5 ', ' 133.37.25.21 '

1 cat  1.txt | sed     | tr   \ n ' |sed ' s/,$/\ n /g                        //

 

Linux sed command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.