Counting the number of strings in a file is actually looking for stones in the sand. Some people will mark the stones (grep) after seeing them ), then remember how many tags you have made; some After seeing the stone, people dug it (TR) and finally counted how many stones they dug. Some people jumped it (awk) after seeing the stone ), then count the number of jumps you have made. This is my file. [Code] [Root @ bzhou test] # Cat File Hafsdha Hahafsdfsdhaha Haha [/Code] I want to match the 'hahaha' string 1.-O options of grep [Code] [Root @ bzhou test] # grep-C 'hahaha' File 2 [/Code] In the beginning, the-C option was used, but-C can only count one row. If a row contains multiple matched strings,-C will be powerless. This is correct [Code] [Root @ bzhou test] # grep-O 'hahaha' file | WC-l 3 [/Code] 2. awk Thanks to blackold ON Cu. [Code] [Root @ bzhou test] # awk-V rs = 'hahaha'' 'end {print -- Nr} 'file [/Code] -V is used to set the value of a variable. RS is the record separator and the default value is new line (/n). That is to say, awk reads data by one row, however, when RS is set to 'hahaha', data is read by 'hahaha'. nr is the number of read records, and N records are separated by n-1 delimiters, so it's -- Nr. 3. tr Strictly speaking, TR cannot match a string and can only match a single character. Match the number of 'H' in this file. [Code] [Root @ bzhou test] # tr-CD 'H' <file | WC-C 8 [Root @ bzhou test] # grep-O 'H' file | WC-l 8 [/Code] -D can delete a character. If only-D can output the string after the specific character is deleted, but-C can be reversed. This is the character to be deleted. Then you can use WC-C to calculate the number of characters. This is on the CU.ArticleI am actually a porter. Http://bbs2.chinaunix.net/viewthread.php? Tid = 1441850 & extra = & page = 1 |