sed編輯器不會修改原始文件。你刪除的行只是從sed編輯器的輸出中消失了。原始文件仍然包含那些“刪掉的”行
macname@localhost Desktop % cat data1
1The quick brown fox jumps over the lazy dog
2The quick brown fox jumps over the lazy dog
3The quick brown fox jumps over the lazy dog
4The quick brown fox jumps over the lazy dog
5The quick brown fox jumps over the lazy dog
6The quick brown fox jumps over the lazy dog
7The quick brown fox jumps over the lazy dog
macname@localhost Desktop %
刪除第二行
macname@localhost Desktop % sed '2d' data1 1The quick brown fox jumps over the lazy dog 3The quick brown fox jumps over the lazy dog 4The quick brown fox jumps over the lazy dog 5The quick brown fox jumps over the lazy dog 6The quick brown fox jumps over the lazy dog 7The quick brown fox jumps over the lazy dog
刪除第二行和第三行
macname@localhost Desktop % sed '2,3d' data1 1The quick brown fox jumps over the lazy dog 4The quick brown fox jumps over the lazy dog 5The quick brown fox jumps over the lazy dog 6The quick brown fox jumps over the lazy dog 7The quick brown fox jumps over the lazy dog
刪除第二行和第三行
macname@localhost Desktop % macname@localhost Desktop % cat ddd 一The quick brown fox jumps over the lazy dog 二The quick brown fox jumps over the lazy dog 三The quick brown fox jumps over the lazy dog 四The quick brown fox jumps over the lazy dog 五The quick brown fox jumps over the lazy dog 六The quick brown fox jumps over the lazy dog 七The quick brown fox jumps over the lazy dog macname@localhost Desktop % macname@localhost Desktop % sed '2,3d' ddd 一The quick brown fox jumps over the lazy dog 四The quick brown fox jumps over the lazy dog 五The quick brown fox jumps over the lazy dog 六The quick brown fox jumps over the lazy dog 七The quick brown fox jumps over the lazy dog
刪除第三行,以及之后的所有行
macname@localhost Desktop % sed '3,$d' data1 1The quick brown fox jumps over the lazy dog 2The quick brown fox jumps over the lazy dog
刪除第二行和第五行,以及這兩行之間的所有行
macname@localhost Desktop % sed '/2/,/5/d' data1 1The quick brown fox jumps over the lazy dog 6The quick brown fox jumps over the lazy dog 7The quick brown fox jumps over the lazy dog macname@localhost Desktop %
這里是通過內容匹配的
macname@localhost Desktop % cat ddd 一The quick brown fox jumps over the lazy dog 二The quick brown fox jumps over the lazy dog 三The quick brown fox jumps over the lazy dog 四The quick brown fox jumps over the lazy dog 五The quick brown fox jumps over the lazy dog 六The quick brown fox jumps over the lazy dog 七The quick brown fox jumps over the lazy dog macname@localhost Desktop % macname@localhost Desktop % sed '/2/,/5/d' ddd 一The quick brown fox jumps over the lazy dog 二The quick brown fox jumps over the lazy dog 三The quick brown fox jumps over the lazy dog 四The quick brown fox jumps over the lazy dog 五The quick brown fox jumps over the lazy dog 六The quick brown fox jumps over the lazy dog 七The quick brown fox jumps over the lazy dog macname@localhost Desktop % sed '/二/,/五/d' ddd 一The quick brown fox jumps over the lazy dog 六The quick brown fox jumps over the lazy dog 七The quick brown fox jumps over the lazy dog macname@localhost Desktop %
除此之外,你要特別小心,因為只要sed編輯器在數據流中匹配到了開始模式,刪除功能就 會打開。這可能會導致意外的結果。
第二個出現數字“1”的行再次觸發了刪除命令,因為沒有找到停止模式,所以就將數據流中的剩余行全部刪除了。
macname@localhost Desktop % cat data1 1The quick brown fox jumps over the lazy dog 2The quick brown fox jumps over the lazy dog 3The quick brown fox jumps over the lazy dog The quick brown fox jumps over the lazy 1 dog 4The quick brown fox jumps over the lazy dog 5The quick brown fox jumps over the lazy dog 6The quick brown fox jumps over the lazy dog 7The quick brown fox jumps over the lazy dog macname@localhost Desktop % macname@localhost Desktop % sed '/1/,/3/d' data1 macname@localhost Desktop % macname@localhost Desktop %
當然,如果你指定了一個從未在文本中出現的停止模式,顯然會出現另外一個問題。
因為刪除功能在匹配到第一個模式的時候打開了,但一直沒匹配到結束模式,所以整個數據 流都被刪掉了。
macname@localhost Desktop % macname@localhost Desktop % sed '/1/,/10/d' data1 macname@localhost Desktop %