Handle null values-awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Handle null values-awk
# 1  
Old 07-05-2013
Handle null values-awk


I am using below code to validate the source file,code working fine but if any column contains null value then below code throwing error actually it should not.how to customize the below code to handle null null values also.

When I run the script with below source data getting “date error”, as there is null value in first row its throwing error.

Example:
Code:
 
10ÇvivÇ456786ÇeceÇ
1ÇvivekÇ0861ÇCSEÇ2013-05-29 00:00:00
10ÇvivÇ4566ÇegdteÇ2013-05-10 00:00:00

Output:
date-err 10ÇvivÇ456786ÇeceÇ

awk -FÇ  '
       NR==1{next}
       {f="ok"}
       $1!~/^[0-9.]{1,2}$/{f="s-err"}
       $2!~/^[a-zA-Z0-9]{1,20}$/{f="n-err"}
       $3!~/^[0-9.]{1,10}$/{f="p-err"}
       $4!~/^[a-zA-Z0-9]{0,20}$/{f="de-err"}
       $5!~/^[0-9]{0,4}-[0-9]{0,2}-[0-9]{0,2} [0-9]{0,2}:[0-9]{0,2}:[0-9]{0,2}$/{f="date-err"}
       {print f,$0}
       ' $dummy_file

please suggest me how to customize the code.
Moderator's Comments:
Mod Comment use code tags where appropriate, not all the content!

Last edited by vbe; 07-05-2013 at 10:39 AM.. Reason: next time use code tags where appropriate, not all the content!
# 2  
Old 07-05-2013
It's not throwing an error when I run it with the records you provided. Not sure if it's something with the delimiter that it throwing it off (doesn't look like a normal character "c").

If I understand your question, you can add an AND operator to exclude the nulls like this:

Code:
&& $1!=""

For example:
Code:
$1!~/[^0-9.]{1,2}$/ && $1!="" {f="s-err"} {print f,$0} '

# 3  
Old 07-05-2013
Try to or in the empty string into the regex:
Code:
       $5 !~ /^$|^[0-9]...
                ^--- regex OR sign
              ^^--- empty string

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count null values in a file using awk

I have the following a.txt file A|1|2|3|4|5| A||2|3|0|| A|1|6||8|10| A|9|2|3|4|1| A|0|9|3|4|5| A||2|3|4|5| A|0|av|.9|4|9| I use the following command to count null values for 2nd field awk -F"|" '!$2 { N++; next } END {print N}' a.txt It should give the result 2, but it is giving... (2 Replies)
Discussion started by: RJG
2 Replies

2. Shell Programming and Scripting

Printing null values in awk

Hi, I have a csv file with given details abc.txt 123,ra,point,,there 232,ba,points,home,pheer I want to get those values and store them in different variables: Code: while read line do echo $line |awk -F"," '{print $1" "$2" "$3" "$4" "$5"}'|read dbt_acct val_dt crncy AMT... (11 Replies)
Discussion started by: rahulsk
11 Replies

3. Shell Programming and Scripting

Replace null values with dot using awk

Using awk I am trying to replace all blank or null values with a . in the tad delimited input. I hope the awk is close. Thank you :). input name test sam 1 liz 2 al 1 awk awk 'BEGIN{FS=OFS="\t"}{for(i=1;++i<NF;)$i=$i?$i:"."}1'input awk 'BEGIN { FS =... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

Replace null values in csv with zero

Hi, i have another issue: i have three files: FILE 1 ServiceEventHandler, Processed,Percentage 5285337,100% FILE 2 Wallet, Processed,Percentage 5285337,100% (1 Reply)
Discussion started by: reignangel2003
1 Replies

5. Shell Programming and Scripting

How to handle NULL value output from ISQL command?

I am using ISQL command in ksh script. Suppose if i get NULL value from the query which i run,how can i handle it? I am getting a NULL result set and the following error is coming. ############### output of isql command for getting the sum of JEs ################ ----------- NULL... (4 Replies)
Discussion started by: Sharma331
4 Replies

6. Shell Programming and Scripting

How to use sort with null values?

Hello everyone I am doing a join command. Obviously, before I need two files sorted first. ( Both files have headers and have about 2 million lines each one ) The problem is, one of the files has null values in the key to sort (which is the first filed ). For example I have the original... (4 Replies)
Discussion started by: viktor1985
4 Replies

7. Shell Programming and Scripting

sorting null values

Hi I have a file with the values abc res set kls lmn ops i want to sort this file with the null values at the bottom of the file OUTPUT should look like this abc kls lmn ops (6 Replies)
Discussion started by: vickyhere
6 Replies

8. UNIX for Advanced & Expert Users

How to Compare Null values??

Hi, Can someone help me comparing Null values. Scenario is as follows: I have a variable which "cache_prd" which can have either some integer or nothing(Null) if it is integer I have to again do some comparision but these comparisons give me this error:( "line 32: [: 95: unary operator... (3 Replies)
Discussion started by: Yagami
3 Replies

9. UNIX for Dummies Questions & Answers

Check for null values in a column

Hi All, I have a file with 10 columns and get the required data for nine columns properly except 8th. In 8th column i have both NULL and NON NULL values...i.e certain records have values for all the columns including 8th column and certain records have 8th column as NULL.My requisite is,without... (20 Replies)
Discussion started by: ganesh_248
20 Replies

10. Shell Programming and Scripting

comparing the null values in the unix

hi all, iam new to this forum.i have to submit the script EOD.so please help me. my requirement is to compare two null values..iam trying to compare two null values :One null value output of the storedprocedure and another iam giving spaces in script. it is giving the error... (11 Replies)
Discussion started by: bbc17484
11 Replies
Login or Register to Ask a Question