我有
1. Lorem
He he he
% not sure spelling
2. Lorem
ipsun
我希望有
\textbf{1. Lorem}
He he he
% not sure spelling
\textbf{2. Lorem}
ipsun
我的伪代码尝试
perl -000pe 's/\n\n\d./; s/\n\d.\n\\textbf\{ /g; s/$/\}/'
这是基于我之前关于正则表达式的两个问题。我尝试匹配以数字开头的内容。替换比赛的开始和比赛的结束。
代码给了我
Backslash found where operator expected at -e line 1, near "s/\n\n\d./; s/\"
Backslash found where operator expected at -e line 1, near "n\"
Backslash found where operator expected at -e line 1, near "n\"
Backslash found where operator expected at -e line 1, near "textbf\"
Backslash found where operator expected at -e line 1, near "$/\"
(Missing operator before \?)
syntax error at -e line 1, near "s/\n\n\d./; s/\"
Execution of -e aborted due to compilation errors.
如何将给定的文本加粗?
答案1
由于-000
使每个“段落”成为“行”,因此您可以使用经典的正则表达式锚点(^
和$
)来匹配每个“行”的开头和结尾。因此,就您而言,您所需要的只是:
$ perl -000pe 's/^(.+)\n/\\textbf{$1}\n/;' file
\textbf{1. Lorem}
He he he
\textbf{2. Lorem }
ipsun
请注意,需要\
转义(\\
),因为\
是一个特殊字符,用于转义其他字符,因此您还需要使用它来转义自身。
如果您可以将注释作为段落的第一行,那么此方法会失败,您需要将所有以数字开头的行加粗:
perl -pe 's/^\d\..+/\\textbf{$&}/' file
答案2
尝试这个:
$ perl -ple '$_ = "\\textbf{$_}" if /^\d/' cat2
\textbf{1. Lorem}
He he he
\textbf{2. Lorem}
ipsun
答案3
我使用了-0777
将整个文件立即加载到内存中的选项。然后,您可以替换换行符:
perl -0777 -pe 's/\n(\d\..*)/\n\\textbf{$1}/g'