Visit Our UNIX and Linux User Community


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
# 1  
Old 09-19-2009
how to validate a field when it is blank using awk prog

Hi,

I tried the below piece of code for my script to check whether it has a blank space for a particular field.

Code:
 
if(f10==/[[:blank:]]/){
print "Field 10 is Correct";}
else{
print "Field 10 is Wrong"; }

Quote:
But when field10 holds a value
Output should be :" Field10 is Wrong"
but the Actual Output : "Field10 is correct"
Please help me to know whether the "if" condition applied here is correct or do i need to specify it differently for checking blank space.

Thanks In Advance
Meva.
# 2  
Old 09-19-2009
Assuming the field separator is comma...
Code:
 
awk -F"," '{if($10==""){
  print "Field 10 is Correct";}
  else{
  print "Field 10 is Wrong"; }}' infile

# 3  
Old 09-19-2009
Actually the column values are stored in a variable like f1,f2....f10... and so on...

So now how do i check for the blank space stored in this variable?

I tried the below specified piece of code for a particular field

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

but it dint work? it gave me the wrong results for my validation....is that i have used it wrongly or is there any other option?

Please help me with ur inputs

Thanks In Advance
Meva
# 4  
Old 09-19-2009
Hi,

If you want to use regex the comparison operator i the tidle:
Code:
awk 'BEGIN{f10=" ";if(f10 ~ /^[[:space:]]$/) {print "Space"}else{print "not Space"}}'

# 5  
Old 09-19-2009
hi..

let f10=""

if [[ -z $f10 ]]
then
echo blank
else
echo non-blank
fi

this will work...
# 6  
Old 09-19-2009
I think the OP wants a awk solution.
# 7  
Old 09-23-2009
Yes you are right!!! awk output is required...

but when i tried with the below piece of code

Code:
 ls sample.txt | awk '
  { 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(f10 ~ /^[[:space:]]$/){
                        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 ~ /^[[:space:]]$/){
                         print "Field 13 is Correct"}
                        else{
                         print "Field 13 is Wrong"}
                        f15=substr(f1,158,2)
                        print("Field14::",f15); 
			} 
              }
     }'

It always gave me the output as
Quote:
Field 10 is Wrong
Field 13 is Wrong
...even when it is supposed to display Field 10/13 is Correct.

what might be the problem with my if condition...please help me on this....

Previous Thread | Next Thread
Test Your Knowledge in Computers #307
Difficulty: Easy
The common software-programming acronym 18N comes from the term Interlocalization;.
True or False?

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

Featured Tech Videos