下面举的例子是删除文本类文件的某段内容,然后借助临时文件保存 #!/bin/bash ######################################################################### # # # author : Wison Xu # # # # date : 2010/12/01 # # # ######################################################################### targetfile="/root/Desktop/target.test" temp_file="/tmp/temp_${PPID}" #get number of line which contains "#begin" or "#end" begin=$(sed -n "//#begin/=" ${targetfile}) end=$(sed -n "//#end/=" ${targetfile}) # delete lines between ${begin} and ${end} of ${file} and save to ${temp_file} sed -e "${begin},${end}d" ${targetfile}>${temp_file} 2>/dev/null #sleep 2s # copy to ${targetfile}, ${targetfile}'s popedom is kept cp -f ${temp_file} ${targetfile} # delete the temp file rm -f ${temp_file}