2

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.

1
  • You can specify multiple ranges, the file will be processed only once. Dec 10, 2015 at 16:36

1 Answer 1

3

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

#/bin/sed -f

/begin1/,/end1/{
# commands specific to range 1
}

/begin2/,/end2/{
# 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).

2
  • Will this read the file only once?
    – WAF
    Dec 10, 2015 at 17:43
  • Yes - I've edited to clarify. Dec 10, 2015 at 17:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .