awk script to check and throw error for multiplication result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script to check and throw error for multiplication result
# 8  
Old 05-10-2018
Hello as7951,

Could you please try following and let me know if this helps you.
Code:
awk -F"|" 'FNR==1{print $1,$2,$5;next} (($5-($1*$3))>=-2) && ((($1*$3)-$5)<=2){next} {print $0 " is bad, line number " FNR}' OFS="||"  Input_file

Adding a non-one liner form of solution too now.
Code:
awk -F"|" '
FNR==1                                    {   print $1,$2,$5;   next               }
(($5-($1*$3))>=-2) && ((($1*$3)-$5)<=2)   {   next                                 }
                                          {   print $0 " is bad, line number " FNR }
' OFS="||"    Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 05-10-2018 at 09:24 AM..
# 9  
Old 05-10-2018
Hi RavinderSingh13,

Your code only returning below header

Code:
a||b||e

# 10  
Old 05-10-2018
Quote:
Originally Posted by as7951
Hi RavinderSingh13,
Your code only returning below header
Code:
a||b||e

Hello as7951,

Could you please check once if your Input_file is having carriage characters by doing.
Code:
cat -v  Input_file

In case it is there then try removing them by doing as follows.
Code:
tr -d '\r' < Input_file  > temp_file  &&  mv temp_file  Input_file

Once they are removed then try my previous post's code and let us know then.

Thanks,
R. Singh
# 11  
Old 05-10-2018
Hi RavinderSingh13,

Your code is working, but not showing error for last 2 rows

How to handle if a file has rows in thousands or in lakhs

Input file

Code:
a|b|c|d|e
2.4||3.5||6.6
5||50||249
6.55||3456||22635.7
2.4||3.5||6.3
5||50||247
6.55||3456||22634.4
2.4||3.5||9.1
5||50||256
6.55||3456||22638.9

Getting below output

Code:
a|b|c|d|e
2.4||3.5||6.3 is bad, line number 5
5||50||247 is bad, line number 6
6.55||3456||22634.4 is bad, line number 7

# 12  
Old 05-10-2018
Quote:
Originally Posted by as7951
Hi Rudic,

For the below row, multiplication result of $1 * $3 is equal to $5, so i should not get an error message, but im getting with your code
Difficult to believe; this is what I get:
Code:
Error 401 : column1 and colmn3 does not match with column5,Field position 5, Linenumber:1 a|b|c|d|e

Correct, isn't it?

Quote:
Error should display only for the bad records rows with header in another file , those have multiplication result deviation of more than +/- 2.
Code:
a|b|c|d|e
2.4||3.5||8.4

Also i am using below code(.awk) to do the same thing with below input file

Code:
BEGIN { FS="|" }
NR>1 { print $0 "\tis " (abs($5-($1*$3)) > 2 ? "bad" : "good") }
function abs(val) { return (val<0 ? -val : val) }

Executing as awk -f tst.awk file

Input

Code:
a|b|c|d|e
2.4||3.5||6.6
5||50||249
6.55||3456||22635.7
2.4||3.5||6.3
5|50|247
6.55||3456||22634.4

could you please help.
So with the above code i want to print only bad records in another file along with header and row number and not the both good and bad records.
Expected output

Code:
a||b||e
2.4||3.5||6.3   is bad, line number 1
5|50|247        is bad, line number 2
6.55||3456||22634.4     is bad, line number 3

Try
Code:
awk '
BEGIN   {FS="|"
        }
function abs(val)       {return (val<0 ? -val : val)
                        }
NR == 1
abs($5-$1*$3) > 2 &&
NR > 1  {print $0 "\tis bad"
        }
' file
a|b|c|d|e
2.4||3.5||6.3    is bad
5|50|247    is bad
6.55||3456||22634.4    is bad

# 13  
Old 05-10-2018
Quote:
Originally Posted by as7951
Hi RavinderSingh13,
Your code is working, but not working after 3 rows,
only showing output for 3 rows.
How to handle if a file has rows in thousands or in lakhs
Input file
Code:
a|b|c|d|e
2.4||3.5||6.6
5||50||249
6.55||3456||22635.7
2.4||3.5||6.3
5||50||247
6.55||3456||22634.4
2.4||3.5||9.1
5||50||256
6.55||3456||22638.9

