I need to implement a 3-line sliding window with Sed in order change any occurrence of the 3 following grouped lines in a big text file :Fax: 05.11.22.33.44<LF><LF><LF>with this :Fax: 05.11.22.33.44<LF>###<LF>I tried to do that with the following command line (sed running in a msdos batch file, but it doesn't work too under my linux bash) :sed -i :a;$!N;s/\nFax: \([ 0-9\.]*\n\n\);tenough;$!ba;:enough/\nFax: \1###\n/;$!ba;P;D file.txtWhat's wrong ?
Sed substitute with 3 lines sliding windows
sed
You got the P;D part right. The rest is a failed attempt at pulling lines in the pattern space until a substitution is successful, which isn't necessarily a bad thing but definitely not a sliding window.You should pull in one line when on the first line, then use a N;P;D cycle, (that way you always have three lines in the pattern space) and attempt to substitute each time you pull in a new linesed '1N;$!N;s/\(PATTERN\n\)\(\n\)$/\1###\2/;P;D' infile