Linux bash删除文件中含“指定内容”的行功能示例

发布时间:2022-04-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Linux bash删除文件中含“指定内容”的行功能示例脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了Linux bash删除文件中含“指定内容”的行功能。分享给大家供大家参考,具体如下:

#!/bin/sh
# 功能:    删除文件中含"指定内容"的行
# 运行方式: ./dline.sh c.LOG  ==> 产生输出文件: c.log0
array=(
  "rm -f lvr_3531_pf_new"
  "arm-hisiv100-linux-gcc "
  "In function "
  "excess elements IN ARRAY inITializer"
  "warning: multi-line comment"
  "embedded '\\0' in format"
  "__NR_SysCALL_BASE"
  "this is the location of the previous definition"
  "dereferencing tyPE-punned pointer will break strict-aliasing rules"
  "differ in signedness"
  "but argument is of type"
  "implicit declaration of"
)
if [ $# -lt 1 ]; then
  echo "usage: $0 <logfile>"
  exit
fi
file="$1"0
cp -f $1 $file
function deleteLine()
{
  sed "/$1/d" $file > tmp
  mv -f tmp $file
}
wc -l $file
for line in "${array[@]}"
do
  if [ ${#line} -gt 0 ] && [ ${line:0:1} != "#" ]; then
    deleteLine "$line"
  fi
done
wc -l $file

运行情况:

[feng@bash #69]$./dline.sh c.log
556 c.log0
63 c.log0
[feng@bash #70]$

希望本文所述对大家bash shell学习有所帮助。

脚本宝典总结

以上是脚本宝典为你收集整理的Linux bash删除文件中含“指定内容”的行功能示例全部内容,希望文章能够帮你解决Linux bash删除文件中含“指定内容”的行功能示例所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。