linux shell 修改文本 sed
linux shell 修改文本 [[email?protected] tmp]# echo ‘yhqt1 test1‘ > test1.txt [[email?protected] tmp]# cat test1.txt yhqt1 test1 [root@DSI tmp]# echo ‘yhqt2 test2‘ > test1.txt [[email?protected] tmp]# cat test1.txt yhqt2 test2 [root@DSI tmp]# echo ‘yhqt1 test1‘ >> test1.txt ##追加 [[email?protected] tmp]# cat test1.txt yhqt2 test2 yhqt1 test1 ##增加文本 [[email?protected] tmp]# cat >> test1.txt << EOF export HISTTIMEFORMAT=‘%F %T ‘ EOF sed [[email?protected] tmp]# sed -i ‘$a test3‘ test1.txt ##$最后一行,a是新增 [[email?protected] tmp]# cat test1.txt yhqt2 test2 yhqt1 test1 test3 [root@DSI tmp]# sed ‘/yhqt1/atest4‘ test1.txt ##在yhqt1 行后面增加一行test4 yhqt2 test2 yhqt1 test1 test4 test3 [root@DSI tmp]# sed ‘/yhqt1/atest5ntest6‘ test1.txt ##在yhqt1 行后面增加2行 yhqt2 test2 yhqt1 test1 test5 test6 test3 [root@DSI tmp]# sed ‘/yhqt1/itest7‘ test1.txt ##在yhqt1行前面增加一行test7 yhqt2 test2 test7 yhqt1 test1 test3 [root@DSI tmp]# cat test1.txt yhqt2 test2 test7 yhqt1 test1 test5 test6 test4 test3 test7 [root@DSI tmp]# sed -i ‘/test7/a1111‘ test1.txt ##存在多行test7的情况,每个匹配的地方都会新增一行1111数据 [[email?protected] tmp]# cat test1.txt yhqt2 test2 test7 1111 yhqt1 test1 test5 test6 test4 test3 test7 1111 ##如果只向第二个test7后面增加一行,可以先获取第二个test7的行号,然后根据此行号在后面增加一行数据 ##获取行号 [[email?protected] tmp]# cat -n test1.txt |grep test7 |awk ‘ {print $1}‘|sed -n "2"p 9 [root@DSI tmp]# sed -n ‘/test7/=‘ test1.txt |sed -n "2"p 9 [root@DSI tmp]# sed -e ‘9a2222‘ test1.txt yhqt2 test2 test7 1111 yhqt1 test1 test5 test6 test4 test3 test7 2222 1111 [root@DSI tmp]# sed ‘s/test5/& yhq1314/g‘ test1.txt ##在指定行test5后面增加数据yhq1314 yhqt2 test2 test7 1111 yhqt1 test1 test5 yhq1314 test6 test4 test3 test7 1111 [root@DSI tmp]# sed -i ‘s|test2|test222|‘ test1.txt ##对字符串进行替换test2替换为test222 [[email?protected] tmp]# cat test1.txt yhqt2 test222 test7 1111 yhqt1 test1 test5 yhq1314 test6 test4 test3 test7 1111 [root@DSI tmp]# sed -i ‘$a yhq,abc1,3456‘ test1.txt [[email?protected] tmp]# cat test1.txt yhqt2 test222 test7 1111 yhqt1 test1 test5 yhq1314 test6 test4 test3 test7 1111 yhq,3456 [root@DSI tmp]# sed -i ‘s|,|*|‘ test1.txt ##替换特殊字符 [[email?protected] tmp]# cat test1.txt yhqt2 test222 test7 1111 yhqt1 test1 test5 yhq1314 test6 test4 test3 test7 1111 yhq*abc1,3456 sed是stream editor(流编辑器)的缩写,是文本处理中非常重要的工具,配合正则表达式进行使用功能更强大。 $ sed ‘s/pattern/replace_string/‘ file 或者 $ cat file |sed ‘s/patter/replaces_string/‘ file 使用 -i选项,可以将替换结果应用于原文件,很多在进行替换之后,借助重定向来保存文件 $ sed ‘s/text/replace/‘ file > newfile
$ mv newfile file
其实就是一个命令 [[email?protected] tmp]# echo this thisthisthis | sed ‘s/this/THIS/2g‘ this THISTHISTHIS [root@DSI tmp]# echo this thisthisthis | sed ‘s/this/THIS/3g‘ this thisTHISTHIS [root@DSI tmp]# echo this thisthisthis | sed ‘s/this/THIS/4g‘ this thisthisTHIS #当需要从第N出匹配开始替换时,可以使用/Ng 1 移除空白行 $ sed ‘/^$/d‘ file ##/pattern/d会移除匹配样式的行,在空白行中,行尾标记紧随着行首标记 [[email?protected] tmp]# cat test1.txt yhqt2 test222 test7 1111 yhqt1 test1 test5 yhq1314 test6 test4 test3 test7 1111 yhq*abc1,3456 xxx [root@DSI tmp]# sed -i ‘/^$/d‘ test1.txt [[email?protected] tmp]# cat test1.txt yhqt2 test222 test7 1111 yhqt1 test1 test5 yhq1314 test6 test4 test3 test7 1111 yhq*abc1,3456 xxx 2 已匹配字符串标记& [[email?protected] tmp]# echo this is an example | sed ‘s/w+/[&]/g‘ [this] [is] [an] [example] ##正则表达式w+ 匹配每一个单词,然后用[&]替换它,&对应于之前所匹配到的单词 3 子串匹配标记1 [[email?protected] tmp]# echo this is digit 7 in a number | sed ‘s/digit ([0-9])/1/‘ this is 7 in a number ##这个命令将digit 7替换为7.样式中匹配到的子串是7,(pattern)用于匹配子串,模式被包括在使用斜线转义过的()中,对于匹配到的第一个子串, 其对应的标记是1,匹配到的第二个子串是2,往后依次类推 [root@DSI tmp]# echo seven EIGHT | sed ‘s/([a-z]+) ([A-Z]+)/2 1/‘ EIGHT seven ##([a-z])+)匹配第一个单词,([A-Z]+)匹配第二个单词,1,2用来引用他们,这种引用称为向后引用,在替换部分,他们的次序被更改 为21,因此结果就是逆序 4 组合多个表达式 [[email?protected] tmp]# text=hello [[email?protected] tmp]# echo hello world | sed "s/$text/HELLO/" ##$text的求值结果是hello HELLO world (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |