下面举的例子是删除文本类文件的某段内容,然后借助临时文件保存
#!/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}
本文介绍了一个Bash脚本实例,该脚本用于从指定的文本文件中删除特定行范围的内容,并通过创建临时文件来确保数据的安全更新。此方法适用于自动化文件维护任务。
90

被折叠的 条评论
为什么被折叠?



