How to Use Sed Command to replace white spaces with comma from between two fields - Mayank


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Use Sed Command to replace white spaces with comma from between two fields - Mayank
# 1  
Old 07-07-2010
How to Use Sed Command to replace white spaces with comma from between two fields - Mayank

SHELL SCRIPT

Hi
I have a file in the following format
Mayank Sushant
Dheeraj Kunal
ARUN Samir

How can i replace the white space in between and replace them with a comma?? The resultant output should be
Mayank,Sushant
Dheeraj,Kunal
ARUN,Samir

i tried using
sed -e "s/[ ^I]/,/g"
and
sed -e "s/[ ^I]*/,/g"
# 2  
Old 07-07-2010
Hi
If it is single space, this should do for you:

Code:
sed 's/ /,/'  file

Guru
# 3  
Old 07-07-2010
Try even if more than one space...

Code:
 
sed 's/\( \)\{1,\}/,/g' infile

# 4  
Old 07-07-2010
Data

Hi Guru

Actually i took output of a MySql query (the result had 2 columns) into a file. There is white space between the values of two separate columns.

I tried saving it as a CSV file but it did not work. And the sed command u have given or the ones i have tried do not work either.

---------- Post updated at 04:42 PM ---------- Previous update was at 04:39 PM ----------

Hi,

The data within the file is like :

2000020354 Testing
2000020351 Sagar
2000020342 Hello
2000020334 asdsadsda
2000020329 Hello
2000020328 samir
2000020328 sameer

Its not multiple spaces but a single tab. and yet no sed command is working. Is it due to the reason that i saveed the result from a Mysql Query??
# 5  
Old 07-07-2010
Quote:
Originally Posted by mayanksargoch
Hi Guru

Actually i took output of a MySql query (the result had 2 columns) into a file. There is white space between the values of two separate columns.

I tried saving it as a CSV file but it did not work. And the sed command u have given or the ones i have tried do not work either.
have you tried this?

Code:
sed 's/\( \)\{1,\}/,/g' infile

# 6  
Old 07-07-2010
hey Malcomex999, Yes I did try this but did not work...
# 7  
Old 07-07-2010
Assuming you are having two columns only...

Code:
 
awk 'NF{print $1","$2}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to extract fields from a CSV i.e comma separated where some of the fields having comma as value?

can anyone help me!!!! How to I parse the CSV file file name : abc.csv (csv file) The above file containing data like abv,sfs,,hju,',',jkk wff,fst,,rgr,',',rgr ere,edf,erg,',',rgr,rgr I have a requirement like i have to extract different field and assign them into different... (4 Replies)
Discussion started by: J.Jena
4 Replies

2. Shell Programming and Scripting

Replace spaces with underscores up to first comma but not after the comma

I have a comma delimited file of major codes and descriptions. I want to replace all occurrences of spaces with underscores up to the first comma (only in the first field), but not replace spaces following the comma. For instance I have the following snippet of the file: EK ED,Elementary and... (7 Replies)
Discussion started by: tdouty
7 Replies

3. 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

4. Shell Programming and Scripting

How to replace NewLine with comma using sed?

Hi , I have a huge file with following records and I want to replace the last comma with ',NULL'. I try using SED but could not create a correct script . In my opinion I need a script which can convert ,/n with ,NULL/n 1,CHANGE_MEMBER,2010-12-28 00:05:00, 2,CHANGE_MEMBER,2012-09-02... (8 Replies)
Discussion started by: Ajaypal
8 Replies

5. UNIX for Dummies Questions & Answers

How to replace two or more spaces with one comma?

I'm using sh on hp-ux. I've got a file that looks like this. -5.65 175 -16.17 160 -13.57 270 -51.72 260 -8.30 360 -42.71 460 -.38 375 -.20 375 -4.15 170 -21.53 560 -18.84 360 I'd like to replace all the whitespace between the columns with one comma. I can't... (4 Replies)
Discussion started by: Scottie1954
4 Replies

6. Shell Programming and Scripting

Replace white spaces of multiple files at once

How to rename multiple files by replacing the white spaces with underscore for ex: 293TrexFH\ \ \ GSM855007RINGB_lhb11_binary.txt 293TrexFH\ \ \ GSM855007RINGB_lhb12_binary.txt 293TrexFH\ \ \ GSM855007RINGB_lhb13_binary.txt 293TrexFH_GSM855007RINGB_lhb11_binary.txt... (1 Reply)
Discussion started by: quincyjones
1 Replies

7. AIX

Replace all TAB characters with white spaces

Dear Gurus Can you please advise me on how to Replace all TAB characters with white spaces in a text file in AIX? Either using vi or any utilities (2 Replies)
Discussion started by: tenderfoot
2 Replies

8. Shell Programming and Scripting

Include white spaces while using CUT command

Hi I tried to extract 19 characters (default) enclosed with in tag from a file using cut command. If the characters comprises of double space, the cut command gives the output with a single spacing. file 1 <name>Kumar Rajasekaran</name> cut -c7-26 "file1" the out put i received is ... (48 Replies)
Discussion started by: Sekar1
48 Replies

9. Shell Programming and Scripting

Help with sed and replacing white spaces with commas

Dear all, I am in a bit of a quandary. I have 400 text files which I need to edit and output in a very specific way. Here is a sample text file copied from gedit ... The columns will come out a bit messed up but when I cat <file>, it gives a table with six columns (0-28, tot lob vol, vcsf,... (6 Replies)
Discussion started by: antonz
6 Replies

10. UNIX for Dummies Questions & Answers

Replace only first found white spaces with some other characters

Anybody can help me How can I replace only four first white spaces with , or any other characters aaaa 08/31/2004 08/31/2009 permanent Logical Processors in System: 64 bedad 08/16/2001 08/15/2011 permanent Logical Processors in System: 64 badnv14 05/31/2008 05/30/2013 permanent Logical... (5 Replies)
Discussion started by: pareshan
5 Replies
Login or Register to Ask a Question