I have a script that reads a file of about 3GB and sends it through a pipeline involving a very small number of replacements in a very small part of the file. To accomplish this with minimal overhead, the script specifies a small known range within which to make the replacements using sed '/begin/,/end/'.

I would like to add another couple replacements in another small, known, "regex-delimited" range in the same file. If I pipe through sed again, that introduces unnecessary overhead, and won't scale up nicely.

Is there a way to specify two ranges between patterns if I know the order in which they will appear in the file, such that the file only needs to be read once?

Something like 'sed '/begin1/,/end1/ ... /begin2/,/end2/' is what I have in mind.

The sed manual states

An address range can be specified by specifying two addresses separated by a comma (,).

But it doesn't mention specifying sets of two addresses each.

  • You can specify multiple ranges, the file will be processed only once. – don_crissti Dec 10 '15 at 16:36

You don't need to know which range appears first in the file. You can write the obvious:

#/bin/sed -f

# commands specific to range 1

# commands specific to range 2

and it will Just Work.

Sed always makes a single pass through its inputs, reading each line, processing it through the commands you've given it, and then writing (or not, as appropriate). You can prove this by executing sed in a pipeline (piped input is not seekable, so multi-pass operation is not possible).

| improve this answer | |
  • Will this read the file only once? – WAF Dec 10 '15 at 17:43
  • Yes - I've edited to clarify. – Toby Speight Dec 10 '15 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.