linux 命令 sed 數據操作

搜索、顯示

wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ nl test1.txt | sed '/hello/p'
     1  hello world!
     1  hello world!
     2  newLine2
     3  hello python.
     3  hello python.
     4  hello linux!
     4  hello linux!
     5  This is a linux testfile!
     6  newLine
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$

搜索、刪除

wuzhiguo@OptiPlex-790:~/learn/linux_cmd$
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ nl test1.txt | sed '/hello/d'
     2  newLine2
     5  This is a linux testfile!
     6  newLine
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$

搜索、替換
類似於vim的替換命令。

wuzhiguo@OptiPlex-790:~/learn/linux_cmd$
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ nl test1.txt | sed 's/newLine/here is newline/g'
     1  hello world!
     2  here is newline2
     3  hello python.
     4  hello linux!
     5  This is a linux testfile!
     6  here is newline
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$

-e 多個執行命令

wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ nl test1.txt
     1  hello world!
     2  newLine2
     3  hello python.
     4  hello linux!
     5  This is a linux testfile!
     6  newLine
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ nl test1.txt | sed -e '3d' -e 's/newLine/here is newline/g'
     1  hello world!
     2  here is newline2
     4  hello linux!
     5  This is a linux testfile!
     6  here is newline
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$

xargs、find、sed

wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ find ./ -name *.txt | xargs sed -i 's/newLine/here is newline/g'
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ cat test1.txt
hello world!
here is newline2
hello python.
hello linux!
This is a linux testfile!
here is newline
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ find ./ -name *.txt | xargs sed -i '1i here is 1st line'
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ cat test1.txt
here is 1st line
hello world!
here is newline2
hello python.
hello linux!
This is a linux testfile!
here is newline
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ find ./ -name *.txt | xargs sed -i '1a here is 2nd line'
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ cat test1.txt
here is 1st line
here is 2nd line
hello world!
here is newline2
hello python.
hello linux!
This is a linux testfile!
here is newline
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章