sed是GNU/Linux中有用的文本处理工具。sed的完整英文名称是Stream Editor。使用sed
命令可以很容易地完成许多简单和复杂的文本处理任务。可以使用带有sed命令的正则表达式来搜索、替换和删除文本或文件中的任何特定字符串。但是此命令会临时执行所有类型的修改,并且默认情况下不会更改原始文件内容。如果需要,用户可以将修改后的内容存储到另一个文件中。sed命令的基本用法在本教程中通过使用50个独特的示例进行了说明。
在开始本教程之前,您必须通过运行以下命令来检查操作系统中sed的安装版本。
$ sed --version
sed (GNU sed) 4.4
sed命令基本格式:
sed [options]… [script] [file]
将Bash替换成Perl,'s’表示search和replace任务。
jun@ubuntu:~$ echo "Bash Scripting Language" | sed 's/Bash/Perl/'
Perl Scripting Language
创建weekday.txt文件内容如下:
jun@ubuntu:~$ cat weekday.txt
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
jun@ubuntu:~$ sed 's/Sunday/Sunday is holiday/' weekday.txt
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday is holiday
创建Python.txt文件内容如下:
利用行号、‘s’、‘g’,替换第2行中所有Python为perl
jun@ubuntu:~$ cat Python.txt
Python is a very popular language.
Python is easy to use. Python is easy to learn.
Python is a cross-platform language
jun@ubuntu:~$ sed '2 s/Python/perl/g' Python.txt
Python is a very popular language.
perl is easy to use. perl is easy to learn.
Python is a cross-platform language
'g’后面添加匹配每行第几次之后
jun@ubuntu:~$ sed 's/Python/perl/g2' Python.txt
Python is a very popular language.
Python is easy to use. perl is easy to learn.
Python is a cross-platform language
创建lang.txt文件内容如下:
保留Programming前面部分,后面替换为Scripting。
\1 指的是前面.*匹配保留的部分。
jun@ubuntu:~$ cat lang.txt
C Language
Shell Programming
Python Programming
Perl Programming
jun@ubuntu:~$ sed 's/\(.*\)Programming/\1Scripting/' lang.txt
C Language
Shell Scripting
Python Scripting
Perl Scripting
jun@ubuntu:~$ sed '1 s/Python/perl/' Python.txt
perl is a very popular language.
Python is easy to use. Python is easy to learn.
Python is a cross-platform language.
jun@ubuntu:~$ sed '$ s/Python/Perl/' Python.txt
Python is a very popular language.
Python is easy to use. Python is easy to learn.
Perl is a cross-platform language.
jun@ubuntu:~$ echo /home/ubuntu/code/perl/add.pl | sed 's#/#\\/#g'
\/home\/ubuntu\/code\/perl\/add.pl
jun@ubuntu:~$ echo "/home/ubuntu/temp/myfile.txt" | sed 's/.*\///'
myfile.txt
jun@ubuntu:~$ echo "/home/ubuntu/temp/myfile.txt" | sed 's#.*/##'
myfile.txt
替换包含CSE的行中的Count为95,替换包含Civil的行中的Count为100
jun@ubuntu:~$ cat dept.txt
CSE - Count
EEE - Count
Civil - Count
jun@ubuntu:~$ sed '/Civil/ s/Count/100/; /CSE/ s/Count/95/;' dept.txt
CSE - 95
EEE - Count
Civil - 100
jun@ubuntu:~$ sed '/EEE/! s/Count/100/;' dept.txt
CSE - 100
EEE - Count
Civil - 100
jun@ubuntu:~$ cat lang.txt
C Language
Shell Programming
Python Programming
Perl Programming
jun@ubuntu:~$ sed 's/\(Programming\)/is \1 Language/' lang.txt
C Language
Shell is Programming Language
Python is Programming Language
Perl is Programming Language
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed '/OS/d' os.txt
Windows
Linux
Android
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed '/Android/,+2d' os.txt
Windows
Linux
jun@ubuntu:~$ hexdump -c os.txt
0000000 W i n d o w s \t \n \n L i n u x
0000010 \n \n A n d r o i d
0000020 \n O S \n
0000028
jun@ubuntu:~$ sed 's/[[:blank:]]*$//' os.txt >os2.txt
jun@ubuntu:~$ hexdump -c os2.txt
0000000 W i n d o w s \n \n L i n u x \n \n
0000010 A n d r o i d \n O S \n
000001b
jun@ubuntu:~$
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed '/^$/d' os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ hexdump -c os.txt
0000000 \t W i n d o w s \n
0000010 \t L i n u x \n
0000020 \t A n d r o i d \n
0000030 \t O S \n
0000035
jun@ubuntu:~$ sed 's/[^[:print:]]//g' os.txt >os2.txt
jun@ubuntu:~$ hexdump -c os2.txt
0000000 W i n d o w s \n
0000010 L i n u x \n
0000020 A n d r o i d \n O S
0000030 \n
0000031
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed '/Windows/ s/$/ 10/' os.txt
Windows 10
Linux
Android
OS
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed '/Linux/ s/^/iOS\n/' os.txt
Windows
iOS
Linux
Android
OS
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed 's/droid/&\nUbuntu/' os.txt
Windows
Linux
Android
Ubuntu
OS
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed '/Linux/! s/$/ Operating System/' os.txt
Windows Operating System
Linux
Android Operating System
OS Operating System
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed '/Linux/!d' os.txt
Linux
jun@ubuntu:~$ cat Python.txt
Python is a very popular language.
Python is easy to use. Python is easy to learn.
Python is a cross-platform language.
jun@ubuntu:~$ sed 's/to /& /g' Python.txt
Python is a very popular language.
Python is easy to use. Python is easy to learn.
Python is a cross-platform language.
jun@ubuntu:~$ cat list1.txt
1001 => Jafar Ali
1023 => Nir Hossain
1067 => John Michel
jun@ubuntu:~$ cat list2.txt
1001 CSE GPA-3.63
1002 CSE GPA-3.24
1023 CSE GPA-3.11
1067 CSE GPA-3.84
jun@ubuntu:~$ sed `cat list1.txt | awk '{print "-e s/"$1"/"$3"/"}'` <<< "`cat list2.txt`"
Jafar CSE GPA-3.63
1002 CSE GPA-3.24
Nir CSE GPA-3.11
John CSE GPA-3.84
jun@ubuntu:~$ echo "Bash Perl Python Java PHP ASP" | sed 's/Python/Added Text\n/'
Bash Perl Added Text
Java PHP ASP
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed -z 's/\n/,/g' os.txt
Windows,Linux,Android,OS,jun@ubuntu:~$
jun@ubuntu:~$ echo "Kaniz Fatema,30th,batch" | sed "s/,/\n/g"
Kaniz Fatema
30th
batch
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed '/linux/Id' os.txt
Windows
Android
OS
jun@ubuntu:~$ echo "I like bash programming " | sed 's/Bash/PHP/i'
I like PHP programming
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed 's/\(Windows\)/\U\1/Ig' os.txt
WINDOWS
Linux
Android
OS
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed 's/\(os\)/\L\1/Ig' os.txt
Windows
Linux
Android
os
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed 's/\(.*\)/\L\1/' os.txt
windows
linux
android
os
jun@ubuntu:~$ cat items.txt
HDD 100
Monitor 80
Mouse 10
jun@ubuntu:~$ sed -E 's/([[:digit:]]+)/$\1/g' items.txt
HDD $100
Monitor $80
Mouse $10
jun@ubuntu:~$ echo "5098673" | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/ \1,\2/;ta'
5,098,673
jun@ubuntu:~$ echo -e "1\t2\t3" | sed 's/\t/ /g'
1 2 3
jun@ubuntu:~$ echo -e "1\t2\t3" | sed 's/\t/++++/g'
1++++2++++3
jun@ubuntu:~$ echo -e "1 2" | sed 's/\s\{4\}/\t/g' | hexdump -c
0000000 1 \t 2 \n
0000004
jun@ubuntu:~$ cat in.txt
PHP is a server-side scripting language.
PHP is an open-source language and PHP is case-sensitive. PHP is platform-independent.
The following `sed` command will truncate each line of in.txt file into 80 characters.
jun@ubuntu:~$ sed 's/\(^.\{1,80\}\).*/\1/' in.txt
PHP is a server-side scripting language.
PHP is an open-source language and PHP is case-sensitive. PHP is platform-indepe
The following `sed` command will truncate each line of in.txt file into 80 chara
jun@ubuntu:~$ echo "hello, how are you?" | sed 's/\(hello\)/\1 John/'
hello John, how are you?
jun@ubuntu:~$ cat input.txt
PHP is a server-side scripting language.
PHP is an open-source language and PHP is case-sensitive.
PHP is platform-independent.PHP PHP
jun@ubuntu:~$ sed 's/\(PHP\)/\1 (New Text added)/2' input.txt
PHP is a server-side scripting language.
PHP is an open-source language and PHP (New Text added) is case-sensitive.
PHP is platform-independent.PHP (New Text added) PHP
jun@ubuntu:~$ cat input.txt
PHP is a server-side scripting language.
PHP is an open-source language and PHP is case-sensitive.
PHP is platform-independent.PHP PHP
jun@ubuntu:~$ vim sedcmd
jun@ubuntu:~$ cat sedcmd
s/PHP/ASP/
s/independent/dependent/
jun@ubuntu:~$ sed -f sedcmd input.txt
ASP is a server-side scripting language.
ASP is an open-source language and PHP is case-sensitive.
ASP is platform-dependent.PHP PHP
jun@ubuntu:~$ echo "perl python" | sed -e 's/\([^ ]*\) *\([^ ]*\)/\2 \1/'
python perl
jun@ubuntu:~$ echo "Ubuntu Centos Debian" | sed -e 's/Ubuntu/Kubuntu/; s/Centos/Fedora/'
Kubuntu Fedora Debian
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ cat os.txt | sed 's/Linux/Fedora/'| sed 's/windows/Windows 10/i'
Windows 10
Fedora
Android
OS
jun@ubuntu:~$ cat stdlist
#ID #Name
[101] -Ali
[102] -Neha
jun@ubuntu:~$ sed G stdlist
#ID #Name
[101] -Ali
[102] -Neha
jun@ubuntu:~$
jun@ubuntu:~$ cat stdlist
#ID #Name
[101] -Ali
[102] -Neha
jun@ubuntu:~$ sed 's/[A-Za-z0-9]/ /g' stdlist
# #
[ ] -
[ ] -
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed -n 's/^L/Matched String is - &/p' os.txt
Matched String is - Linux
jun@ubuntu:~$ cat course.txt
PHP ASP
MySQL Oracle
CodeIgniter Laravel
jun@ubuntu:~$ sed 's/\([^ ]*\)\s*\([^ ]*\)/\2 \1/' course.txt
ASP PHP
Oracle MySQL
Laravel CodeIgniter
jun@ubuntu:~$ echo "I like bash programming" | sed 's/\([a-z]\)\([a-zA-Z0-9]*\)/\u\1\2/g'
I Like Bash Programming
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed '=' os.txt
1
Windows
2
Linux
3
Android
4
OS