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
# 15  
Old 09-29-2009
Glad to hear that you solved it yourself. May I suggest some optimization for your code?

Parsing fixed length records is easy with GNU awk. Working on the sample file you gave above:
Code:
0000000200aaaaaa aaaaaaaa aaaaaaaaa 00/00/00 00:00:00000000 1 1111111 2222.22 2222.22 00

With GNU awk, it is as easy as this:
Code:
BEGIN{
    FIELDWIDTHS="1 1 1 5 2 26 17 7 2 8 8 7 1 2 2"
}
{
    for (i=1;i<=NF;i++){
        printf "Field%d:: %s\n", i, $i
    }
}

With original awk or nawk, you need to create your own parse function:
Code:
function mysplit(str, width){
    str=substr(str, offset, width)
    offset+=width
    return str
}

BEGIN{
    offset=1
    fieldwidths="1_1_1_5_2_26_17_7_2_8_8_7_1_2"
    n=split(fieldwidths, a, "_")
}
{
    for (i=1;i<=n;i++){
        printf "Field%d:: %s\n", i, mysplit($0,a[i])
    }
    offset=1
}

Both snippets will yield:
Code:
Field1:: 0
Field2:: 0
Field3:: 0
Field4:: 00002
Field5:: 00
Field6:: aaaaaa aaaaaaaa aaaaaaaaa 
Field7:: 00/00/00 00:00:00
Field8:: 000000 
Field9:: 1 
Field10:: 1111111 
Field11:: 2222.22 
Field12:: 2222.22
Field13::  
Field14:: 00

When you define the fieldwidths values, don't forget to include the spaces.
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