Linux如何删除指定行,用shell脚本实现

2025-04-13 21:05:49
推荐回答(2个)
回答1:

#!/bin/sh
FILENAME=file.txt # 可换为$1

if [ ! -f file.txt ];then
echo "$FILENAME no found"
exit 1
fi
echo -n "INPUT SOME THING: "
read
if [ ! -z ”$REPLY“ ];then
INFO=$(grep $REPLY FILENAME)
if [ ! -z "$INFO" ] ; then
sed -i -e '/$REPLY/d' FILENAME #删除只用这行就可以了
else
echo "没有指定内容的信息"
exit 1
fi
else
echo "input some thing"
exit 1
fi
exit $?

回答2:

sed '/yourstring/'d yourfile