Reputation: 1066
I want to search for patterns in a file and remove the lines containing the pattern. To do this, am using:
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
This works fine for the first iteration. For the second run, it throws :
grep: input file ‘3.txt’ is also the output
Any solutions or alternate methods?
Upvotes: 1
Views: 309
Reputation: 1066
Used this to fix the problem:
while read line
do
echo "Removing"
echo $line
grep -v "$line" $temp | tee $outputFile
temp=$outputFile
done <$falseFailures
Upvotes: 0
Reputation: 157947
Use sed
for this:
sed '/.*pattern.*/d' file
If you have multiple patterns you may use the -e
option
sed -e '/.*pattern1.*/d' -e '/.*pattern2.*/d' file
If you have GNU sed
(typical on Linux) the -i
option is comfortable as it can modify the original file instead of writing to a new file. (But handle with care, in order to not overwrite your original)
Upvotes: 0
Reputation: 6984
Trivial solution might be to work with alternating files; e.g.
idx=0
while ...
let next='(idx+1) % 2'
grep ... $file.$idx > $file.$next
idx=$next
A more elegant might be the creation of one large grep
command
args=( )
while read line; do args=( "${args[@]}" -v "$line" ); done < $whiteList
grep "${args[@]}" $origFile
Upvotes: -1
Reputation: 23374
The following should be equivalent
grep -v -f "$whiteListOfErrors" "$originalLogFile" > "$outputFile"
Upvotes: 3
Reputation: 1434
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
Upvotes: 0