|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
||||
|
||||
|
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 Code:
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 |
| Sponsored Links | ||
|
|
#2
|
||||
|
||||
|
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. |
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
Thank you. It works
![]() |
| Sponsored Links | ||
|
![]() |
| Tags |
| awk, awk trim, trim, trim awk |
| Thread Tools | Search this Thread |
| Display Modes | |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| trim spaces and replacing value | badrimohanty | Shell Programming and Scripting | 2 | 06-29-2009 04:30 PM |
| trim spaces in a file | badrimohanty | Shell Programming and Scripting | 7 | 06-22-2009 01:53 PM |
| How to trim the white space around a string in C program | hxm1303 | Programming | 18 | 10-17-2008 10:43 AM |
| TRIM spaces in shell | anumkoshy | UNIX for Advanced & Expert Users | 3 | 08-31-2007 04:13 AM |
| To Trim spaces at the end of line | sbasetty | Shell Programming and Scripting | 1 | 01-31-2007 09:01 PM |
|
|