LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 09-08-2008, 06:48 AM   #1
Ashok_mittal
Member
 
Registered: Dec 2007
Posts: 73

Rep: Reputation: 15
How to replace space using sed in shell script


Hi All,

I want to use sed to replace multiple spaces with single character(say : ).
ex: test.txt

12:Mit 23627 83763 tty message


suppose the file test.txt contains the line as above and i want to replace all spaces with : so that the outcome should be like below:

12:Mit:23627:83763:tty:message

i have used

sed 's/ /:/g' test.txt

but this command replacing every single space with a :, so the output is like:

12:Mit:::23627:83763::::tty:::::::message
 
Old 09-08-2008, 06:57 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,361

Rep: Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909Reputation: 1909
Use the + modifer to match one or more occurences of the previous regular expression. It needs to be escaped:
Code:
$ echo "12:Mit     23627   83763   tty message" | sed 's/ \+/:/g'
12:Mit:23627:83763:tty:message
 
Old 09-08-2008, 07:05 AM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You could use tr instead:
Code:
tr -s ' ' ':' <inputfile >outputfile
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed - How do you replace end of line with a space pppaaarrrkkk Programming 7 02-07-2011 11:27 AM
how to replace value of 4.1.0.1.22 to 4.1.0.0.22 using shell script amit_pansuria Programming 3 08-06-2008 08:30 PM
Using sed to replace newling with space binarybob0001 Programming 2 05-29-2008 03:32 PM
Shell Script , using Sed mogra Linux - Newbie 8 05-07-2008 01:18 PM
Need a script to search and replace text in file using shell script unixlearner Programming 14 06-21-2007 10:37 PM


All times are GMT -5. The time now is 08:13 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration