Re: lines matching N and N+1

"Daniel" <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
Yes, I did mean "word" as in GNU sed \<word\> sense.

With the "test file" and "desired output" I mentioned, 
here's what I came up with, similar to something I saw
from Y-J Chang. It assumes using -r command line option.

$! N 
s:(.*)\<foo\>(.*)\n(.*)\<bar\>(.*):\1FOO\2\n\3BAR\4:
t    
P; D 

# Append next line if NOT last line of file
# Try changing foo in line #1, bar in line #2
# If change happened, print lines and read a new pair
# Else, print & delete line #1, run N to make next pair

This seems similar to some of the other solutions 
that were posted.

Thanks,
Daniel

--- In [email protected], Davide Brini  wrote:
>
> On Mon, 28 Jan 2013 20:18:23 -0000, "Daniel"  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/\ \(.*\n.*\)\ /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/\(\ \)\(.*\n.*\)\(\ \)/\U\1\E\2\U\3\E/
> P
> D
> 
> 
> -- 
> D.
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.