LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 07: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, 08:25 AM   #2
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,397
Blog Entries: 1

Rep: Reputation: 64
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, 08: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, 08: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


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting Contents of File abdul_zu Linux - Software 3 12-04-2005 03:12 PM
show contents of random file? iluvatar Linux - Software 6 07-13-2004 02:53 AM
cutting down contents of text file kam_kenneth Linux - Newbie 2 05-22-2004 02:16 AM
How to save the shell's contents to a file? Xiangbuilder Linux - Newbie 3 09-04-2003 08:47 PM
Delete contents of the file, but not file? mikeshn Linux - General 9 05-28-2003 07:47 AM


All times are GMT -5. The time now is 06:05 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration