LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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
  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
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

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


Reply



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



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:49 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration