linux shell 修改文本
echo
[root@DSI tmp]# echo 'yhqt1 test1' > test1.txt [root@DSI tmp]# cat test1.txt yhqt1 test1 [root@DSI tmp]# echo 'yhqt2 test2' > test1.txt [root@DSI tmp]# cat test1.txt yhqt2 test2 [root@DSI tmp]# echo 'yhqt1 test1' >> test1.txt ##追加 [root@DSI tmp]# cat test1.txt yhqt2 test2 yhqt1 test1
##增加文本
[root@DSI tmp]# cat >> test1.txt << EOF export HISTTIMEFORMAT='%F %T ' EOF
sed
[root@DSI tmp]# sed -i '$a test3' test1.txt ##$最后一行,a是新增 [root@DSI tmp]# cat test1.txt yhqt2 test2 yhqt1 test1 test3 [root@DSI tmp]# sed '/yhqt1/a\test4' test1.txt ##在yhqt1 行后面增加一行test4 yhqt2 test2 yhqt1 test1 test4 test3 [root@DSI tmp]# sed '/yhqt1/a\test5\ntest6' test1.txt ##在yhqt1 行后面增加2行 yhqt2 test2 yhqt1 test1 test5 test6 test3 [root@DSI tmp]# sed '/yhqt1/i\test7' 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/a\1111' test1.txt ##存在多行test7的情況,每個匹配的地方都會新增一行1111數據 [root@DSI tmp]# cat test1.txt yhqt2 test2 test7 1111 yhqt1 test1 test5 test6 test4 test3 test7 1111 ##如果只向第二個test7后面增加一行,可以先獲取第二個test7的行號,然后根據此行號在后面增加一行數據 ##獲取行號 [root@DSI 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 '9a\2222' 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 [root@DSI 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 [root@DSI tmp]# cat test1.txt yhqt2 test222 test7 1111 yhqt1 test1 test5 yhq1314 test6 test4 test3 test7 1111 yhq,abc1,3456 [root@DSI tmp]# sed -i 's|,|*|' test1.txt ##替換特殊字符 [root@DSI tmp]# cat test1.txt yhqt2 test222 test7 1111 yhqt1 test1 test5 yhq1314 test6 test4 test3 test7 1111 yhq*abc1,3456
sed是stream editor(流編輯器)的縮寫,是文本處理中非常重要的工具,配合正則表達式進行使用功能更強大。
sed可以替換給定文本中的字符串,可以利用正則表達式進行匹配
$ sed 's/pattern/replace_string/' file
或者
$ cat file |sed 's/patter/replaces_string/' file
使用 -i選項,可以將替換結果應用於原文件,很多在進行替換之后,借助重定向來保存文件
$ sed 's/text/replace/' file > newfile
$ mv newfile file
其實就是一個命令
$ sed -i 's/text/replace/' file
如果需要替換每一行中滿足條件的內容,加參數g
$ sed 's/pattern/replace_string/g' file
后綴/g意味着sed會替換每一處匹配,如果需要從N+1開始匹配,加參數N
[root@DSI 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
字符/在sed中作為定界符使用。可以像下面一樣
sed 's:text:replace:g'
sed 's|text|replace|g'
當定界符出現在樣式內部時,必須使用前綴\對它進行轉義
sed 's|te\|xt|replace|g'
\|是一個出現在樣式內部並經過轉義的定界符
1 移除空白行
$ sed '/^$/d' file ##/pattern/d會移除匹配樣式的行,在空白行中,行尾標記緊隨着行首標記 [root@DSI 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 [root@DSI tmp]# cat test1.txt yhqt2 test222 test7 1111 yhqt1 test1 test5 yhq1314 test6 test4 test3 test7 1111 yhq*abc1,3456 xxx
2 已匹配字符串標記&
在sed中,用&標記匹配樣式的字符串,就能夠在替換字符串時使用已匹配的內容
[root@DSI tmp]# echo this is an example | sed 's/\w\+/[&]/g' [this] [is] [an] [example] ##正則表達式\w\+ 匹配每一個單詞,然后用[&]替換它,&對應於之前所匹配到的單詞
3 子串匹配標記\1
&代表匹配給定樣式的字符串,但也可以匹配給定樣式的其中一部分
[root@DSI 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用來引用他們,這種引用稱為向后引用,在替換部分,他們的次序被更改 為\2\1,因此結果就是逆序
4 組合多個表達式
sed 'expression' | sed 'expression'
等價於
$ sed 'expression; expression'
5 引用
sed表達式通常用單引號來引用。也可以使用雙引號。雙引號會通過對表達式求值來對其進行擴展
[root@DSI tmp]# text=hello [root@DSI tmp]# echo hello world | sed "s/$text/HELLO/" ##$text的求值結果是hello HELLO world