Some uses of shell SED

Source: Internet
Author: User

#1, replace

Replace 2017 in #将1. txt file with 2016 display on screen

Sed "s/2017/2016/g" 1.txt

Replace 2017 in #将1. txt file with 2016 display to completely modify the file on the screen, plus I modify the file

Sed-i "S/2017/2016/g" 1.txt

#Sed读取系统变量, variable substitution

date= "sed" s/2017/$DATE/g "1.txt

#关闭SELinux, modify the SELinux policy enforcing to Disabled, find the SELinux row, and then change its row enforcing value to disabled,!s to not include SELinux rows:

Sed-i "/selinux/s/enforcing/disabled/g"/etc/selinux/configsed-i "/selinux/!s/enforcing/disabled/g"/etc/selinux/ Config


#2, printing

#打印1 the first line of the. txt text to the third line

Sed-n "1,3p" 1.txt

#3打印1 the first and last line of the. txt text

Sed-n "1p, $p" 1.txt


#3, delete

#删除第一行至第三行

Sed ' 1,3d ' 1.txt

#删除匹配行至最后一行

Sed '/2017/, $d ' 1.txt

#删除最后一行

Sed ' $d ' 1.txt

#删除最后6行 (do 6 delete last line)

For i in {1..6};d o sed-i ' $d ' 1.txt;d One

#删除1. txt any space before and at the end of a line:

Sed ' s/^[\t]*//;s/[\t]*$//' 1.txt

#删除所有空格

Sed ' s/[\t]//g ' 1.txt


#4, adding characters

#在1. txt to find the row for you and add the me character to the next line, a means to add a string on its next line:

Sed '/you/ame ' 1.txt

#在1. txt to find the row where you are, and add the me character on the line above it, I means to add a string on its previous line:

Sed '/you/ime ' 1.txt

#在1. txt to find the end of the line ending with a string me,$ to indicate the end of the identification,& in sed to add:

Sed ' s/you$/&me/g ' 1.txt

#在1. txt to find a line for you, add a string at the beginning of the me,^ to represent the starting identity,& in sed to add:

Sed '/you/s/^/&me/' 1.txt


#5, multiple sed command combinations

#使用-e parameter

Sed-e '/www.jd.com/s/^/&1./'-e ' s/www.jd.com$/&./g ' 1.txt

#使用分号 ";" Segmentation

Sed-e '/www.jd.com/s/^/&1./;s/www.jd.com$/&./g ' 1.txt


#6, inserting blank lines

#每一行后边插入一行空行

Sed '/^$/d; G ' 1.txtsed ' G ' 1.txt

#每一行后边插入两行空行

Sed '/^$/d; G G ' 1.txtsed ' G; G ' 1.txt

#前三行每行后插入空行

Sed '/^$/d;1,3g; ' 1.txt

#第三行后插入空行

Sed '/^$/d;3g; ' 1.txt

#第三行后插入3行空行

Sed '/^$/d;3{g; G G} ' 1.txt

#匹配行后插入空行

Sed '/2017/g ' 1.txt

#在匹配行前一行插入空行

Sed '/2017/{x;p;x;} ' 1.txt

#同时在匹配前后插入空行

Sed '/2017/{x;p;x; G;} ' 1.txt


#7, add sequential number, tab \ t, and. Symbol before 1.txt per line:

sed = 1.txt| Sed ' n;s/\n//' sed = 1.txt| Sed ' n;s/\n/\t/' sed = 1.txt| Sed ' n;s/\n/\./'


#8, print, and delete the last two lines of 1.txt:

Sed ' $! n;$! D ' 1.txtsed ' n;$! p;$! D; $d ' 1.txt


#9, merge the next two lines, that is, two lines of merging:

Sed ' $! n;s/\n//' 1.txtsed ' n;s/\n//' 1.txt


This article is from the "Mo Jing" blog, reproduced please contact the author!

Some uses of shell SED

Related Article

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.