Grep a text from a specific file extension
Answer:
Suppose you want to grep a specific code from files with a specific file extension, you can try
# grep -r --include \*.php 'error_log(' .
Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.
Grep a text from a specific file extension
Answer:
Suppose you want to grep a specific code from files with a specific file extension, you can try
# grep -r --include \*.php 'error_log(' .
Count the number of lines contain a specific string using grep
Answer:
Usually when you want to count the number of lines contain a specific string, it is easy with the grep command,
e.g.
# cat input.txt | grep foo | wc -l
But actually a more direct approach would be:
# cat input.txt | grep -c foo
How to grep for tab character(s) in Linux
Answer:
To grep for file contains tab characters, you can execute the command below:
# grep $'\t' input.txt
Only show filenames in grep command
Answer:
Sometimes you want the grep command only return the filenames that matched a pattern, you can try the following:
# grep -r -l "foo" .
The key is the -l option.
How to use sed to simulate grep -v?
Answer:
There are two methods to simulate grep -v (print out line(s) if not match) in sed.
# sed -n '/regexp/!p'
or
# sed '/regexp/d'