0

我刚刚注意到

grep -rni 'a2}' *

不会为所有具有字符串a2}的文档提供匹配行。为什么会这样?

我试图创建一个最小的示例,但是当我创建一个新文件并粘贴内容时,它失败了。所以我已将文件上传到Git 存储库。也许这是一个编码问题。

该文件的内容是:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{KV-Diagramme}
\label{chap:a2}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \PsTexAbbildungOhneCaption{figures/a2-1}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "skript"
%%% End:

结果grep -rni 'a2}' *

moose@pc08 ~/Downloads/algorithms/grep $ grep -rni "a2}" *
%%% End:master: "skript"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

但我期待

moose@pc08 ~/Downloads/algorithms/grep $ grep -rni "a2}" *
\label{chap:a2}

为什么我会得到这个结果?

4

2 回答 2

0

这是因为您的文件使用的是 Mac OS 9 行尾。您需要首先转换为 UNIX 行尾。你如何做取决于你的场景,但你可以用这个做一个文件:

tr '\r' '\n' < anhang-2.tex > anhang-2.txt

然后,您将能够 grep 该新文件。

于 2013-08-10T05:52:55.103 回答
0

该文件具有 CR 行终止符,因此它看起来是一个单行文件:

#> file anhang-2.tex
anhang-2.tex: LaTeX document, ASCII text, with CR line terminators

将其转换为 Linux 格式:

#>  mac2unix anhang-2.tex
mac2unix: converting file anhang-2.tex to Unix format ...
#>  grep -rni 'a2}' anhang-2.tex 
3:\label{chap:a2}
于 2013-08-10T05:41:28.550 回答