dynamically change awk Field Separator FS

Hi All,

I was wondering if anyone knew how to dynamically change the FS in awk to accept vairiable containing a field separator. the current code is as below and does not work when i introduce the dynamic FS change :frowning:

validate_source_file()
{
source_file=$1
datetimestamp=`date +"%Y%m%d%H%M%S"`
invalid_file=$2_$datetimestamp.dat
field_count=$3
file_delimeter=$4

 \#awk 'BEGIN\{ FS="|"; \} \{ 
 awk 'BEGIN\{ FS=file_delimeter; \} \{ 
    if \( NF != field_count \) \{
        invalid\_rec_count\+=1 ;
        \#
        \# print the header for the bad file once....then start printing the invalid records to the same file...
        \#
        print $0 > invalid_file ;
        
    \}    
    else \{
        valid\_rec_count\+=1 ;          
    \}
 \} 
 END \{ printf "%-10s\\t\\n%-10s\\t\\n%-10s\\t\\n","Total Lines Read ["NR"]","Valid Records Found ["valid\_rec_count"]","Invalid Records Found ["invalid\_rec_count"]" ; 
       
       if \(invalid\_rec_count>1\) \{ 
          print " "
          print "Please refer to file "invalid_file" for full invalid record set." 
       \}
 \}
 ' invalid\_file="$invalid_file" field\_count="$field_count" file\_delimiter="$file_delimeter" $source_file

}

so when I call it i would want to pass a 4th parameter like:

validate_source_file "$filenamedir" "$source_tabin_name" "$field_count" ","

or

validate_source_file "$filenamedir" "$source_tabin_name" "$field_count" "|"

           Any ideas appreciated!

Regards
Satnam

You have to pass variables into awk with -v

awk -v fdelim=$file_delimeter 'BEGIN{ FS=fdelim; } { 
        if ( NF != field_count ) {
< snip >

You can probably do it with -F as well and just get rid of the BEGIN statement:

awk -F $file_delimeter '{ 
        if ( NF != field_count ) {

Top stuff..solution well fine

cheers for responding! :slight_smile:

regards
Satnam