sed,曾用過的操作

2021-10-08 22:57:35 字數 529 閱讀 4329

刪除空行

sed "/^$/d" test.txt
hello 前面新增 world

sed "s/hello/&world/" test.txt
在有 hello 的行前面新增一行world

sed '/hello/i\world' test.txt
在有 hello 的行後面新增一行world

sed '/hello/a\world' test.txt
hello 後面新增 world

sed '/hello//' test.txt
刪除有 hello 的行及其後一行

sed '/hello/,+1d' test.txt
檢視 5 到 10 行中有 hello 的行

sed -n '5,10' test.txt

sed操作指令

1 替換 1.txt文字中old為new,如下 sed s old new g 1.txt 2 列印 1.txt文字第一行至第三行,如下 sed n 1,3p 1.txt 3 列印 1.txt文字中第一行與最後一行,如下 sed n 1p p 1.txt 4 刪除 1.txt第一行至第三行 刪除匹配...

Sed命令基礎操作

sed 用法的小技巧 1 在查詢範圍時不需要用到替換,所以不用 s 2 當只需要列印被修改行時,可以使用 n 和 p 選項,注意二者一定配合使用 3種方式指定命令列上的多重指令 1 用逗號分隔指令 2 在每個指令前放置 e 3 輸入單分號後敲擊 return 刪除 d命令 sed 2d exampl...

sed對檔案的寫操作

原始資料 cat data line 1line 2line 3line 4line 5line 6line 7line 8line 9line 10將第1,2行儲存到新檔案中 sed 1,2w test data line 1line 2line 3line 4line 5line 6line 7...