文本内容如下:
...
....
\begin{quote}
....
....
...
...
\end{quote}
...
....
....
\begin{quote}
....
....
...............
........
\end{quote}
....
需要把\begin{quote}
和\end{quote}
之间内容进行替换。
awk 方案
{
if($0 ~ /begin{quote}/){
start=FNR
}
if(FNR > start && start > 0){
if(length($0)>2){ # non-blank line
if($0 !~ /end{quote}/){
if($0 !~ /^\\/){
print $0"\\\\"
} else {
print
}
# print $0"\\\\"
} else {
print
start=""
end=FNR
}
} else { #blank line
print
}
} else {
print
}
}
结果如下:
\begin{quote}
....\\
....\\
...............\\
........\\
\end{quote}
sed 方案
/begin{quote}/{
p;n;
:a
N;
/end{quote}/!ba;
N;
s/\n\n/\\\\\n/g;
};
p;
结果呢,会把其间的空行删除。如下:
\begin{quote}
....\\
....\\
...............\\
........\\
\end{quote}
比较而言,awk更加的精准,但是if结构复杂。sed语言精炼,但是需要做二次处理。