Compare IP addresses and increment

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
1 Like

Thank for replying. can you explain what it does....New here, sorry... (Tried to compile but still not sure what some lines do)

-- delete

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

Not quite sure I understand your request. Start at the highest IP in your two files, and create 254 IP addresses from there? Try:

sort file1 file2 | tail -1 | { IFS="." read A B C D; for J in {1..25}; do [ $((++D)) -eq 255 ] && { ((C++)); D=0; }; echo $A.$B.$C.$D; done }
134.123.3.236
134.123.3.237
134.123.3.238
134.123.3.239
134.123.3.240
134.123.3.241
134.123.3.242
134.123.3.243
134.123.3.244
134.123.3.245
134.123.3.246
134.123.3.247
134.123.3.248
134.123.3.249
134.123.3.250
134.123.3.251
134.123.3.252
134.123.3.253
134.123.3.254
134.123.4.0
134.123.4.1
134.123.4.2
134.123.4.3
134.123.4.4
134.123.4.5
etc...

Thats cool but when its 254 i need to increment third oct....but in code we deleted it lol how to make it happen?:wink:

---------- 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? :wink:

you can use cat

cat /path_to_file/*.type | sort ........so on
1 Like
set -x
sort file*
+ sort file file~ file1 file1~ file2 file2~ file3 file3~ file4

sort takes any number of files to work upon (up to certain system limits). So, if unquoted, /Users/unknown/files/* should work.

1 Like

Thank u RudiC:b:

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.