知道txt文件两行的行号,想把这两行之间的内容全部删除怎么做?
文章来源: 在城里2015-05-15 20:32:10
比如,想删除文件sample.txt的4和6行之间的内容,保存为newtxt.txt,可以这样:

解法1)sed '4,6d' sample.txt > newtxt.txt
[注:如果是windows环境,请安装Git Bash for windows]

解法2)用UltraEdit打开文件sample.txt,创建并运行宏:
ColumnModeOff
HexOff
GotoLine 4 1
Loop 3
SelectLine
DeleteLine
EndLoop
最后保存为newtxt.txt。

解法3)用python:
with open("n:\\desktop\\sample.txt") as src:
    lines=src.readlines()
with open("n:\\desktop\\newtxt.txt", "w") as dest:
    dest.writelines( lines[0:3] + lines[6:] )

在城里 2015.05.15