The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Removing blank lines from comma seperated and space seperated file. pinnacle Shell Programming and Scripting 11 05-08-2009 12:58 PM
Search and then concat 4m other file (comma seperated) szchmaltz UNIX for Dummies Questions & Answers 1 06-30-2008 03:45 AM
Handling .CSV( Comma seperated value) in awk Laud12345 Shell Programming and Scripting 1 05-09-2005 05:41 PM
How to load comma seperated values file (*.csv) into Oracle table handynas UNIX for Advanced & Expert Users 4 06-24-2002 01:35 PM
How to load comma seperated values file (*.csv) into Oracle table handynas UNIX for Dummies Questions & Answers 5 06-05-2002 12:10 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-07-2009
pinnacle pinnacle is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 182
Trimming fields for comma or pipe seperated file

I have file like this

FileA:
abc , "helloworld" , america
def,asia, japan
ghi, africa, ipl

Output Needed:
abc,"helloworld",america
def,asia,japan
ghi,africa,ipl

I would like to implement using awk.
I want to trim each field for its leading and trailing spaces.
  #2 (permalink)  
Old 05-07-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,553
for the past 6 posts, you have been asking awk questions, so what have you tried so far? have you been reading up on awk ?
  #3 (permalink)  
Old 05-07-2009
manish6725 manish6725 is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 13
Hi ..
USE THIS Command

awk '{gsub(" ","",$0); print $0;}' filename
regards
Manish
  #4 (permalink)  
Old 05-08-2009
pinnacle pinnacle is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 182
Quote:
Originally Posted by manish6725 View Post
Hi ..
USE THIS Command

awk '{gsub(" ","",$0); print $0;}' filename
regards
Manish

The above script just removes spaces from any where in the line.
I like to trim each field in a comma seperated file.

I wrote the following code:
Code:
awk '{gsub("^[ ]*","",$1);gsub("^[ ]*","",$2) ;gsub("^[ ]*","",$3) print $1,$2,$3;}' filename > filename1
awk '{gsub("[ ]*$","",$1);gsub("[ ]*$","",$2) ;gsub("^[ ]*$","",$3) print $1,$2,$3;}'filename1 > filename
The problem with this code is it not versatile for different files with different number of fields.

Appreciate help on making this versatile
  #5 (permalink)  
Old 05-08-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,322
This removes the spaces except the spaces within the double quotes:

Code:
awk -F"\"" '{for(i=1;i<=NF;i++){if(i%2)gsub(" ","",$i)}}1' OFS="\"" file
Regards
  #6 (permalink)  
Old 05-08-2009
pinnacle pinnacle is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 182
Quote:
Originally Posted by Franklin52 View Post
This removes the spaces except the spaces within the double quotes:

Code:
awk -F"\"" '{for(i=1;i<=NF;i++){if(i%2)gsub(" ","",$i)}}1' OFS="\"" file
Regards

Thankyou Franklin52

This was what i was looking for that does triming and not with in double quotes.

Thanks again
  #7 (permalink)  
Old 05-08-2009
pinnacle pinnacle is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 182
Quote:
Originally Posted by pinnacle View Post
Thankyou Franklin52

This was what i was looking for that does triming and not with in double quotes.

Thanks again
Code:
nawk -F"\"" '{for(i=1;i<=NF;i++){if(i%2)gsub("^[ ]*","",$i);gsub("[ ]*$","",$i)}}1' OFS="\"" filetst1
Modified as the earlier version was eating away the spaces between words in a csv thanks!!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:59 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0