[SOLVED] loop sed to remove all strings in file
by pedropt from LinuxQuestions.org on (#4PZJ3)
Hi , i want to use sed in a loop to remove directly all the lines containing the search variables , but in the end the file stays the same and i have no idea why .
Heres the code
Code:# Ips already blocked in firewall , get how many they are
cntfr=$(wc -l "$path/fireips" | awk '{print$1}')
# Sequence of code : Read firewall ip line by line , if ip
# Exists in logfile then remove that line from logfile
# if not exist then do nothing
for i in $(seq "$cntfr")
do
# read line i from file and get the ip
rdip=$(sed -n ${i}p < "$path/fireips")
# it may exist the subnet blocked in firewall , so filter the ip
# to be ex : 192.168.1.1 to 192.168.1.0/24
ip2="${rdip%.*}.0/24"
# Do a quick search with grep if the ip exists on logfile
ckip=$(grep "$rdip" < $cmlog)
# Do a quick search with grep if the subnet of that ip exists on #logfile
cksb=$(grep "$ip2" < $cmlog)
# Case subnet output was not empty
if [[ ! -z "$cksb" ]]
then
# Remove all lines containg that ip from main log file
sed -i -e '/$rdip/d' $cmlog
fi
# Case ip exists in logfile
if [[ ! -z "$ckip" ]]
then
# remove that ip from logfile
sed -i -e '/$ckip/d' $cmlog
fi
doneThe thing here is that code runs without any error , but in the end the logfile was not changed , this means that sed did not made the changes to current file .
Any Idea why ?


Heres the code
Code:# Ips already blocked in firewall , get how many they are
cntfr=$(wc -l "$path/fireips" | awk '{print$1}')
# Sequence of code : Read firewall ip line by line , if ip
# Exists in logfile then remove that line from logfile
# if not exist then do nothing
for i in $(seq "$cntfr")
do
# read line i from file and get the ip
rdip=$(sed -n ${i}p < "$path/fireips")
# it may exist the subnet blocked in firewall , so filter the ip
# to be ex : 192.168.1.1 to 192.168.1.0/24
ip2="${rdip%.*}.0/24"
# Do a quick search with grep if the ip exists on logfile
ckip=$(grep "$rdip" < $cmlog)
# Do a quick search with grep if the subnet of that ip exists on #logfile
cksb=$(grep "$ip2" < $cmlog)
# Case subnet output was not empty
if [[ ! -z "$cksb" ]]
then
# Remove all lines containg that ip from main log file
sed -i -e '/$rdip/d' $cmlog
fi
# Case ip exists in logfile
if [[ ! -z "$ckip" ]]
then
# remove that ip from logfile
sed -i -e '/$ckip/d' $cmlog
fi
doneThe thing here is that code runs without any error , but in the end the logfile was not changed , this means that sed did not made the changes to current file .
Any Idea why ?