Output, working with files. What is wrong?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output, working with files. What is wrong?
# 1  
Old 10-06-2013
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
Code:
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

# 2  
Old 10-06-2013
I think you posted same as in https://www.unix.com/shell-programmin...dout-file.html

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.

Last edited by Akshay Hegde; 10-06-2013 at 02:36 PM..
# 3  
Old 10-06-2013
Hi,
In red, you assign a value to 2 times m4, it's normal ?

Quote:
Originally Posted by Manu1234567
Code:
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

Regards.
# 4  
Old 10-06-2013
Quote:
Originally Posted by disedorgue
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 ----------

Quote:
Originally Posted by Akshay Hegde
I think you posted same as in https://www.unix.com/shell-programmin...dout-file.html

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.
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
# 5  
Old 10-06-2013
Try

Code:
$ cat ip1
134.123.3.236
134.123.3.235
134.123.5.237
134.123.5.254

Code:
$ 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

Code:
$ cat ip2
134.123.3.236
134.123.3.235

Code:
$ 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

# 6  
Old 10-06-2013
Quote:
Originally Posted by Akshay Hegde
Try

Code:
$ cat ip1
134.123.3.236
134.123.3.235
134.123.5.237
134.123.5.254

Code:
$ 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

Code:
$ cat ip2
134.123.3.236
134.123.3.235

Code:
$ 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(
# 7  
Old 10-06-2013
Dear I too confused Smilie

if you want to use it as variable do like this

Code:
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

https://www.unix.com/shell-programmin...increment.html
This User Gave Thanks to Akshay Hegde For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Wrong output when writing to file

Hello, I am having problem while redirecting output to a file where as on console output is proper. for dir in */; do printf "%s, " "$dir"; ls -m "$dir"; echo; done > output.txt Output of above command is coming in single line but when i am redirecting output to a file, single line i... (10 Replies)
Discussion started by: Manoj Rajput
10 Replies

2. Shell Programming and Scripting

Script Output coming in wrong format....

Hi team, getting output logs wrong in different format from telnet script ... getting Output.txt macro_outdoor_dist-6.0.0(v4_0_2) DN:1.3.903 (1101:100:11w:500:3:2:103:aa) macro_outdoor_dist-8.1.0(v3_1_0) DN:1.3.409 (N/A)... (3 Replies)
Discussion started by: Ganesh Mankar
3 Replies

3. Shell Programming and Scripting

Wrong output when run via Cron

Hello, This may be a simple one, but i can't see what the issue is. When i run the script via CLI, then i get the correct output via the if statement, but when i run via CRON i get the wrong statement. echo " Checking Job Status" >> $DIR/Bpimagecleanup_$DATE... (3 Replies)
Discussion started by: Junes
3 Replies

4. Shell Programming and Scripting

Wrong output in find command

Hi guys - I am trying a small script to tell me if there is a file that exists less than 1k. It should report ERROR, otherwise the check is good. I wrote this script down, however it never runs in the if/then statement. It always returns the echo ERROR. MYSIZE=$(find /home/student/dir1... (8 Replies)
Discussion started by: DallasT
8 Replies

5. Shell Programming and Scripting

Perl...getting wrong output?

Good morning! Im trying to write and learn at the same time a simple script. fTHe script should tell me if a number is odd or even/ #!/usr/bin/perl $num = 10; $string1 = "This number is odd"; $string2 = "This number is even"; if ($num /= 2) { print "$string1\n"; }else{ ... (3 Replies)
Discussion started by: bigben1220
3 Replies

6. Emergency UNIX and Linux Support

getting wrong output with AWK command!!!

i have a file which gets appended with 9 records daily and the file keeps growing from then...i use to store the previous day files count in a variable called oldfilecount and current files count as newfilecount.my requirement is that i need to start processing only the new records from the... (3 Replies)
Discussion started by: ganesh_248
3 Replies

7. Shell Programming and Scripting

wrong output in perl script

Hi, Here is my piece of code-- #!/usr/bin/perl my $Time_Stamp ; my $User_Name; my $Success; my $Failure; my $ErrorCode; my $ErrorMsg; my $logDir = $ARGV; my $logPrefix = $ARGV; die "usage: $0 <logDir> <logPrefix>" unless $logDir and $logPrefix; die "Log dir $logDir doesn't... (2 Replies)
Discussion started by: namishtiwari
2 Replies

8. Shell Programming and Scripting

tr command giving wrong output

Hi All, i have a file which have many fields delimited by ,(comma) now i have to show only few fields and not all. the sample text file looks like this: TYPE=SERVICEEVENT, TIMESTAMP=05/06/2009 11:01:40 PM, HOST=sppwa634, APPLICATION=ASComp, FUNCTION=LimitsService, SOU... (8 Replies)
Discussion started by: usha rao
8 Replies

9. Shell Programming and Scripting

Sort command giving wrong output

Hi all, I have a problem with sort command. i have a file which looks like this: "file1 1073 java/4 1073 java/180 1073 java/170 1073 java/176 1073 java/167 1073 java/40 1073 java/33 1073 java/136 28988 java/76 28988 java/73 28988 java/48 28988 java/26" and i want to sort... (8 Replies)
Discussion started by: usha rao
8 Replies

10. AIX

Printing to a Savin - Size of output wrong

Hello, I am trying to print reports from our aix system to a Savin Printer. The trouble is that the reports come out enlarged on it. The same report comes out fine though on a regular laser printer or pick ticket printer. Windows prints fine to the savin, Its just from our AIX system and... (2 Replies)
Discussion started by: h1timmboy
2 Replies
Login or Register to Ask a Question