Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change two or more spaces to a single semicolon

I am struggling with formatting of a db2 query. I would like to replace 2 or more spaces in a string with a semicolon. I know how to do it with all spaces, the question would be how to change only 2 or more spaces :P

Here is an example, i would like to change this:

john doe    1900-01-01    california

to

john doe;1900-01-01;california

The problem is that i have one space in the name field, so i can't use a simple tr command. In my real job, there can be a single space in any field.

I would appreciate your help very much! Thanks in advance!

like image 351
Aron Avatar asked Jul 26 '10 13:07

Aron


2 Answers

$ sed 's/   */;/g' 
john doe   1900-01-01  california 
john doe;1900-01-01;california 
one space  two  spaces   three    spaces
one space;two;spaces;three;spaces
like image 142
msw Avatar answered Oct 03 '22 16:10

msw


Try using

sed -i 's/   */\;/g' your-file

This will substitute every 2 or more spaces with a ; in the file you pass it.

like image 42
Alberto Zaccagni Avatar answered Oct 03 '22 17:10

Alberto Zaccagni