[Chapter 33] 33.8 Batch Editing Gotcha: Editors Bomb on Big Files
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:46:54Z

#

| Chapter 33 Batch Editing |

## 33.8 Batch Editing Gotcha: Editors Bomb on Big Files

People use the ed editor with script files to make global edits. But many versions of ed can't edit large files. The ex editor is usually better, but it has limits, too. How large is "large"? That depends on your version. Most eds I've seen can't handle more than about 100,000 characters.

There are no limits on sed (34.24), although you'll need to save its output somehow (34.3), and your editing script may have to be changed to work with sed. [1] Here's what you'll see when ed bombs:

> [1] By default, ed commands apply to the current line. sed commands are global. Also, relative line addresses like `-5` don't work in sed.

>     % cat edscr
>     s/Unix/UNIX/g
>     w
>     % ed - words < edscr
>     ?
>     %

The `?` is ed's verbose way of telling you that something's wrong. This obscure message is especially bad if you write a shell script that edits multiple files in a loop; you may not notice the error or be able to tell which file had the problem. Be sure your script checks for errors!

Unfortunately for programmers, ed may not return an error status that you can test. There are workarounds (46.9), though. When the `ed -` command succeeds, it doesn't display anything. The simplest way to find errors is to check for any output on stdout or stderr. This chunk of a Bourne shell script shows how (your filename is in the shell variable `$filename` (6.8)):

    2>&1
    [ ] $?
    edout="`ed - $filename < edscr 2>&1`"
    if [ -n "$edout" -o $? -ne 0 ]
    then
       echo "$edout" 1>&2
       echo "QUITTING: 'ed - $filename < edscr' bombed?!?" 1>&2
       exit 1
    fi

- JP

33.7 ed/ex Batch Edits: Avoid Errors When No Match |  | 33.9 patch: Generalized Updating of Files that Differ