Re: lines matching N and N+1
Davide Brini <[email protected]>
| Newsgroups | gmane.editors.sed.user |
|---|---|
| Message-ID | <[email protected]> |
On Mon, 28 Jan 2013 20:18:23 -0000, "Daniel" <[email protected]> wrote: > This came up in a recent cross-Atlantic conversation. :) > > The problem: How to apply a change when a *combination* > of lines occurs, eg when the word "foo" appears in line > N and the word "bar" appears in line N+1. How then to > apply the change in line N (or line N+1)? For example, > change word "foo" to "FOO" and word "bar" to "BAR". > > Here's a test file I dreamed up: > > food foot foo > bare bark bar > bar bark bare > bark bar bare > foot foo food > bark bar bare > foot foo food > foo foot food > bar bark bare > foot foo food > > Here's the desired output: > > food foot FOO > bare bark BAR > bar bark bare > bark bar bare > foot FOO food > bark BAR bare > foot foo food > FOO foot food > BAR bark bare > foot foo food > > I tried doing this in bash and couldn't make it > work. I do have a possible sed solution. > > Thought I'd post this, see if anyone would like > to take a look, either sed or bash (or whatever!). I assume that you use "word" as meant by GNU sed's \< and \> operators (or \b). Leaving out some unspecified corner cases: $!N s/\<foo\>\(.*\n.*\)\<bar\>/FOO\1BAR/ P D If "foo" and "bar" are regular expressions and not fixed strings, turning them to uppercase is more complicated, yet still possible using GNU sed (which I'm assuming anyway); $!N s/\(\<foo\>\)\(.*\n.*\)\(\<bar\>\)/\U\1\E\2\U\3\E/ P D -- D.