Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
/home/someuser/sometext.txt という場所にファイルがあります。特定の文字列が出現する行数を数えたい。Linuxコマンドラインからそれを行う方法は何ですか?
スイッチを使用したgrep-cが必要です:
-c
grep -c "pattern" /home/someuser/sometext.txt
awk を使用した代替ソリューション:
awk '/regex/{c++}END{print c+0}' /home/someuser/sometext.txt
grepコマンドを探しています。これが基本的なチュートリアルです。ファイル内の文字列検索に非常に便利です。また、正規表現もサポートしています。
次のようなことをするようです。
grep -c "mystring" /home/someuser/sometext.txt
引数は forの-c略で--count、文字列を含む行数を出力するように grep に指示します。
--count