bash編程——sed 50例
sed是GNU/Linux中有用的文本處理工具。sed的完整英文名稱是Stream Editor。使用sed
命令可以很容易地完成許多簡單和複雜的文本處理任務。可以使用帶有sed命令的正則表達式來搜索、替換和刪除文本或文件中的任何特定字符串。但是此命令會臨時執行所有類型的修改,並且默認情況下不會更改原始文件內容。如果需要,用戶可以將修改後的內容存儲到另一個文件中。sed命令的基本用法在本教程中通過使用50個獨特的示例進行了說明。
在開始本教程之前,您必須通過運行以下命令來檢查操作系統中sed的安裝版本。
$ sed --version
sed (GNU sed) 4.4
sed命令基本格式:
sed [options]… [script] [file]
1、基本字符串查找替換
將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
2、替換指定行匹配的所有字符串
創建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
3、替換每一行中匹配第二次之後的字符串
'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
4、正則匹配每行,部分保留,部分替換
創建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
5、替換第一行中第一個匹配的字符串
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.
6、替換最後一行第一個匹配的字符串
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.
7、替換文件路徑
jun@ubuntu:~$ echo /home/ubuntu/code/perl/add.pl | sed 's#/#\\/#g'
\/home\/ubuntu\/code\/perl\/add.pl
8、去掉路徑獲取文件名
jun@ubuntu:~$ echo "/home/ubuntu/temp/myfile.txt" | sed 's/.*\///'
myfile.txt
jun@ubuntu:~$ echo "/home/ubuntu/temp/myfile.txt" | sed 's#.*/##'
myfile.txt
9、替換有關鍵字的行中的字符串
替換包含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
10、和以上相反,替換無關鍵字的行中的字符串
jun@ubuntu:~$ sed '/EEE/! s/Count/100/;' dept.txt
CSE - 100
EEE - Count
Civil - 100
11、在關鍵字前後添加內容
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
12、刪除匹配的行
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed '/OS/d' os.txt
Windows
Linux
Android
13、刪除匹配行之後2行
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed '/Android/,+2d' os.txt
Windows
Linux
14、刪除每行結尾空格和tab等
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:~$
15、刪除關鍵字兩次出現的所有行
16、刪除空行
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed '/^$/d' os.txt
Windows
Linux
Android
OS
17、刪除無法打印的字符
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
18、在匹配行的末尾添加內容
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed '/Windows/ s/$/ 10/' os.txt
Windows 10
Linux
Android
OS
19、在匹配行之前插入內容
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed '/Linux/ s/^/iOS\n/' os.txt
Windows
iOS
Linux
Android
OS
20、在匹配內容之後插入
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed 's/droid/&\nUbuntu/' os.txt
Windows
Linux
Android
Ubuntu
OS
21、在不匹配行之後添加內容
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
22、刪除不匹配的行
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed '/Linux/!d' os.txt
Linux
23、多次匹配,在匹配字符串後添加空格
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.
24、列表2中第一列替換成列表1的第三列
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
25、替換匹配字符串並斷行
jun@ubuntu:~$ echo "Bash Perl Python Java PHP ASP" | sed 's/Python/Added Text\n/'
Bash Perl Added Text
Java PHP ASP
26、替換’\n’爲’,’
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed -z 's/\n/,/g' os.txt
Windows,Linux,Android,OS,jun@ubuntu:~$
27、替換’,‘爲’\n’
jun@ubuntu:~$ echo "Kaniz Fatema,30th,batch" | sed "s/,/\n/g"
Kaniz Fatema
30th
batch
28、忽略大小寫,刪除匹配的行
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed '/linux/Id' os.txt
Windows
Android
OS
29、忽略大小寫,替換成新字符串
jun@ubuntu:~$ echo "I like bash programming " | sed 's/Bash/PHP/i'
I like PHP programming
30、關鍵字替換成大寫
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed 's/\(Windows\)/\U\1/Ig' os.txt
WINDOWS
Linux
Android
OS
31、關鍵字替換成小寫
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed 's/\(os\)/\L\1/Ig' os.txt
Windows
Linux
Android
os
32、把文件中所有大寫轉成小寫
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed 's/\(.*\)/\L\1/' os.txt
windows
linux
android
os
33、檢索數字並在數前面添加內容
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
34、分組數字,3個數字加個’,’
jun@ubuntu:~$ echo "5098673" | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/ \1,\2/;ta'
5,098,673
35、將\t替換成4空格
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
36、替換4個連續空格爲tab
jun@ubuntu:~$ echo -e "1 2" | sed 's/\s\{4\}/\t/g' | hexdump -c
0000000 1 \t 2 \n
0000004
37、截斷所有行爲80個字符
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
38、檢索關鍵字並附加內容
jun@ubuntu:~$ echo "hello, how are you?" | sed 's/\(hello\)/\1 John/'
hello John, how are you?
39、每行匹配第二次出現的關鍵字,並在關鍵字後面附加內容
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
40、把sed命令放入文件執行
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
41、連續多行匹配,並同時替換成其他字符串
42、調換關鍵字的順序
jun@ubuntu:~$ echo "perl python" | sed -e 's/\([^ ]*\) *\([^ ]*\)/\2 \1/'
python perl
43、執行多條sed命令
jun@ubuntu:~$ echo "Ubuntu Centos Debian" | sed -e 's/Ubuntu/Kubuntu/; s/Centos/Fedora/'
Kubuntu Fedora Debian
44、sed命令拼接
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
45、在每行下面插入一個空行
jun@ubuntu:~$ cat stdlist
#ID #Name
[101] -Ali
[102] -Neha
jun@ubuntu:~$ sed G stdlist
#ID #Name
[101] -Ali
[102] -Neha
jun@ubuntu:~$
46、將所有英文數字替換成空格
jun@ubuntu:~$ cat stdlist
#ID #Name
[101] -Ali
[102] -Neha
jun@ubuntu:~$ sed 's/[A-Za-z0-9]/ /g' stdlist
# #
[ ] -
[ ] -
47、使用’&'打印匹配字符串
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
48、每行中交換關鍵字順序
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
49、單詞首字母大寫
jun@ubuntu:~$ echo "I like bash programming" | sed 's/\([a-z]\)\([a-zA-Z0-9]*\)/\u\1\2/g'
I Like Bash Programming
50、打印文件行號
jun@ubuntu:~$ cat os.txt
Windows
Linux
Android
OS
jun@ubuntu:~$ sed '=' os.txt
1
Windows
2
Linux
3
Android
4
OS