how to validate a field when it is blank using awk prog


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to validate a field when it is blank using awk prog
# 8  
Old 09-23-2009
Without a sample input file and desired output, it will be difficult to help you.
# 9  
Old 09-23-2009
You could try using the length function

Code:
if (length(f10) == 0){
        print "Field 10 is Correct";}
else{
        print "Field 10 is Wrong";}

# 10  
Old 09-24-2009
its still not working....

I have written the below piece of code for reading a input file which is a fixed length file...

Code:
ls sample.txt | awk '

Code:
  { a[NR]=$1 }
  END{
  FS="n"
  for(i=1;i<=NR;i++)
     {
            while( getline < a[i] ) 
                        {
                                       f1=$0;
                                       print("Line::",f1);  
                        f2=substr(f1,1,1)     
                        print("Field1::",f2);
                        f3=substr(f1,2,1)
                        print("Field2::",f3);
                        f4=substr(f1,3,6)
                        print("Field4::",f4);
                        f5=substr(f1,9,2)     
                        print("Field5::",f5);
                        f6=substr(f1,11,58)
                        print("Field6::",f6);
                        f7=substr(f1,69,17)     
                        print("Field7::",f7);
                        f8=substr(f1,86,11)
                        print("Field8::",f8);
                        f9=substr(f1,97,1)     
                        print("Field9::",f9);
                        f10=substr(f1,98,7)
                        print("Field10::",f10);
                        if(length(f10) == 0){
                        print "Field 10 is Correct";}
                        else{
                         print "Field 10 is Wrong";}
                        f11=substr(f1,105,21)     
                        print("Field11::",f11);
                        f12=substr(f1,126,15)
                        print("Field12::",f12);  
                        f13=substr(f1,141,15)
                        print("Field12::",f13);         
                        f14=substr(f1,156,2)
                        print("Field13::",f14); 
                        if(f14 ~ /^["][ ]+["]$/){
                         print "Field 13 is Correct"}
                        else{
                         print "Field 13 is Wrong"}
                        f15=substr(f1,158,2)
                        print("Field14::",f15); 
                                    } 
              }
     }'



Quote:
sample.txt--->name of the input file
Quote:
content of the file will be specified in the below format
0000000200aaaaaa aaaaaaaa aaaaaaaaa 00/00/00 00:00:00000000 1 1111111 2222.22 2222.22 00


the output for the above input is
Quote:
Line:: 0000000000aaaaaa aaaaaaaa aaaaaaaaa 00/00/00 00:00:00000000 1 1111111 2222.22 2222.22 00
Quote:

Field1:: 0
Field2:: 0
Field4:: 00000
Field5:: 00
Field6:: aaaaaa aaaaaaaa aaaaaaaaa
Field7:: 00/00/00 00:00:00
Field8:: 000000
Field9:: 1
Field10::
Field 10 is Wrong
Field11:: 111111
Field12:: 2222.22
Field12:: 2222.22
Field13::
Field 13 is Wrong
Field14:: 00


But instead of displaying it as Correct it prints wrong.

Please help me on knowing what went wrong in the above piece of code
# 11  
Old 09-24-2009
If I understand, you are expecting a space in field 10 and 13 right?

If your check fails it is probably because there are more spaces than you expect. Try printing them with delimiters to check the output:

Code:
printf "Field10::>%s<", f10;



---------- Post updated at 11:09 AM ---------- Previous update was at 11:00 AM ----------

I can already see your problem:
Code:
f10=substr(f1,98,7)
print("Field10::",f10);
if(f10 ~ /^[[:space:]]$/){
	print "Field 10 is Correct"}
else{
	print "Field 10 is Wrong"
}

For f10 you extract 7 characters from f1. The next test will never match on single space.
# 12  
Old 09-24-2009
we cannot add any delimiter to that file for printing the field values and it is a fixed length file

here what we expect is that we have 7 characters which is expected to be blank even if one of them is not then we need to have get an error message.

Please help us in this.

Thanks in advance
meva
# 13  
Old 09-25-2009
Quote:
Originally Posted by meva
we cannot add any delimiter to that file for printing the field values and it is a fixed length file

here what we expect is that we have 7 characters which is expected to be blank even if one of them is not then we need to have get an error message
That's what I thought. You are trying to match a string with 7 space characters against the pattern ^[[::space::]]$ which means exactly *one* space character.

Try the pattern ^[[::space::]]+$.

As for the delimiter, my suggestion is only a testing trick to visualize the length of a string of spaces.

