LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-18-2005, 03:57 AM   #1
pcdude
LQ Newbie
 
Registered: Sep 2004
Location: South Africa
Distribution: Mandrake
Posts: 26

Rep: Reputation: 15
replacing characters in a file with others


Hi all.

I would like to know which command to use to sustitute a backslash('\') in a file with a forward slash('/'). Also I want to replace all the semi-colons(';') with colons (':').

I guessing that I'm going to have to use the sed command to do this.

I realize that I should be reading the manual to be getting these answers, I plan on doing this but it may take my quite a while, so I'm asking for something I can use right now.

Thanks in advance.
PCDude
 
Old 01-18-2005, 04:45 AM   #2
jrtayloriv
Member
 
Registered: Jun 2004
Location: Inland NW, US
Distribution: Ubuntu
Posts: 366
Blog Entries: 1

Rep: Reputation: 44
You were right about the man pages. Sed is one of the most useful tools that you have at your disposal, it would really benefit you to learn as much as you can about it. But I too want a quick answer every now and then so here it is:

Code:
sed 's/\\/\//g'  <INPUT FILE>
Replaces the slashes. Note that you need to use escape characters in front of those slashed to keep from getting sed confused. The g at the end tells it to replace all instances instead of just the first one it finds.

Code:
sed 's/;/:/g' <INPUT FILE>
Ditto, but for the semicolon.

Hope this helps,
jrtayloriv
 
Old 01-18-2005, 06:32 AM   #3
pcdude
LQ Newbie
 
Registered: Sep 2004
Location: South Africa
Distribution: Mandrake
Posts: 26

Original Poster
Rep: Reputation: 15
Thanks your replies.

That works well. It displays the correct output to the screen but does not make changes to the file.
I know this is typical of Linux and also a good thing.
Code:
sed 's/\\/\//g'  <INPUT FILE>  >  <INPUT FILE>
results in an empty file. I guess you can't read and write a file at the same time.

Is this the next easiest way of doing things:
Code:
sed 's/\\/\//g'  <TEMP FILE>
cat <TEMP FILE> > <INPUT FILE>
rm <TEMP FILE>
Thanks
PCDude
 
Old 01-18-2005, 06:45 AM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Quote:
sed 's/\\/\//g' <INPUT FILE> > <INPUT FILE>
I think maybe sed 's/\\/\//g' file.txt > file1.txt

If you only want one file use a move operation at the end....
mv file1.txt file.txt
 
Old 01-18-2005, 07:01 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
The option '-i' which stands for inplace will replace the input file immediately.

Also, if you want to replace the '/' character, you can use a different character as a seperator in the command.

sed -i 's#/#\\#g' filename
 
Old 01-18-2005, 07:12 AM   #6
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,304

Rep: Reputation: 65
instead of
sed 's/\\/\//g' <INPUT FILE> > <INPUT FILE>

you can sed 's/\\/\//g' < input file >output file

the input file is the original, and the output file is the changed one.
 
Old 01-19-2005, 12:29 AM   #7
pcdude
LQ Newbie
 
Registered: Sep 2004
Location: South Africa
Distribution: Mandrake
Posts: 26

Original Poster
Rep: Reputation: 15
Thank you all for your replies.

I think there was some confusion here, I wanted to modify my input file, not output to a different file.
So sed's -i option works well.

Thanks.
PCDude
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
replacing text in file kushalkoolwal Programming 17 10-25-2005 05:25 AM
Replacing a particular position in a file sgracelin Linux - Newbie 14 06-23-2005 06:40 AM
Replacing a particular position in a file sgracelin Linux - Newbie 1 06-20-2005 05:54 PM
Replacing String with File Content in Sed meshcurrent Linux - General 2 06-01-2003 12:54 AM
perl: replacing a special line in a file markus1982 Programming 1 09-26-2002 02:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:10 PM.

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