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 califo开发者_运维百科rnia
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!
$ 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
Try using
sed -i 's/ */\;/g' your-file
This will substitute every 2 or more spaces with a ;
in the file you pass it.
The best way is to query your database tables with the proper delimiter (ie ";"). See if you can do it that way. Otherwise,
$ echo "john doe 1900-01-01 california" | sed 's/ /;/2g'
john doe;1900-01-01;california
This will break if the name has 3 or more words.
精彩评论