Getting below output
Code:
a|b|c|d|e
2.4||3.5||6.3 is bad, line number 5
5||50||247 is bad, line number 6
6.55||3456||22634.4 is bad, line number 7

Hello as7951,

That's obvious because NONE of them I believe are satisfying the conditions(since their difference is in range of +2 to -2, please check and let us know on same. Kindly check once and get back to us.

NOTE: Also try to encourage people by using THANKS button at the left most corner for their efforts.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 14  
Old 05-10-2018
Hi RavinderSingh13,

Now getting required output with below
Code:
awk -F"|" 'FNR==1{print $0;next}(($5-($1*$3))>=-2) && (($5-($1*$3))<=2){next} {print $0 " is bad, line number " FNR}' OFS="|" a1.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Percentage / Multiplication in Shell Script

Hello, I am trying to compute the percentage in a script as shown below: PerCover=`echo "scale=2 ; 100 \* ($InputCover/$Total)" | bc` However the PerCover value is blank/null. What do I need to do differently? Thanks for your input! ~Guss (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

2. Shell Programming and Scripting

Multiplication of a column from 2 files using awk

Hi, I have two files (file1.txt, file2.txt) in which, I would like to multiply all the values in file1 with the first row value of file2, file1 * second row value of file2, file1 * third row value of file2 and so on. Below are my sample data. file1.txt file2.txt ... (6 Replies)
Discussion started by: ida1215
6 Replies

3. Shell Programming and Scripting

search line with more than two dots if so throw error

Hi, I have a requirement like i have to search a script and find names that conatins more than two dots, if so then throw error. For ex: a1.b1.comname here i have to find comname and check two dots. it will not throw error a1.b1.c1.comname here it contains more than 2dots it will throw... (3 Replies)
Discussion started by: swagat123
3 Replies

4. Shell Programming and Scripting

Error with "multiplication table" in shell script

#!/bin/sh echo Enter the multiplication number required: read number for i in 1 2 3 4 5 6 7 8 9 10 do echo "$number * $i = expr $number \* $i" done I am not getting the output for this multiplication table. (4 Replies)
Discussion started by: vinodpaw
4 Replies

5. Shell Programming and Scripting

Find Command in the script throw error

Hi I have script that is developed to serch for 30 days old Directory & Files and then remove them ... when i run it successfully removes the Directory & files & but it throw errors on the screen .. .. + find . -type f -mtime +30 -exec rm -f {} ; + exit please help me ?? I... (0 Replies)
Discussion started by: Beginner123
0 Replies

6. Shell Programming and Scripting

program for multiplication in shell script

Hi, I wanted to write a schell program that fetches the values from a file and prints the output as its onerall multiplication. for example I have a file named abc. it has values 2, 3, 4 now my program should give me 2*3*4 ie 24. note:this file abc can have any numbers. so experts,... (9 Replies)
Discussion started by: sandeep.krish
9 Replies

7. UNIX for Dummies Questions & Answers

if a file is empty throw an error

I want to count the number of lines in a file and store it in a variable if this count is zero i hv to throw an error ...is this syntax correct , but i am not getting the desired result I am not using -s option here as i am concerned about record count not the size #!/bin/ksh set $count1... (4 Replies)
Discussion started by: mavesum
4 Replies

8. Shell Programming and Scripting

using awk multiplication

Suppose i have a file A 1*2*3*4 2*4*4*22 and second file B 2*3*4*5 4*4*6*7 By multiplying file A by file B that is file A by first column in file B respectively output shud be 2*6*12*20 8*16*24*154 my code is =$1 next } {for (f=1;f<=NF;f++) (2 Replies)
Discussion started by: cdfd123
2 Replies

9. Shell Programming and Scripting

throw a generic message upon error

hi all, in ksh, is there a way to throw a generic error message. i have lots of commands in my script and i didnt want to put if ; then doStuff(); else print "an error occured, please run script again"; fi around all the commands used. is there a way detect a command has... (1 Reply)
Discussion started by: cesarNZ
1 Replies

10. UNIX for Dummies Questions & Answers

multiplication shows syntax error

my shell script is simple arithmetic process.my addtion,substract and divide all can be worked only except multiplication.please help me.the shell is following: #!/usr/bin/sh echo "enter a number1:" read number1 echo "enter an operator:" read operator echo "enter a number2:" read number2... (4 Replies)
Discussion started by: sonicstage
4 Replies
Login or Register to Ask a Question