If I have a file that looks like this:
word
foo
bar
word
And I want to duplicate foo\nbar lines, so that it looks like this:
word
foo
bar
foo
bar
word
I have tried using N to load the next line into the buffer, but I must be using it incorrectly, as it appears to skip over lines sometimes.
sed -e '{
N
s/\(foo\nbar\)/\1\1/
}' foobar.txt
I think it is loading word\nfoo into the buffer, then bar\nword into the buffer, and misses the pattern entirely. How do you use N appropriately? Would this be easier with awk, perl, or some other tool?
Since you specifically tagged the question with sed I thought I’d post a sed solution: