【Linux shell】sed实践(3)

    xiaoxiao2022-07-16  163

    1、特殊字符&的应用 #sed 's/sd/d&/g' test.file 将文件中的sd字符替换成dsd字符 #sed 's/wcnm/&b/g' test.file 将文件中的wcnm字符替换成wcnmb字符 & : 代表其前 pattern 字串,直被替换的字串 2、i和a匹配字符指定行上下插入新的一 #sed '/lg/a\test' test.file    或者    #sed '/lg/a test' test.file 匹配含有lg的行并在该行的下方插入新的一行test #sed '/lg/i\test' test.file    或者    #sed '/lg/i test' test.file 匹配含有lg的行并在该行的上方插入新的一行test 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 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字母 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字母 相关资源:新版Linux Shell编程实训(全)20170518.docx
    最新回复(0)