# La verificación del archivo contiene una línea que comienza con 'xxx' pero no con 'yxxx'

I am writing a small bash script that checks whether to execute makeindex or not if a .tex file contains the line \makeindex. The MakeIndex run would not be run if the command is commented-out.

How do I check that a file, say source.tex has the line?

I know that I need grep—I am, however, fairly new to regexes and bash scripting.

preguntado el 01 de febrero de 12 a las 14:02

## 3 Respuestas

As I recall, tex comments are %. So we'll search for a line that contains \makeindex sin un % before it on the line:

grep '^[^%]*\\makeindex' source.tex
#grep -- the program we're running, obviously.
#    '                 ' -- Single quotes to keep bash from interpreting special chars.
#     ^ -- match the beginning of a line
#      [  ] -- match characters in the braces.
#       ^  -- make that characters not in the braces.
#        % -- percent symbol, the character (in the braces) we do not want to match.
#          * -- match zero or more of the previous item (non-percent-symbols)
#           \\ -- a backslash; a single one is used to escape strings like '\n'.
#             makeindex -- the literal string "makeindex"
#                        source.tex-- Input file


Muestra:

$grep '\\end' file.tex 51:src/file.h\end{DoxyCompactItemize} 52:%src/file.h\end{DoxyCompactItemize} 53:src/%file.h\end{DoxyCompactItemize} 54: %\end{DoxyCompactItemize} 55:src/file.h\end{DoxyCompactItemize}%$ grep '^[^%]*\\end' file.tex
51:src/file.h\end{DoxyCompactItemize}
55:src/file.h\end{DoxyCompactItemize}%
$ Respondido 01 Feb 12, 19:02 If you want to anchor match to the beginning of line it's grep ^xxx files...  Respondido 01 Feb 12, 18:02 Thank you. Is it possible to use this in a [[$myvar =~ ^\s*xxx ]] fashion without having to iterate through the whole line? - Minustar

Haría algo como [[ "$myvar" = "${myvar#prefix}" ]] -- if variable with the said prefix cut off is the same then it has no such prefix. - Michael Krelin - hacker

BTW, why do you need to iterate through the whole line and what do you mean by "the whole line"? - Michael Krelin - hacker

You mean, you have the whole file in variable? I think it's more efficient to use grep. - Michael Krelin - hacker

Now I figured what you were asking about, thanks to Kevin's answer. I'll upvote him. - Michael Krelin - hacker

Puede hacer esto con una llamada a awk:

#!/bin/bash
if awk '/^xxx/{f=1}/^yyy/{f=0}END{if(!f)exit 1}' file; then
echo "file OK"
else