文件检查包含以“xxx”开头而不是“yxxx”的行

时间:2012-02-01 14:20:22

标签: regex bash grep

我正在编写一个小的bash脚本,检查是否执行makeindex,如果.tex文件包含行\makeindex。如果命令被注释掉,则不会运行MakeIndex运行。

如何检查文件,说source.tex有行?

我知道我需要grep - 但是,对于正则表达式和bash脚本来说,我是相当新的。

3 个答案:

答案 0 :(得分:2)

如果你想将匹配锚定到行的开头

grep ^xxx files...

答案 1 :(得分:2)

看来你的标题和问题都在问不同的事情。有几个人已经回答了你的头衔,我会解决你的问题。

我记得,tex评论是%。因此,我们会在该行上搜索包含\makeindex而没有%的行:

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

样品:

$ 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}%
$

答案 2 :(得分:0)

您可以通过拨打awk来执行此操作:

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