2012-02-01 20 views
1

.texファイルにmakeindexを実行するかどうかをチェックする小さなbashスクリプトを作成しています。コマンドがコメントアウトされている場合、MakeIndexの実行は実行されません。ファイルのチェックに'xxxx 'で始まる行が含まれていますが、' yxxx 'ではありません。

ファイル、たとえばsource.texに行があることを確認するにはどうすればよいですか?

私はgrepが必要だと知っています。しかし、正規表現とbashスクリプトにはかなり新しいです。

答えて

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

あなたが行の先頭にマッチを固定したい場合は、それはあなたがawkに一度のコールでこれを行うことができます

grep ^xxx files... 
+0

、ありがとうございました。これを '[[$ myvar =〜^ \ * *]]'ファッションで使用することは可能ですか?ライン全体を反復する必要はありませんか? – Minustar

+0

私は '[[" $ myvar "=" $ {myvar#prefix} "]]'のような何かをします。 - 上記接頭辞を切り捨てた変数が同じであれば、そのような接頭辞はありません。 –

+0

ところで、なぜあなたは全体の行を繰り返し処理する必要がありますか?そして、「全体の行」は何を意味しますか? –

0

だ:あなたのタイトルと質問が異なるものを求めているようだ

#!/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 
関連する問題