Replace spaces at a specific Location


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace spaces at a specific Location
# 1  
Old 02-09-2012
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 04:07 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-09-2012
Code:
perl -F, -ane '$F[-4]=~s/ $//;print join(",", @F)' inputfile

# 3  
Old 02-10-2012
try this

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

# 4  
Old 02-10-2012
Code:
tr -d ' ' < file

# 5  
Old 02-10-2012
Quote:
Originally Posted by parthmittal2007
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.
# 6  
Old 02-10-2012
Just column 12 or both column 11 and 12?
Code:
awk -F, '{gsub(/[ \t]/,x,$11);gsub(/[ \t]/,x,$12)}1' OFS=, infile

This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 02-10-2012
Thank you very much. This does it exactly what I needed.. Need to learn awk for sure.. Very helpful. Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell command to Strip white spaces at specific location

Hello, I have a Comma separated input file with one of the sample records as below: -9223372036854477528,"834","834003325515BXNI00101012013C","5","PLAN_VALUE","PPO, General Cable","C",20130101,99991231,"A","2012-12-25-13.58.14.434000","ZPL2 ","2012-12-25-13.58.14.434000","ZPL2 ... (4 Replies)
Discussion started by: Praveenkulkarni
4 Replies

2. Shell Programming and Scripting

Using sed to replace a word at specific location

I'm try to change a the prohibit to aix for the lines starting with ssh and emagent and rest should be the same. Can anyone please suggest me how to do that using a shell script or sed passwd account required /usr/lib/security/pam_prohibit passwd session required ... (13 Replies)
Discussion started by: pjeedu2247
13 Replies

3. Shell Programming and Scripting

Content merging at a specific location in a file

Hi, This is a bit lengthy problem, i will try to keep explaining it simple. I have got a file say file1 that contains the following in it, ------------------------------------------------------------------------ r201463 | ngupta@gmail.com | 2012-06-19 22:02:20 +0530 (Tue, 19 Jun 2012) |... (3 Replies)
Discussion started by: Kashyap
3 Replies

4. Shell Programming and Scripting

Help with replace character based on specific location

Hi, I got long list of reference file (column one is refer to the header in input file; column 2 is info of start position in input file; column 3 is info of end position in input file;) shown as below: read_2 10 15 read_3 5 8 read_1 4 10 . . . Input file (huge file with total... (6 Replies)
Discussion started by: perl_beginner
6 Replies

5. Shell Programming and Scripting

Find and replace a string a specific value in specific location in AIX

Hi, I have following samp.txt file in unix. samp.txt 01Roy2D3M000000 02Rad2D3M222222 . . . . 10Mik0A2M343443 Desired Output 01Roy2A3M000000 02Rad2A3M222222 . . (5 Replies)
Discussion started by: techmoris
5 Replies

6. Shell Programming and Scripting

Updating the files at specific location

Hi All, I have a requirement wherein I need to test if a line in file succeeding "IF" statement has "{" .If the line succeeding "IF" statement does not have "{" , then I need to insert the "{". Similarly , I need to check if a line in file preceding "ENDIF" statement has "}" .If not , then I... (2 Replies)
Discussion started by: swapnil.nawale
2 Replies

7. Shell Programming and Scripting

How to append a string to a specific location in a line

Hi, I have a to modify a line and insert a keyword in the middle to a specific location. My line looks like this FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS (ABC, DEF, GHI) I want to change it as FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' TRAILING... (4 Replies)
Discussion started by: mwrg
4 Replies

8. Shell Programming and Scripting

how to Add word at specific location in line

eg . i have file x.txt contains : java coding , shell scriptting etc... now i want to add "is langauge" after word java. output should be java is langauge coding , shell scriptting etc... any idea how to use shell script to do it ? (10 Replies)
Discussion started by: crackthehit007
10 Replies

9. Infrastructure Monitoring

Need to kill processes from a specific location

Hi, I need to kill processes from a specific location for Linux, HP, AIX and Solaris. My problem is to be able to identify the location of the running processes. I've tried with a simple "ps -fu username | grep mylocation" but if the process command has been run directly from the location,... (2 Replies)
Discussion started by: Peuj
2 Replies

10. HP-UX

replacing text in specific location

i have a file that looks like this: 000000112/01/2008 D99999 000000 12/01/2008 D99999 000000 12/01/2008 1D99999 i need to replace the blanks into 1 for column 7,18-19 how can this be achieved using awk? Thanks. (1 Reply)
Discussion started by: zeontman
1 Replies
Login or Register to Ask a Question