Output, working with files. What is wrong?

I cannot figure out what is wrong.... I have 3 files with IP addresses:
file1 134.123.3.236
file2 134.123.3.235
file3 134.123.5.237
I type "prob1 Oops x2x3x4". Then my code creates file with name Oops and first line x2x3x4. Moreover, my code generate IP and it gives to file Oops as a second line. In addition, my code looks at all files with IP addresses compare and give next own IP. For example, the maximum according to third and fourth oct is file3 with his IP! So I need to create Oops file with 134.123.5.238 (254 is a max ) as a second line.

Everything works but my second line gives me wrong IP. Help me please ;( thanks

for i in $1 $2
   do

my_variable=$(awk -F '[.]' '
$3 > m3 {m3 = $3; m4 = $4; next}
$3 == m3 && $4 > m4 {m4 = $4; m4=$4 + 1 }
END { if(m4 == 254) {m4=0; m3++}

{print  "134.123."m3"."m4
}

} ' /Users/Unknown/hosts/*
)

echo "$2" "\n" "$my_variable" > /Users/Unknown/hosts/$1.txt

done

I think you posted same as in Stdout in file

please show clearly your input and expected output so that it will be helpful for users to answer, and as far as I know unix.com is the best forum where you definitely get solution soon if your post/requirement is clear.

Hi,
In red, you assign a value to 2 times m4, it's normal ?

Regards.

Lol I know what you mean. But I just wanted to increment m4 after finding the maximum m4 and output it in file. Sorry, I just started to learn... I deleted m4 = $4 +1, but I dont know how to increment it.... Moreover, smth wrong with my code. It gives me wrong answer...

---------- Post updated at 01:44 PM ---------- Previous update was at 01:41 PM ----------

file1 134.123.3.236
file2 134.123.3.235
file3 134.123.5.237

Input: "prob1 Oops x2x3x4"
Output: cat Oops
x2x3x4
134.123.5.238

Try

$ cat ip1
134.123.3.236
134.123.3.235
134.123.5.237
134.123.5.254
$ awk -F'.' '{m3=($3>m3)?$3:m3;m4=($4>m4)?$4:m4}END{print (m4>=254)?134 FS 123 FS m3+1 FS m4*0:134 FS 123 FS m3 FS m4+1}' ip1
134.123.6.0
$ cat ip2
134.123.3.236
134.123.3.235
$ awk -F'.' '{m3=($3>m3)?$3:m3;m4=($4>m4)?$4:m4}END{print (m4>=254)?134 FS 123 FS m3+1 FS m4*0:134 FS 123 FS m3 FS m4+1}' ip2
134.123.3.237

Lol wow is it possible to make it without awk utility? just using for looops and if statements? Totally confused with awk(

Dear I too confused :slight_smile:

if you want to use it as variable do like this

my_new_ip_address=$( awk -F'.' '{m3=($3>m3)?$3:m3;m4=($4>m4)?$4:m4}END{print (m4>=254)?134 FS 123 FS m3+1 FS m4*0:134 FS 123 FS m3 FS m4+1}'  filename) 

I think you should follow RudiC answer he already given solution

1 Like

lol Im so sorry guys... Confused u... Got it thank you so much...

Don't worry...it happens while learning..