Ceil not working as function in awk statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ceil not working as function in awk statement
# 1  
Old 12-02-2013
Ceil not working as function in awk statement

Hi,
I have the following code in which i am trying to find ceil of 10th & 11th fields. For finding ceil i have a function in the awk statement. When i test it for some values say on command line it gives correct response(say $10=0 & $11=750). But when the same value occurs in a file having more 3 millions records(it is just an example & behaviour is abrupt for some cases), the value gets impacted. Can you please suggest in case some modifications are required.

Code:
function pdsnrater()
 {
  awk -F"|" ' 
  function ceil(val) { return (val == int(val)) ? val : int(val)+1}
  { 
   if ($5 == "MB")
   printf("%s,%s,%s,%s,%s,%d,%s,%s,%s,%s,%s,%s,%d,%s,%s,%s\n",$1,$2,$3,$4,$6,ceil(($10+$11)/1024/1024),$8,$9,$10,$11,$12,$13,$14,$15,$16,$5) 
   else
   printf("%s,%s,%s,%s,%s,%d,%s,%s,%s,%s,%s,%s,%d,%s,%s,%s\n", $1,$2,$3,$4,$6,ceil(($10+$11)/1024/10),$8,$9,$10,$11,$12,$13,$14,$15,$16,$5)
  } ' FS="|" OFS="|" $1 > $1.tmp
  mv $1.tmp $1
}

# 2  
Old 12-02-2013
Without seeing what is in the file named by $1, the output you're trying get, and how that is different from the output you are getting, we have no way of guessing what is wrong with your code.
# 3  
Old 12-02-2013
Hi Don,
Following are some of the lines of the file passed as $1. Please look into this and suggest
Code:
5000044903|1|9152|295052|MB|20131201000001|10487450|N||0|10335910|151540|C28053OB|533|SSCHHA01S201312010005000000.PDSN|0
41619703|45|9152|093905|MB|20131201000346|9912683|N||0|8998792|913891|C2205Ohz|533|SSCHHA01S201312010005000000.PDSN|0
41619703|45|9152|093905|MB|20131201000346|569377|N||0|550139|19238|C2205Ohz|534|SSCHHA01S201312010005000000.PDSN|0
41619703|45|9152|093905|MB|20131201000346|479|N||0|0|479|C2205Ohz|336|SSCHHA01S201312010005000000.PDSN|0
41619703|45|9152|093905|MB|20131201000346|1107|N||0|909|198|C2205Ohz|335|SSCHHA01S201312010005000000.PDSN|0
41619703|45|9152|093905|MB|20131201000346|2161|N||0|1378|783|C2205Ohz|334|SSCHHA01S201312010005000000.PDSN|0
5000052620|1|9152|133596|MB|20131201000445|7688246|N||0|7308890|379356|C21055UW|533|SSCHHA01S201312010005000000.PDSN|0
5000052620|1|9152|133596|MB|20131201000445|2798798|N||0|2528150|270648|C21055UW|534|SSCHHA01S201312010005000000.PDSN|0
5000052620|1|9152|133596|MB|20131201000445|0|N||0|0|0|C21055UW|334|SSCHHA01S201312010005000000.PDSN|0
41619703|45|9152|093905|MB|20131201000846|5270324|N||0|4870952|399372|C2205OiX|533|SSCHHA01S201312010010000001.PDSN|0

# 4  
Old 12-02-2013
I repeat: "Without seeing what is in the file named by $1, the output you're trying get, and how that is different from the output you are getting, we have no way of guessing what is wrong with your code."
# 5  
Old 12-02-2013
Hi Siramitsharma,

It would be good if you can provide us desire output (based on input which you have provided in earlier post).

Thanks
Pravin
# 6  
Old 12-02-2013
Saw my code again...& found the bug Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk call in bash function called with arugments not working, something lost in translation?

Hello, I have this awk code in a bash script to perform a find and replace task. This finds one unique line in a file and substitutes the found line with a replacement. #! /bin/bash # value determined elsewhere total_outputs_p1=100 # file being modified... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

3. Programming

A single statement without main function in c

A sample.c file is written with only one single statement. main; Segmentation fault occurred when executed that file. Any statement other than main; is written, for example unix; then it won't compile. why is this behaviour ! (2 Replies)
Discussion started by: techmonk
2 Replies

4. Shell Programming and Scripting

Running a function from a case statement

Hi, I have the below script that should take the command line option and run the desired script on another server. Only it doesn't seem to run the function, infact it just returns back to the command line. case $1 in 1) msgbacklog() ;; 2) jobstatus() ;; ... (10 Replies)
Discussion started by: chris01010
10 Replies

5. Shell Programming and Scripting

Embed function in if statement

Hey everyone, I am just trying to figure out how to embed a function in an if statement. I have the following test script so far: PRIMARY=192.168.1.2 SECONDARY=192.168.1.1 function checkAlive { ping -c 1 -q $1 } if then echo "equaled 0" fi This... (1 Reply)
Discussion started by: msarro
1 Replies

6. Shell Programming and Scripting

Calling function in awk statement.

Hi All, I have an awk statement and a function defined in a script. I am trying to call the function from inside awk statement, i.e. awk ' myFunk () ;' filename But when I define myFunk() before awk, then I receive this error: s2.sh: line 48: syntax error: unexpected end of file and... (5 Replies)
Discussion started by: morningSunshine
5 Replies

7. Shell Programming and Scripting

If statement is not working.

Hi. With the help of this group I have created a shell script to find the factorial of a number. OK. Then I got wild.;) I tried to put in a check to make sure the entry is a number. read num If )) then echo "This is not a valid number. Try again." fi while (( $var <= $num)) more... (5 Replies)
Discussion started by: Ccccc
5 Replies

8. Shell Programming and Scripting

read statement not working in a function

Pls this is emergency.I have written a script which is taking input from another script. and the contents of my second script are acting as functions to my main script.Now the problem is that in one of the functions i want the script ececution to stop and start when user enters any character r... (2 Replies)
Discussion started by: sumitdua
2 Replies

9. Shell Programming and Scripting

Using Subroutine / Function in case statement

I have issue running functions under case statement #!/bin/bash single() { Commands } multiple() { Commands } until ; do echo -e " \t \t M A I N - M E N U Perforce delete script \n" (1 Reply)
Discussion started by: sriram003
1 Replies

10. Shell Programming and Scripting

If statement not working

I understand this question probably poses some child like stupidity, but I can't get this if statement to work for love or money. #!/bin/ksh echo "Input either 1 or 2" read Num if ; then echo "Message 1" if ; then echo "Message 2" else echo "false" fi $ ksh decisions Input either 1... (6 Replies)
Discussion started by: Hazmeister
6 Replies
Login or Register to Ask a Question