grep as a filter for your raw text based data
August 15th, 2006 mysurface Posted in Common, Text Manipulation, cat, grep, pipeline, ps | Hits: 15029 |
grep is a very useful and simple tools for a unix user to extract info from text based data files, command output, etc. grep always apply together with pipeline to simplified the data output we obtain to form the information which is presentable.
For example, ps aux will shows info about the current running processes. But we are only looking for a specific process, therefore we simplified the output from ps by setting a matching filter, such as
ps aux | grep gaim
This will leads the output line of ps aux which have the string “gaim” display to us.
Sometimes, we want to filter out the line which match the keywork we specified, and display the rest of the line. For example, I would like to filter out the comments in the iptables configuration files, which is the line with “#”, I can do this with specified -v:
cat /etc/sysconfig/iptables | grep -v "#"
First of all we read and display all iptables content to a buffer (standard output), before displaying to user, we pass the output through the pipeline and apply the filter using grep.
The last example I am going to show here is searching for text files with keyword and also display the line number where if match the keyword we specified.
grep "keyword_we_specified" * -n
* means any files in a particular directory and -n ask it to show with line number.
Live Chat!







March 1st, 2009 at 7:51 pm
print the lines, where line start with ‘*’ and ‘^’ character.
July 28th, 2009 at 2:50 am
How could I use this pipe to search for a string in any file in a tree (e.g. my home directory down)?
ihatespam