Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Edit, then save to a file without opening it (NO REDIRECT)

Status
Not open for further replies.

suhaimi

Technical User
Aug 3, 2001
71
US
perl -i -p -e 's/foo/bar/g' test.txt -- Is there any equivalent method in Unix Shell (eq vi, sed, awk ..etc). DON'T GIVE ME THE ANSWER BY USING REDIRECT (> OR >>) TO OTHER FILES.
 
Suhaimi,

The perl command IS called from the shell. See faq219-969 for an example of how to use the perl call to recursively change several files at once.

Cheers, Neil :cool:
 
suhaimi - redirection would be the obvious way to do this, why can't you use that? Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
If you really don't want to use the perl option for some reason, then you can try:
Code:
echo '1,$s/foo/bar/g
w' | ed - test.txt

Cheers, Neil :)
 
Neil,
The method you give does not work at all....

echo '1,$s/foo/bar/g
w' | ed - test.txt

MikeLacey,
The reason I do not want to use redirect is bcoz it's not practical to use if the file is very huge (>1000 K lines) and processed in for loop...

suhaimi
 
Try

echo '%s/foo/bar/g|w' | ex - test.txt

Hope this helps.

CaKiwi
 
Suhaimi,
The code worked OK when I tested it. Could you be more specific with the errors you are getting:
(1) is echo in your $PATH
(2) is ed in your $PATH
(3) does your shell allow continutions (ie: 'xxx[return]yyy')
(4) have you remembered the [return] between the subsitute and save command

I still don't understand why you can't use the perl command?
Cheers, Neil
 
Hi Neil,
Thanks for the code anyway. I don't know why it didn't work. I tried CaKiwi method it worked fine. Thanks CaKiwi.

Suhaimi
 
suhaimi,

if you are finding and replacing the same number of characters you can open the file in read/write mode with a C program or perl script - you can then scan through the file and write just the data you want into the original file

it's not particuarly easy though - i'd just use the redirect stuff if possible Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top