zl程序教程

您现在的位置是:首页 >  系统

当前栏目

【Linux shell】sed实践(3)

Linuxshell 实践 sed
2023-09-11 14:21:06 时间
decc85b7ebc4f45df7d70a22f9edd9727e18a863
2、i和a匹配字符指定行上下插入新的一行 #sed /lg/a\test test.file    或者    #sed /lg/a test test.file 匹配含有lg的行并在该行的下方插入新的一行test f665a3761e187055b77d81c21dc8bcf8b6644c4d
#sed /lg/i\test test.file    或者    #sed /lg/i test test.file 匹配含有lg的行并在该行的上方插入新的一行test
7efc503a845c69ef9e29cf17acc87143e7c89405
3、匹配一串字符串保留某些需要的字符并替换其他字符 #sed -n s/w\(cnm\)/\1d/p test.file 将cnm标记为1,并将wcnm替换为cnmd #sed -n s/\(wcn\)m/\1d/p test.file 将cnm标记为1,并将wcnm替换为wcnd ab1021f9bdbe0700185567744bc8df654bc7f406
4、同时替换多个字符 #sed 2,4s/a/b/;s/c/b/g test.file 替换2-4行第一个a字母为b字母,并替换所有行的第一个c字母为b字母 #sed 2,4s/a/b/g;s/c/b/g test.file 替换2-4行所有a字母为b字母,并替换所有行的c字母为b字母
414d0fcf588edd54a8e78cfb75e26cc39d028c0c
5、替换符合条件的字符进行替换(或) #sed s/a\|c/o/ test.file 匹配含有字母a或c的行并替换第一个字母(a或c谁在前替换谁,后面不再替换) #sed s/a\|c/o/g test.file 匹配含有字母a或c的行并将a和b全部替换为o字母 e9f8938ffe3ef8465a9aefb2532dfb577ca7cc99

Bash shell 中,三种子 shell 实践 Bash shell 中,三种子 shell 实践 让我们先来看一下下面这个简单的例子: #!/bin/bash #=============================================================================== # FILE: process_test.