How to get no. of lines count that matches a string from all the files in a folder
You can pipe your grep
with wc -l
to get count of lines containing your keyword:
grep -r "string_example" . | wc -l
Unix: How can I count all lines containing a string in all files in a directory and see the output for each file separately
Why not simply use grep -c
which counts matching lines? According to the GNU grep manual it's even in POSIX, so should work pretty much anywhere.
Incidentally, your use of -o
makes your commands count every occurence of the string, not every line with any occurences:
$ cat > testfile
hello hello
goodbye
$ grep -o hello testfile
hello
hello
And you're doing a regular expression search, which may differ from a string search (see the -F flag for string searching).
How can I count all the lines of code in a directory recursively?
Try:
find . -name '*.php' | xargs wc -l
or (when file names include special characters such as spaces)
find . -name '*.php' | sed 's/.*/"&"/' | xargs wc -l
The SLOCCount tool may help as well.
It will give an accurate source lines of code count for whatever
hierarchy you point it at, as well as some additional stats.
Sorted output:
find . -name '*.php' | xargs wc -l | sort -nr
Count all occurrences of a string in lots of files with grep
cat * | grep -c string
How can I find and count number of files matching a given string?
You can use
find / -type f -name 'foo*' | wc -l
- Use the single-quotes to prevent the shell from expanding the asterisk.
- Use
-type f
to include only files (not links or directories). wc -l
means "word count, lines only." Sincefind
will list one file per line, this returns the number of files it found.
How to count lines in a document?
Use wc
:
wc -l <filename>
This will output the number of lines in <filename>
:
$ wc -l /dir/file.txt
3272485 /dir/file.txt
Or, to omit the <filename>
from the result use wc -l < <filename>
:
$ wc -l < /dir/file.txt
3272485
You can also pipe data to wc
as well:
$ cat /dir/file.txt | wc -l
3272485
$ curl yahoo.com --silent | wc -l
63
get count of the word in all files
Try something like this:
find . -type f | xargs -n1 grep "Hello" -c
Adding -type f
to find
ensures that it only returns files, not directories. Adding -n1
to xargs
makes it so that every file returned by find
gets its own invocation of grep
, so that you can get a per-file count. The -c
argument to grep
returns the count of matches instead of every match.
The above expression will count the number of lines that have 'Hello' in them. If you need the total number of Hellos, instead of just the number of lines that have Hello in them, you'll need to do something more sophisticated. You can use the -o
option on grep to just print the matching section of a line, and then combine that with wc -l
to get the number of total occurrences.
Related Topics
Xkb: How to Convert a Keycode to Keysym
Extract Text Between Two Strings Repeatedly Using Sed or Awk
Split Delimited File into Smaller Files by Column
Run a Command Conditionally with Netcat and Grep
G++ Conio.H: No Such File or Directory
Cmake Set Environment Variable
How to Configure a Specific Ip in Prometheus Yml Configuration File
Why Is Execution Time of a Process Shorter When Another Process Shares the Same Ht Core
How to Set Emacsclient Background as Emacs Background
Dlopen Failed: Cannot Open Shared Object File: No Such File or Directory
Best Text Search Engine for Integrating with Custom Web App
Coqide 8.5: No Syntax Highlighting on Linux
Temporarily Prevent Linux from Shutting Down
How to Do a One Way Diff in Linux
Can Any One Say How to Install the Slack in Ubuntu14.04 and While Trying This Was the Error