LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-24-2006, 04:49 PM   #1
pxumsgdxpcvjm
Member
 
Registered: Aug 2005
Posts: 235

Rep: Reputation: 30
ed Scripting Question


Hi, I'm trying to use unix utility ed to search and replace lines in a number of '.id' files

I type the following command:

find -name '*.id' -exec ed {} < ed_script \;

Where the ed_script is the find and replace commands I made.

This ed script only makes the changes to the first file found. If you take away the .id file, then the script will operate on the new file that is first, but it never operates on more than 1 file. Does anyone know why this is? Should I make a more complex script using sed to do my file editing?

Thanks

Last edited by pxumsgdxpcvjm; 12-24-2006 at 07:55 PM.
 
Old 12-25-2006, 10:27 AM   #2
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

This illustrates one interesting characteristic of processes. Here is my understanding: the script does not directly substitute file ed_script for STDIN for the ed command. Since "< ed_script" is unprotected from the shell, it re-directs for this process, the find process. The child process in which ed runs will inherit this re-direction and can be read once. After that, an EOF is encountered for subsequent reads.

There are a number of solutions. You can place the ed and the re-direct into a second script, omitting any mention of re-direction in the script in which find is located, and calling the new script in the exec portion of the find.

Another solution would be to protect the command ed <ed_script with quotes, but then you would need to use eval to re-scan it. However, a further complication arises because eval is not an external command, it is a shell builtin. That means that you would need to call bash explicitly, like:
Code:
find d1 -name 't?' -exec bash -c "eval ed {} <ed_script" \;
One slight drawback to ed is that you will see the character counts for reading and writing when you edit the file.

You don't need to use sed in place of ed, but I tend to use sed. It has an extra complication in that it will require you to use in-place editing in sed, or re-copy the output stream over the source file. I recommend you work on copies of your important files while getting any of these solutions running.

I've tested all of these for this problem (except sed, which is fairly straight-forward).

You could also investigate opening and closing STDIN, but I did not look at that, because you'd probably do that in a script, and. if you're going to write a script, you might as well use the simplest solution.

As an aside, the find command has some interesting features and uses; see http://amitsharma.linuxbloggers.com/how_to_find.htm for an good tutorial with examples.

Best wishes ... cheers, makyo
 
Old 12-25-2006, 10:30 AM   #3
penguiniator
Member
 
Registered: Feb 2004
Location: Olympia, WA
Distribution: SolydK
Posts: 442
Blog Entries: 3

Rep: Reputation: 60
Yes. sed was designed to operate on streams by default, so you won't have those problems.
 
  


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
Yet another scripting question kinetik Linux - General 4 04-01-2006 12:30 PM
scripting question kpachopoulos Programming 1 03-03-2006 01:03 PM
Scripting Question... Darklight451 Linux - Newbie 3 09-23-2004 05:03 PM
Scripting question.... Darklight451 Linux - Newbie 2 09-13-2004 11:29 PM
Scripting question aikitortoise Linux - Newbie 3 11-14-2002 05:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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