Remove Spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove Spaces
# 1  
Old 04-23-2009
Remove Spaces

Hi All,
I have a comma seperated file.
I wanna remove the spaces from column 2. I mean i don't wanna remove the spaces those are presnt in column 1.

ex:
emp name, emp no, salary
abc k, abc 11, 1000 00
bhk s, bhk 22, 2000 00

the output should be:
emp name, emp no, salary
abc k, abc11, 100000
bhk s, bhk22, 200000

column 2 and 3 spaces needs to removed.
Can anyone has the solution for this problem.

Thanks:
Smilie
# 2  
Old 04-23-2009
Try this

awk -F "," 'OFS=","{gsub(/ /,"",$2);gsub(/ /,"",$3);print $1,$2,$3}' filename



cheers,
Devaraj Takhellambam
# 3  
Old 04-23-2009
Hi devtakh,

Thanks for your quick reply.
But, the number of columns may differ from file to file.
I mean to say that the number of columns are not constant.
Could anyone please suggest any other way!!!

Thanks
# 4  
Old 04-23-2009
Quote:
Originally Posted by javeed7
Hi devtakh,

Thanks for your quick reply.
But, the number of columns may differ from file to file.
I mean to say that the number of columns are not constant.
Could anyone please suggest any other way!!!

Thanks
awk -F "," 'OFS=","{first=$1;gsub(/ /,"",$0);$1=first;print $0}'


cheers,
Devaraj Takhellambam
# 5  
Old 04-23-2009
Code:
# awk -F"," '{gsub(/ +/,"",$2);}1' OFS="," file
abc k,abc11, 1000 00
bhk s,bhk22, 2000 00

perl (5.8)
Code:
# perl -F"," -lane '$F[1]=~s/\s+//g;print join(",",@F) ' file
abc k,abc11, 1000 00
bhk s,bhk22, 2000 00

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove spaces on a line?

Hi, suppose I have the following data: albert music=top40 age=20 bob music=punk rock age=25 candy music=r n b age=22 dave music=mozart or bach only age=30 I want to extract and manipulate the music column but it's got spaces in it. How can I substitute the space with an underscore... (2 Replies)
Discussion started by: almonds
2 Replies

2. Shell Programming and Scripting

Remove spaces in a file

Hi friends, I have a file1.txt 1 | a | 4757634 | jund jdkj | erhyj 2 | a | 4757634 | jnd jdkj | rhje hjrhwj i have used tr -d '\040' to remove the spcaes output file cat file1.txt | tr -d '\040' 1|a|4757634|jundjdkj|erhyj... (5 Replies)
Discussion started by: i150371485
5 Replies

3. Linux

How to remove spaces in the file?

hiii i have a file that contains spaces in the begining of a file till the middle the from there the txt would appear. hw can i remove those spaces and bring the text to the begining portion file1 text starts from here (12 Replies)
Discussion started by: anurupa777
12 Replies

4. Shell Programming and Scripting

remove spaces

Hi folks, I need to remove spaces at the end of each line in a *.txt file. it looks like this word 1 word 2 . . . word n i found some sed commands but any of them didnt work so far thank you for your posts (6 Replies)
Discussion started by: Jimmy7
6 Replies

5. Shell Programming and Scripting

Remove spaces in filenames

Hi, I have files like below, In files coming as spaces. Before transfering those files into ftp server. I want to remove the spaces and then can transfer the files into unix server. e.g: filenames are 1) SHmail _profile001_20120908.txt 2) SHmail_profile001 _20120908.txt 3) sh... (3 Replies)
Discussion started by: kirankumar
3 Replies

6. Shell Programming and Scripting

Not able to remove leading spaces

Hi Experts, In a file tht i copied from the web , i am not able to remove the leading white spaces. I tried the below , none of them working . I opened the file through vi to check for the special characters if any , but no such characters found. Your advice will be greatly appreciated. sed... (5 Replies)
Discussion started by: panyam
5 Replies

7. Shell Programming and Scripting

Remove spaces before a delimiter

Hi All, I need to modify a script to remove spaces from a csv file. The csv file is delimited by the '~' character and I need to remove the spaces which appear before this character. i.e Sample input: LQ001 SWAT 11767727 ~9104 ~001 ~NIRSWA TEST 18 ~2 ~Standard Test ~0011 Desired... (5 Replies)
Discussion started by: SRyan84
5 Replies

8. Shell Programming and Scripting

Remove spaces from columns

I have a file with only one field something like this : 213.45 220.65 240.47 345.67 320.45 I want to remove all spaces in between. Is there any unix command for that ?Thanks in advance.. (2 Replies)
Discussion started by: jacks
2 Replies

9. UNIX for Dummies Questions & Answers

How to remove trailing spaces

Hi, I have a file like this (ADD_MONTHS((Substr(Trim(BOTH FROM Translate(Maximum(closeDa ------------------------------------------------------------ 2007-06-30 00:00:00 I have a requirement where i need just the date. When i do: tail -1... (2 Replies)
Discussion started by: mahek_bedi
2 Replies

10. Shell Programming and Scripting

need help to remove spaces from first column

Hi, Here is sample data which I have: column#1 column#2 column#3 001A 50005 ROCKER ADJ 00010000100018UTIRR 001A 50020 CRANKSHAFT 0003445ES 001A 52201 SPARKPLUG ... (4 Replies)
Discussion started by: tayyabq8
4 Replies
Login or Register to Ask a Question