Linux Learning Notes (27) SED

Source: Internet
Author: User

Sed
Sed is a stream editor, which is a very useful tool in text processing and can be used perfectly in conjunction with regular expressions. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output. SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program and so on.

sed -n ‘5‘p test.txt                             //输出第5行 sed -n ‘1,5‘p test.txt                          //输出1-5行 sed -n ‘1,$‘p test.txt                          //输出第一行到最后一行,$指最后一行 sed -n ‘/root/‘p test.txt                       //输出含有root的行 sed -n ‘/root/‘Ip test.txt                      //输出含有root的行,不区分大小写 sed -n ‘/^1/‘p test.txt                         //输出以数字1开头的行 sed -n ‘/in$/‘p test.txt                        //输出以in结尾的行 sed -n ‘/r..o/‘p test.txt                       //输出ro中间有两个字符的行 sed -n ‘oo*‘p test.txt                          //输出o+o重复n次的行 sed -e ‘1‘p -e ‘/111/‘p -n test.txt             //输出第一行和含有111的行






sed ‘1‘d test.txt                                // 删除第一行(只是输出到屏幕,文件内容不变)sed  -i ‘1‘d test.txt                            //删除第一行 (文件内容改变)head test.txt|sed -r ‘s/([^:]+):(.*):([^:]+)/\3:\2:\1/‘  以:分隔,将第一段和最后一段字符互换位置sed -r ‘s/(.*)/abc:&/‘ test.txt   //在每一行前面增加abc: sed ‘1,3‘d test.txt                             // 删除第1-3行 sed ‘/oot/‘d test.txt                           //删除有oot的行 sed ‘1,2s/ot/to/g‘ test.txt                     //将前两行的所有ot替换为to sed ‘s#ot#to#g‘ test.txt                        //将所有的ot替换为to sed ‘s/[0-9]//g‘ test.txt                       //将所有数字替换为空 sed ‘s/[a-zA-Z]//g‘ test.txt                    //将所有字母替换为空 sed -r ‘s/(rot)(.*)(bash)/\3\2\1/‘ test.txt     //将开头是rot,结尾是bash的行中,rot与bash换位置 sed ‘s/^.*$/123&/‘ test.txt                     //所有行前增加123 sed -i ‘s/ot/to/g‘ test.txt                     //将ot改为to(改变文件内容)







Linux Learning Notes (27) SED

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.