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
该-c参数是缩写,--count它告诉 grep 打印出包含该字符串的行数。
--count