0

I've just noticed that

grep -rni 'a2}' *

does not give all documents that have a string a2} the matching line. Why is this the case?

I've tried to create a minimal example, but when I create a new file and paste the content, it fails. So I've uploaded the file to a Git repository. Perhaps it's a encoding problem.

The content of the file is:

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

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

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

The result of grep -rni 'a2}' * is

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

but I expected

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

Why do I get this result?

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958

2 Answers2

0

The file has CR line terminators so it looks a a single-line file:

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

convert it to Linux format:

#>  mac2unix anhang-2.tex
mac2unix: converting file anhang-2.tex to Unix format ...
#>  grep -rni 'a2}' anhang-2.tex 
3:\label{chap:a2}
perreal
  • 94,503
  • 21
  • 155
  • 181
0

It's because your file is using Mac OS 9 line endings. You will need to first translate to UNIX line endings. How you do so depends on your scenario but you can do one file with this:

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

Then you will be able to grep that new file.

cyberwombat
  • 38,105
  • 35
  • 175
  • 251