Trim white spaces using awk | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Trim white spaces using awk

Shell Programming and Scripting


Tags
awk, awk trim, trim, trim awk

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-28-2006
mona's Avatar
mona mona is offline
Registered User
 
Join Date: Nov 2005
Last Activity: 18 June 2007, 11:17 PM EDT
Location: Singapore
Posts: 96
Thanks: 0
Thanked 2 Times in 1 Post
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  
Old 07-28-2006
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
 
Join Date: Apr 2004
Last Activity: 27 October 2014, 1:32 PM EDT
Location: Bordeaux, France
Posts: 1,711
Thanks: 2
Thanked 61 Times in 57 Posts
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  
Old 07-28-2006
mona's Avatar
mona mona is offline
Registered User
 
Join Date: Nov 2005
Last Activity: 18 June 2007, 11:17 PM EDT
Location: Singapore
Posts: 96
Thanks: 0
Thanked 2 Times in 1 Post
Thank you. It works
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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



All times are GMT -4. The time now is 08:57 AM.