linux 命令 sed 行操作

在test文件的第四行后添加一行,并将结果输出到标准输出,在命令行提示符下输入如下命令:

wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ cat test1.txt
hello world!
hello python.
hello linux!
This is a linux testfile!
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ sed -e 4a\newLine test1.txt
hello world!
hello python.
hello linux!
This is a linux testfile!
newLine
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ cat test1.txt
hello world!
hello python.
hello linux!
This is a linux testfile!
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ sed -i 4a\newLine test1.txt
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ cat test1.txt
hello world!
hello python.
hello linux!
This is a linux testfile!
newLine
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$

-e 执行脚本,输出到标准输出
-i 执行脚本,写入文件

第二行之前插入

wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ sed "2 inewLine2" -i test1.txt
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ cat test1.txt
hello world!
newLine2
hello python.
hello linux!
This is a linux testfile!
newLine
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$

a 之后插入
i 之前插入

将第 2~5 行删除!

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

只要删除第 2 行

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

要删除第 3 到最后一行

wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ nl test1.txt | sed '3,$d'
     1  hello world!
     2  newLine2
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$

新增- 之前、之后

wuzhiguo@OptiPlex-790:~/learn/linux_cmd$
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ nl test1.txt | sed '2i hello docker'
     1  hello world!
hello docker
     2  newLine2
     3  hello python.
     4  hello linux!
     5  This is a linux testfile!
     6  newLine
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ nl test1.txt | sed '2a hello docker'
     1  hello world!
     2  newLine2
hello docker
     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 '2a hello docker\
> hello ubuntu'
     1  hello world!
     2  newLine2
hello docker
hello ubuntu
     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$
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ find ./ -name *.txt | xargs sed -i '$a here is latest line'
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
here is latest line
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$

加空行

wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ find ./ -name *.txt | xargs sed -i '$a \\'
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
here is latest line

wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ 

替换

wuzhiguo@OptiPlex-790:~/learn/linux_cmd$
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$ nl test1.txt | sed '2,5c No 2-5 number'
     1  hello world!
No 2-5 number
     6  newLine
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$

显示

wuzhiguo@OptiPlex-790:~/learn/linux_cmd$
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ nl test1.txt | sed -n '5,7p'
     5  This is a linux testfile!
     6  newLine
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$

列操作 cut

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
here is latest line
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$ sed 's#!#!!!#g' test1.txt | cut -d" " -f 2,3
is 1st
is 2nd
world!!!
is newline2
python.
linux!!!
is a
is newline
is latest
wuzhiguo@OptiPlex-790:~/learn/linux_cmd$
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章