What version of awk are you using? For fixed length data, GNU awk as the FIELDWIDTHS built-in variable that facilitate the parsing. For other versions of awk there is a workaround. Let me know.

---------- Post updated at 09:00 AM ---------- Previous update was at 08:51 AM ----------

Also, there is a inconsistency in your data for field 10 and 13. Your substr() function extracts respectively 7 and 15 characters that are supposed to be spaces but the sample you provides only shows one single space at that place. Please check.
# 14  
Old 09-29-2009
Hi Ripat,

Thanks for your help.

I got the solution when i tried to check the space of the field with the below pattern
Quote:
/^\ *$/
Meva
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a blank field and replace values to NA

Hi All, i have a file like col1 col2 col3 13 24 NA 12 13 14 11 12 13 14 22 NA 18 26 NA in this file if i found "NA" other values in the line are also replace by NA Could you help me! (7 Replies)
Discussion started by: Shenbaga.d
7 Replies

2. Shell Programming and Scripting

Find a blank field

Find a blank field Hi I have set of fields that have some blank values, how to find that and get its line noumbers in output file. Ex: Col1 col2 col3 11 ss 103 12 104 13 105 14 se 106 (2 Replies)
Discussion started by: Shenbaga.d
2 Replies

3. Shell Programming and Scripting

Adding an additional blank field to a file

Hi, I have the following file, I'd like to add an additional blank field to this file This is a tab delimited file, I have tried the same thing on excel, but looking for a unix solution. Here is my input: Country Postal Admin4 StreetBaseName StreetType HUN 2243 Kóka Dózsa György ... (3 Replies)
Discussion started by: ramky79
3 Replies

4. Shell Programming and Scripting

awk - remove row if specific field is empty/blank

I have this text.filecharles darwin sam delight george washington johnson culper darwin sam delight micheal jackson penny lite and would like to remove the row, if the first field is blank. so the result would be: result.filecharles darwin sam ... (4 Replies)
Discussion started by: charles33
4 Replies

5. Shell Programming and Scripting

Help with removal of blank spaces from the second field!

Hi everyone.. I'm trying to eliminate multiple whitespaces from a file.. I must make use of shell script to eliminate whitespaces.. Take a look at the sample file 1 int main() 2 { 3 int a,b; 4 printf("Enter the values of a and b"); 5 scanf("%d%d",&a,&b); 6 if(a>b) ... (6 Replies)
Discussion started by: abk07
6 Replies

6. Shell Programming and Scripting

Find and replace blank in the last field

Hi all, I have a huge file and I need to get ride of the fields 6-11 and replace the blanks in field 5 with a missing value(99999). 159,93848,5354,343,67898,45,677,5443,434,5545,45 677,45545,3522,244, 554,54344,3342,456, 344,43443,2344,444,23477... (12 Replies)
Discussion started by: GoldenFire
12 Replies

7. Shell Programming and Scripting

how to fix the column length in a file using Awk Prog

Hi I use the following code to read the file and to fix the length of the column of the record in the file 'Sample.txt' ls Samp* | awk ' { a=$1 } END{ FS="n" for(i=1;i<=NR;i++) { while( getline < a ) { f1=$0; print("Line::",f1); f2=substr(f1,1,10) print("Field1::",f2);... (10 Replies)
Discussion started by: meva
10 Replies

8. Shell Programming and Scripting

validate each field in txt

Hello, I have a file with a lot of record like below: 00001,CUSTR,CUSTOMER ADDRESS,02310,N,0:00,0,0,0,0,0,0,0,0,0,0,0,0:00,0,0,0,0,0,CSH,ACC Can I validate each record in the file and output the incorrect result? field 1 - customer number, should be "5 digit. field 2 - should be 5... (9 Replies)
Discussion started by: happyv
9 Replies

9. Shell Programming and Scripting

fill a NIL into the blank field

Hello, I have a record which split with "," I would like to check..if the field is empty and it will field "NIL" into the field. input 45111,40404,peter,,0303403,0,030304,john,,9,0, output 45111,40404,peter,NIL,0303403,0,030304,john,NIL,9,0, (8 Replies)
Discussion started by: happyv
8 Replies

10. Shell Programming and Scripting

awk: How to check if field is blank?

In awk, I'd like to check if a field is blank. And by blank I mean, the field could be "" or " " In other words, the field could either be empty, or be filled with spaces. Would the regex look like this? $5 ~ // { Action }? What other ways are there? Hmm.. in any case I think... (7 Replies)
Discussion started by: yongho
7 Replies
Login or Register to Ask a Question