Basically, I have 2 files with IP address. For example 134.123.3.234 in the first file and 134.123.3.235 in the second. Now I want to create a file with a IP address with IP 134.123.3.236...(max 254). So i have to check files which IP address in previous two files and base on that to create a new file with the next IP address which is 134.123.3.236. I wanted to check last number of IP...If it is 254 then increment +1 to third number....
for ip in {0..254}
do
if "192.168.${ip}.${j}" > $file
echo "192.168.${ip}.${j}"> Newfile.txt
for j in {0..254}
do
if "192.168.${ip}.${j}">$file then
echo "192.168.${ip}.${j}"> Newfile1.txt
done
done
Basically, this code is not correct. Have no idea how to solve this problem. Hope you will help me....thanks.
ip=`head -n1 file.dat` # read ip address from file
end=${ip##*\.} # delete everthing before last dot (leaving 234/235)
while (( end++ < 254 )); do # while the 234/235 is less than 254 (increment each time tested)
printf 134.123.3.$end"\r\n" >> NewFile.txt # append your incremented ip address to NewFile.txt
done
Two nested loops will cycle through all combinations.
A break will exit the current loop.
#!/bin/bash
for ip in {0..254}
do
for j in {0..254}
do
if grep -qw "192\.168\.${ip}\.${j}" firstfile secondfile
then
: # exists
else
echo "192.168.${ip}.${j}" >Newfile1.txt
break 2 # exit this loop and the next loop
fi
done
done
Thats cool but when its 254 i need to increment third oct....but in code we deleted it lol how to make it happen?
---------- Post updated at 02:25 PM ---------- Previous update was at 02:09 PM ----------
I see.... in sort command we have 2 files... What about if i have a path to bunch of files??? /Users/unknown/files/* I put after sort but it does not work lol How i can put this path?
c=`head -n1 file | cut -d. -f3`
d=`head -n1 file | cut -d. -f4`
oct3max=4
while (( c <= oct3max )); do
while (( ++d <= 254 )); do
echo 134.123.$c.$d >> NewFile.txt
done; d=-1; ((c++));
done
Note - the appending in this code will take increasing longer as NewFile.txt grows. Increasing oct3max too high will w/o optimizing appending will take a long time to run.