21 Instances of SED

Source: Internet
Author: User

1. Delete the first character of each line of the file.
Sed ' s/.//' install.log
Sed-n ' S/^.//GP '/etc/passwd
Sed-nr ' s/(.) (. *)/\2/p '/etc/passwd2. Delete the second character of each line of the file.
Sed ' S/.//2 ' install.log
Sed ' s/(.) (.) (. *)/1\3\/' Install.log
Sed-nr ' s/(.) (.) (. *)/\1\3/p '/etc/passwd
3. Delete the last character of each line of the file.
sed ' s/.$//' install.log
Sed-nr ' s/.$//p '/etc/passwd
Sed-nr ' s/(. *) (.) /\1/p '/etc/passwd4. Delete the second-to-last character of each line of the file.
sed-r ' s/(. *) (.) (.) /\1\3/' Install.log
Sed-nr ' s/(. *) (.) (.) /\1\3/p '/etc/passwd
5. Delete the second word in each line of the file.
sed-r ' s/([a-z]+) ([^a-z]+) ([a-z]+] ([^a-z]+) (. *)/\1\2\4\5/' Install.log
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-r ' s/([a-z]+) ([^a-z]+) (. *) ([a-z]+] ([^a-z]+] ([a-z+])/\1\2\3\4\6/' Install.log
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-r ' s/(. *) ([^a-z]+) ([a-z]+)/\1\2/'/etc/passwd
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-r ' s/(.) (.) /\2\1/'/etc/passwd
Sed-nr ' s/(.) (.) (. *)/\2\1\3/p '/etc/passwd
9, swaps the first character and the second word of each line.
sed-r ' s/(^.) ([^a-z]*) ([a-z]+) ([^a-z]+) ([a-z]+)/\5\2\3\4\1/'/etc/passwd
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-r ' s/([a-z]+) ([^a-z]+) (. *) ([^a-z]+] ([a-z]+)/\5\2\3\4\1/' Install.log
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 ' install.log
sed ' s/[0-9]*//g '/etc/passwd
12, remove all spaces at the beginning of each line.
sed ' s/^ *//' Install.log
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-r ' s/(+)/\t/g ' Install.log
Sed ' s//\t/g ' ww.txt
sed-n ' s/\/\t/gp ' pass
14, enclose all uppercase letters in parentheses ().
sed ' s/[a-z]/(&)/g '/etc/passwd
Sed-nr ' s/([A-z])/(&)/GP ' TESTP
sed-n ' s/[a-z]/(&)/gp ' TESTP15, print 3 times per line.
sed-n ' p;p;p '/etc/passwd
sed ' p;p ' pass
16, interlace Delete.
sed ' 1d;n;d ' ww.txt
Sed ' 1~2d ' ww.txt
Sed ' 0~2d ' ww.txt
sed-n ' 1~2p ' pass
17, copy the file from line 22nd to line 33rd to the end of line 56th.
cat-n/etc/passwd | sed ' 22h;23,33h;51g '
sed ' 1,21h;22h;23,33h;44g ' pass
18, move the file from line 22nd to line 33rd to the end of line 56th.
cat-n/etc/passwd | sed ' 22{h;d};23,33{h;d};51g '
sed ' 22{h;d};23,33{h;d};44g ' pass
19, show only the first word of each line
sed-r ' s/([a-z]+) (. *)/\1/' Install.log
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-r ' s/([a-z]+) ([^a-z]+) ([a-z]+] (. *)/\1\3/' Install.log
Sed-r ' s/([^a-z]*) ([a-z]+] ([^a-z]+) ([a-z]+] ([^a-z]+) ([a-z]+] (. *)/\2\6/'/etc/passwd
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
echo mm/yy/dd | sed-n ' s#/#; #gp '
Date +%m/%y/%d |sed-n ' s#/#; #gp '
Sed-r ' [email protected]/@;@g '/etc/passwd

This article is from the "Small Zheng Technology Blog" blog, please be sure to keep this source http://ko178.blog.51cto.com/220419/1765204

21 Instances of 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.