The SED of shell programming

Source: Internet
Author: User

First, sed (Stream Editor)

1, positioning line:
Sed-n ' 12,~3p ' pass #从第12行开始 until the next 3 multiples line (12-15 lines)
Sed-n ' 12,+4p ' pass #从第12行开始, 4 consecutive lines (12-16 lines)
Sed-n ' 12~3p ' pass #从第12行开始, interval 3 lines output once (12,15,18,21 ...)
Sed-n ', $p ' pass #从第10行至结尾
Sed-n ' 4!p ' pass #除去第4行

2. Regular: '/regular type/'
Sed-n '/root/p '/etc/passwd
Sed-n '/^root/p '/etc/passwd
Sed-n '/bash$/p '/etc/passwd
Sed-n '/ro.t/p '/etc/passwd
Sed-n '/ro*/p '/etc/passwd
Sed-n '/[abc]/p '/etc/passwd
Sed-n '/[a-z]/p '/etc/passwd
Sed-n '/[^abc]/p '/etc/passwd
Sed-n '/^[^abc]/p '/etc/passwd
Sed-n '/\<root/p '/etc/passwd
Sed-n '/root\>/p '/etc/passwd

3. Extended Regular:
Sed-n '/root\|yerik/p '/etc/passwd #拓展正则需要转义
Sed-nr '/root|yerik/p '/etc/passwd #加-R parameter support extended regular
Sed-nr '/ro (ot|ye) rik/p '/etc/passwd #匹配rootrik和royerik单词
Sed-nr '/ro?t/p '/etc/passwd # match 0-1 leading characters
Sed-nr '/ro+t/p '/etc/passwd #匹配1-N Secondary leading character
Sed-nr '/ro{2}t/p '/etc/passwd #匹配2次前导字符
Sed-nr '/ro{2,}t/p '/etc/passwd #匹配多于2次前导字符
Sed-nr '/ro{2,4}t/p '/etc/passwd #匹配2-4 leading characters
Sed-nr '/(Root) */p '/etc/passwd #匹配0-n secondary leading word

4. Sed edit (insert, delete, replace for row)
Sed '/root/a admin '/etc/passwd #在root行后追加一个admin行
Sed '/root/i admin '/etc/passwd #在root行前插入一个admin
Sed '/root/c admin '/etc/passwd #将root行替换为admin
Sed '/root/d '/etc/passwd #删除含有root的行

S replacement
Sed-n ' s/root/admin/p '/etc/passwd
Sed-n ' s/root/admin/2p '/etc/passwd #在每行的第2个root作替换
Sed-n ' S/ROOT/ADMIN/GP '/etc/passwd
Sed-n ' 1,10 s/root/admin/gp '/etc/passwd
Sed-n ' s/root/aaa&bbb/2p '/etc/passwd #将root替换成AAArootBBB,& as a reverse reference instead of the previous match
Sed-ne ' s/root/aaa&bbb/'-ne ' s/bash/aaa&bbb/p '/etc/passwd #-e Connect multiple commands to replace root or bash lines
Sed-n ' s/root/aaa&bbb/;s/bash/aaa&bbb/p '/etc/passwd #与上命令功能相同
Sed-nr ' s/(Root) (. *) (bash)/\3\2\1/p '/etc/passwd #将root与bash位置替换, two tag replacements
or Sed-n ' s/\ (root\) \ (. *\) \ (bash\)/\3\2\1/p '/etc/passwd
Bash:x:0:0:root:/root:/bin/root

Y Replace
echo "Sorry" |sed ' y/ory/abc/' #一一对应替换 (SABBC)


6. Sed mode space and hold space
H: Mode----> Hold
H: Mode--->> hold
X: Mode <---> Hold
G: Maintain----> Mode
G: Maintain--->> mode

For example:
111
222
333
444
# sed ' 1h;2,3h;4g '

cmd         mode      hold
111          111     \ n
1h           111     111
----------->111
222          222     111
2,3h        222     111\n222 
----------->222
333          333     111\n222
2,3h        333      111\n222\n333
----------->333
444         444     111\n222\n333
4g          444\n111\ n222\n333
----------->444\n111\n222\n333


1-10
11-22
22-33
11-22
34-end

