Trim white spaces using awk
Hi,
I have a CSV file with footer information as below. The third value is the number of records in the file. Sometimes it contains both leading and trailing white spaces which i want to trim using awk.
C,FOOTER , 00000642
C,FOOTER , 00000707
C, FOOTER, 42
C, FOOTER, 129
eval `awk -F, ' /FOOTER/ {print "set RecordsFound=" $3}
/HEADER/ { print "set FileBusinessDate=" $4} END{ print "set ActualRecords=" NR-2 } ' ${SourceDir}/${FileCsv} `
Above is the command which is already present in production and this statement fails since the $3 has spaces in it. So far we didn't get any spaces in the CSV, so this statement was working fine for the past 1 year.
Is there any way to trim the spaces(both leading and trailing spaces) in AWK?
I tried gsub(/ /,"",$3) but it doesn't work
Any help is much appreciated
Thanks in Advance
Mohana
‘-----------------------------------------------------------------------------------------------------------
Your statement is valid for one space, insert it in your AWK program :
Code:
/FOOTER/ {gsub(/ /,"",$3); print "set RecordsFound=" $3}
To suppress multiple spaces :
Code:
/FOOTER/ {gsub(/ */,"",$3); print "set RecordsFound=" $3}
Perhaps your field contains tabulation, modify the gsub statement :
Code:
/FOOTER/ {gsub(/[[:space:]]*/,"",$3); print "set RecordsFound=" $3}
jean-Pierre.
Share with your friends: |