[SOLVED] Loop , use sed to remove and update loop final count
by pedropt from LinuxQuestions.org on (#4SHB6)
Looks complicated but it is not .
Here it is the thing , i want to search and remove directly to the file using sed , but the loop sequence will be updated after sed deleted some lines .
Imagining that loop starts inicialy a 1 and supose to end at 100 , but then some pattern was found and sed have to delete 10 lines , then loop will end at new variable witch is 100 -10 = 90 , by this the initial variable in loop is updated , but this happens consequently in this loop because sed is removing if found what i am searching .
Now the problem is that i stay in a endless loop without change because sed is not replacing the original file with the new updates .
I already had created before a similar thread , but this one a very different .
The Code
Code:# count the lines of file to loop
cntfr=$(wc -l "$cmlog" | awk '{print$1}')
# start sequence until variable cntfr
for i in $(seq "$cntfr")
do
# read the line and grab the ip
# ip will be xxx.xxx.xxx.xxx:port , so this next code
# will remove everything after the : and will get only
# the cleaned ip
var1=$(sed -n ${i}p < $cmlog | awk '{print$11}' | cut -f1 -d":")
# this one i learned here , witch is put something like
# ex: 111.111.111.111 to 111.111.111.0/24
# this will be to see if subnet exists on a file ahead
ip2="${var1%.*}.0/24"
# fireips is a file containing all the blocked ips and
# subnets in the firewall
# search for the subnet in fireips file
cksb=$(grep "$ip2" < "$path/fireips")
# case something was found
if [[ ! -z "$cksb" ]]
then
# remove with sed lines containing that IP from cmlog
sed -i -e '/"$var1"/d' $cmlog
# inform user that ip subnet was found and was deleted
echo " - Existent Subnet : $ip2 for IP : $var1 - Cleaned"
fi
# objective of this next line is to check if the file #changed still have the initial number of lines , in case
#does not then update its value in the loop above
cntfr=$(wc -l "$cmlog" | awk '{print$1}')
doneAn example from cmlog can be found here
https://pastebin.com/apxg0DrB
What happens in this code is that grep finds a value but then sed ahead do not remove the lines directly in file .
There is no error in code that could give any clew .


Here it is the thing , i want to search and remove directly to the file using sed , but the loop sequence will be updated after sed deleted some lines .
Imagining that loop starts inicialy a 1 and supose to end at 100 , but then some pattern was found and sed have to delete 10 lines , then loop will end at new variable witch is 100 -10 = 90 , by this the initial variable in loop is updated , but this happens consequently in this loop because sed is removing if found what i am searching .
Now the problem is that i stay in a endless loop without change because sed is not replacing the original file with the new updates .
I already had created before a similar thread , but this one a very different .
The Code
Code:# count the lines of file to loop
cntfr=$(wc -l "$cmlog" | awk '{print$1}')
# start sequence until variable cntfr
for i in $(seq "$cntfr")
do
# read the line and grab the ip
# ip will be xxx.xxx.xxx.xxx:port , so this next code
# will remove everything after the : and will get only
# the cleaned ip
var1=$(sed -n ${i}p < $cmlog | awk '{print$11}' | cut -f1 -d":")
# this one i learned here , witch is put something like
# ex: 111.111.111.111 to 111.111.111.0/24
# this will be to see if subnet exists on a file ahead
ip2="${var1%.*}.0/24"
# fireips is a file containing all the blocked ips and
# subnets in the firewall
# search for the subnet in fireips file
cksb=$(grep "$ip2" < "$path/fireips")
# case something was found
if [[ ! -z "$cksb" ]]
then
# remove with sed lines containing that IP from cmlog
sed -i -e '/"$var1"/d' $cmlog
# inform user that ip subnet was found and was deleted
echo " - Existent Subnet : $ip2 for IP : $var1 - Cleaned"
fi
# objective of this next line is to check if the file #changed still have the initial number of lines , in case
#does not then update its value in the loop above
cntfr=$(wc -l "$cmlog" | awk '{print$1}')
doneAn example from cmlog can be found here
https://pastebin.com/apxg0DrB
What happens in this code is that grep finds a value but then sed ahead do not remove the lines directly in file .
There is no error in code that could give any clew .