7. Special use of SED
Sed-n '/root/w a.txt ' #将匹配行输出到文件
Sed '/root/r abc.txt '/etc/passwd #把abc. txt file contents after reading into the root matching line
Sed-n '/root/w a.txt '
Sed-n '/root/{=;p} '/etc/passwd #打印行号和匹配root的行
Sed-n '/root/{n;d} '/etc/passwd #将匹配root行的下一行删除
Sed-n '/root/{n;d} '/etc/passwd #将匹配root行和下一行都删除
Sed ' 22{h;d};23,33{h;d};44g ' pass

8. Sed script Writing method
<1> read-in commands from a file
Sed-f sed.sh
sed.sh File Contents:
s/root/yerik/p
s/bash/csh/p

<2> run the script directly./sed.sh/etc/passwd
#!/bib/sed-f
s/root/yerik/p
s/bash/csh/p


###################################
Ii. sed Exercises

1. Delete the first character of each line of the file.
Sed-n ' S/^.//GP '/etc/passwd
Sed-nr ' s/(.) (. *)/\2/p '/etc/passwd
2. Delete the second character of each line of the file.
Sed-nr ' s/(.) (.) (. *)/\1\3/p '/etc/passwd

3. Delete the last character of each line of the file.
Sed-nr ' s/.$//p '/etc/passwd
Sed-nr ' s/(. *) (.) /\1/p '/etc/passwd

4. Delete the second-to-last character of each line of the file.
Sed-nr ' s/(. *) (.) (.) /\1\3/p '/etc/passwd

5. Delete the second word in each line of the file.
Sed-nr ' s/([^a-z]*) ([a-z]+) ([^a-z]+] ([a-z]+) (. *)/\1\2\3\5/p '/etc/passwd

6. Delete the second-to-last word in each line of a file.
Sed-nr ' s/(. *) ([^a-z]+) ([a-z]+) ([^a-z]+] ([a-z]+) ([^a-z]*]/\1\2\4\5\6/p '/etc/samba/smb.conf

7. Delete the last word in each line of the file.
Sed-nr ' s/(. *) ([^a-z]+) ([a-z]+) ([^a-z]*]/\1\2\4/p '/etc/samba/smb.conf

8, swaps the first character and the second character of each line.
Sed-nr ' s/(.) (.) (. *)/\2\1\3/p '/etc/passwd

9, swap the first word and the second word for each line.
Sed-nr ' s/([^a-z]*) ([a-z]+) ([^a-z]+] ([a-z]+) (. *)/\1\4\3\2\5/p '/etc/samba/smb.conf

10, swap the first word and the last word of each line.
Sed-nr ' s/([^a-z]*) ([a-z]+) ([^a-z]+] ([a-z]+) (. *)/\1\4\3\2\5/p '/etc/passwd

11, delete all the numbers in a file.
Sed ' s/[0-9]*//g '/etc/passwd

12, remove all spaces at the beginning of each line.
Sed-n ' s/^\ *//p '/etc/samba/smb.conf
Sed-nr ' s/(*) (. *)/\2/p ' TESTP

13, replace any spaces that appear in the file with tabs.
Sed-n ' s/\/\t/gp ' pass

14, enclose all uppercase letters in parentheses ().
Sed-nr ' s/([A-z])/(&)/GP ' TESTP
Sed-n ' s/[a-z]/(&)/GP ' TESTP

15, print 3 times per line.
Sed ' p;p ' pass

16, interlace Delete.
Sed-n ' 1~2p ' Pass

17, copy the file from line 22nd to line 33rd to the end of line 44th.
Sed ' 1,21h;22h;23,33h;44g ' pass

18, move the file from line 22nd to line 33rd to the end of line 44th.
Sed ' 22{h;d};23,33{h;d};44g ' pass

19, only the first word of each line is displayed.
Sed-nr ' s/([^a-z]*) ([a-z]+) ([^a-z]+] (. *)/\2/p '/etc/passwd

20, print the first word and the third word of each line.
Sed-nr ' s/([^a-z]*) ([a-z]+] ([^a-z]+) ([a-z]+] ([^a-z]+) ([a-z]+] (. *)/\2--\4/p '/etc/passwd

21, change the format of the date format mm/yy/dd to MM;YY;DD
Date +%m/%y/%d |sed-n ' s#/#; #gp '

22, Reverse output
Cat A.txt
Abc
Def
Xyz
The output style becomes
Xyz
Def
Abc

The SED of shell programming

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.