Linux 删除与图案匹配的线条

Linux 删除与图案匹配的线条,linux,shell,loops,grep,Linux,Shell,Loops,Grep,我想在文件中搜索模式并删除包含该模式的行。为此,我使用: originalLogFile='sample.log' outputFile='3.txt' temp=$originalLogFile while read line do echo "Removing" echo $line grep -v "$line" $temp > $outputFile temp=$outputFile done <$whiteListOfErrors

我想在文件中搜索模式并删除包含该模式的行。为此,我使用:

originalLogFile='sample.log'
outputFile='3.txt'
temp=$originalLogFile 

 while read line
 do
    echo "Removing" 
    echo $line
    grep -v "$line" $temp > $outputFile
    temp=$outputFile
done <$whiteListOfErrors
任何解决方案或替代方法?

originallofile='sample.log'
originalLogFile='sample.log'
outputFile='3.txt'
tmpfile='tmp.txt'
temp=$originalLogFile 
while read line
do
   echo "Removing" 
   echo $line
   grep -v "$line" $temp > $outputFile
   cp $outputfile $tmpfile
   temp=$tmpfile
done <$whiteListOfErrors
outputFile='3.txt' tmpfile='tmp.txt' temp=$originallofile 读行时 做 回音“删除” 回音$线 grep-v“$line”$temp>$outputFile cp$outputfile$tmpfile 温度=$tmpfile
完成为此使用
sed

sed '/.*pattern.*/d' file
如果您有多个图案,可以使用
-e
选项

sed -e '/.*pattern1.*/d' -e '/.*pattern2.*/d' file

如果您使用了
GNU-sed
(在Linux上是典型的),那么
-i
选项会很舒服,因为它可以修改原始文件,而不是写入新文件。(但要小心处理,以免覆盖您的原件)

以下内容应等效

grep -v -f  "$whiteListOfErrors" "$originalLogFile" > "$outputFile"

简单的解决方案可能是使用交替文件;e、 g

idx=0
while ...
    let next='(idx+1) % 2'
    grep ... $file.$idx > $file.$next
    idx=$next
更优雅的方法可能是创建一个大的
grep
命令

args=( )
while read line; do args=( "${args[@]}" -v "$line" ); done < $whiteList
grep "${args[@]}" $origFile
args=()
读行时;do args=(“${args[@]}”-v“$line”);完成<$whiteList
grep“${args[@]}”$origFile

使用此选项修复问题:

while read line
do
    echo "Removing" 
    echo $line
    grep -v "$line" $temp | tee $outputFile 
    temp=$outputFile
done <$falseFailures
读取行时
做
回音“删除”
回音$线
grep-v“$line”$temp | tee$outputFile
temp=$outputFile

前面和后面的
*
都是多余的。无论如何,还是按照@1_CR的答案去做。是的
grep
在这里更好,像这样使用
sed
不能很好地处理多个模式。
while read line
do
    echo "Removing" 
    echo $line
    grep -v "$line" $temp | tee $outputFile 
    temp=$outputFile
done <$falseFailures