LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-25-2006, 06:41 AM   #1
shiroh_1982
LQ Newbie
 
Registered: May 2006
Posts: 9

Rep: Reputation: 0
Replace contents of a file


Hi,

I want to replace the contents of a file.I tried using :
sed 's/01514581/01514582/' $p
where 01514581 is the original value
01514582 is the replaced value
$p is the file name (captured in a variable)..

The output does not recognise $p

If you give :
sed 's/01514581/01514582/' test
where 'test' is the filename

then the output is not getting saved into the file 'test'..

Kindly let me know how i can do this.
Thanks
 
Old 05-25-2006, 07:25 AM   #2
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,499
Blog Entries: 2

Rep: Reputation: 68
Quote:
Originally Posted by shiroh_1982
If you give :
sed 's/01514581/01514582/' test
where 'test' is the filename

then the output is not getting saved into the file 'test'..
The editing is not in place. I mean, the original file remains intact and a new one must be created. sed is supposed to be used as a "filter", not as a in-place editor.

Create a copy of test, use the copy as input to sed, and send the modified stream to test.
Code:
$ cp test test.original
$ sed 's/01514581/01514582/' test.original > test

Quote:
Originally Posted by shiroh_1982
The output does not recognise $p
Thats mean $p was not set in the proper way. If you are using bash:
Code:
$ p="test"
$ echo $p
test
$ q="${p}.original"
$ sed 's/01514581/01514582/' $q > $p
 
Old 05-25-2006, 07:29 AM   #3
pronoy
Member
 
Registered: Mar 2005
Location: india
Distribution: FC4, Debian Etch
Posts: 67

Rep: Reputation: Disabled
Quote:
Originally Posted by shiroh_1982
Hi,

I want to replace the contents of a file.I tried using :
sed 's/01514581/01514582/' $p
where 01514581 is the original value
01514582 is the replaced value
$p is the file name (captured in a variable)..

The output does not recognise $p
Sorry I could not get the problem here

Quote:
If you give :
sed 's/01514581/01514582/' test
where 'test' is the filename

then the output is not getting saved into the file 'test'..

Kindly let me know how i can do this.
Thanks
you can do it in 2 ways:

1.
$ sed 's/01514581/01514582/' test >/tmp/test.tmp
$ mv /tmp/test.tmp test
2. perl -pi -e 's/01514581/01514582/' test

Hope this works.
 
Old 05-25-2006, 07:50 AM   #4
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
Quote:
Originally Posted by shiroh_1982
the output is not getting saved into the file 'test'..
"sed -i 's/01514581/01514582/' test" will also do the trick.
 
  


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
Deleting Contents of File abdul_zu Linux - Software 3 12-04-2005 02:12 PM
show contents of random file? iluvatar Linux - Software 6 07-13-2004 01:53 AM
cutting down contents of text file kam_kenneth Linux - Newbie 2 05-22-2004 01:16 AM
How to save the shell's contents to a file? Xiangbuilder Linux - Newbie 3 09-04-2003 07:47 PM
Delete contents of the file, but not file? mikeshn Linux - General 9 05-28-2003 06:47 AM

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

All times are GMT -5. The time now is 07:09 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