简体   繁体   English

在某个 position 处查找具有某个字符的文件中的所有行

[英]Find all lines in a file with a certain character at a certain position

I want, that my bash script counts the lines of a.txt file, which starts with an '#'.我希望我的 bash 脚本计算以“#”开头的 a.txt 文件的行数。

grep -o "#" $FILE | wc -l

I know how to count all lines with a '#' in it but I dont know how to implement the condition, that the '#' needs to be at the start of the program.我知道如何计算所有带有“#”的行,但我不知道如何实现条件,即“#”需要在程序的开头。

The Input are other bash scripts.输入是其他 bash 脚本。 I want to count how many comments are in the different bash scripts.我想计算不同的 bash 脚本中有多少评论。 Than I want to divide the output with the total numbers of lines in the txt file.比我想将 output 与 txt 文件中的总行数相除。 So I have a percentage of how many lines of the bash script are comments.所以我知道 bash 脚本中有多少行是注释。 Like if I have 10 lines of comments in a 100 lines code, 10% of the code are comments.就像如果我在 100 行代码中有 10 行注释,那么 10% 的代码是注释。

I also want to scan these lines starting with a '#' after a certain word like "Hello" and count how many lines having this word.我还想在“Hello”之类的某个单词之后扫描以“#”开头的这些行,并计算有多少行包含这个单词。 Hope someone can help me there too:).希望有人也可以在那里帮助我:)。

You can count all the lines that start with a # by doing:您可以通过以下方式计算所有以#开头的行:

grep -c "^#" $FILE

Or after a certain word (with 0 or more other characters between) by doing:或者在某个单词之后(中间有 0 个或多个其他字符),执行以下操作:

grep -c "Hello.*#" $FILE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM