Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-09-2012
Registered User
 

Join Date: Aug 2010
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Replace spaces at a specific Location

Hello All,
I have a comma separated file which needs to be loaded to the database. But, I need to trim the white spaces for a specific column before its loaded.

Below is the sample line from the input file:

Code:
690,690,0575,"01011940","01011940", , ,   ,  ,  ,36720,36722,V2020,V2999,
690,690,0576,"01011940","01011940", , ,   ,  ,  ,3674 ,3674 ,V2020,V2999,
690,690,0577,"01011940","01011940", , ,   ,  ,  ,36860,36860,V2020,V2999,
690,690,0578,"01011940","01011940", , ,   ,  ,  ,36869,36869,V2020,V2999,
690,690,0579,"01011940","01011940", , ,   ,  ,  ,3688 ,3688 ,V2020,V2999,


The text in bold needs to be trimmed if there are any spaces before and after the data. To be specific any spaces between the 11th and 12th column should be removed and rest of them should be maintained. Is there any specific command that can do this for me? Thanks for you help in advance.

Last edited by Franklin52; 02-10-2012 at 03:07 AM.. Reason: Please use code tags for code and data samples, thank you
Sponsored Links
    #2  
Old 02-09-2012
balajesuri's Avatar
#! /bin/bash
 

Join Date: Apr 2009
Location: India
Posts: 1,019
Thanks: 9
Thanked 285 Times in 277 Posts

Code:
perl -F, -ane '$F[-4]=~s/ $//;print join(",", @F)' inputfile

Sponsored Links
    #3  
Old 02-10-2012
Registered User
 

Join Date: Jul 2011
Posts: 84
Thanks: 0
Thanked 16 Times in 15 Posts
try this


Code:
awk -F"," '{$1=$1;print}' filename

    #4  
Old 02-10-2012
Registered User
 

Join Date: Dec 2011
Posts: 154
Thanks: 21
Thanked 2 Times in 2 Posts

Code:
tr -d ' ' < file

Sponsored Links
    #5  
Old 02-10-2012
balajesuri's Avatar
#! /bin/bash
 

Join Date: Apr 2009
Location: India
Posts: 1,019
Thanks: 9
Thanked 285 Times in 277 Posts
Quote:
Originally Posted by parthmittal2007 View Post
Code:
tr -d ' ' < file

@parthmittal2007: Wouldn't that delete spaces in other fields too? Requirement is to delete spaces only from those fields marked in bold.
Sponsored Links
    #6  
Old 02-10-2012
Scrutinizer's Avatar
mother ate her
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 5,371
Thanks: 80
Thanked 1,107 Times in 1,011 Posts
Just column 12 or both column 11 and 12?

Code:
awk -F, '{gsub(/[ \t]/,x,$11);gsub(/[ \t]/,x,$12)}1' OFS=, infile

The Following User Says Thank You to Scrutinizer For This Useful Post:
Praveenkulkarni (02-10-2012)
Sponsored Links
    #7  
Old 02-10-2012
Registered User
 

Join Date: Aug 2010
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Thank you very much. This does it exactly what I needed.. Need to learn awk for sure.. Very helpful. Thanks again
Sponsored Links
Reply

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
Help with replace character based on specific location perl_beginner Shell Programming and Scripting 6 03-18-2011 03:57 AM
Find and replace a string a specific value in specific location in AIX techmoris Shell Programming and Scripting 5 03-11-2010 06:24 PM
Updating the files at specific location swapnil.nawale Shell Programming and Scripting 2 12-29-2009 01:19 PM
how to Add word at specific location in line crackthehit007 Shell Programming and Scripting 10 09-09-2009 07:44 AM
using sed to replace a specific string on a specific line number using variables todd.cutting Shell Programming and Scripting 2 08-13-2009 09:40 PM



All times are GMT -4. The time now is 04:55 AM.