grep as a filter for your raw text based data

August 15th, 2006 mysurface Posted in cat, Common, grep, pipeline, ps, Text Manipulation | Hits: 40019 | 3 Comments »

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.

3 Responses to “grep as a filter for your raw text based data”

  1. lokesh charora Says:

    print the lines, where line start with ‘*’ and ‘^’ character.

  2. How could I use this pipe to search for a string in any file in a tree (e.g. my home directory down)?

    ihatespam

  3. I would like to thnkx for the efforts you have put in writing this web site. I am hoping the same high-grade website post from you in the upcoming also. In fact your creative writing skills has encouraged me to get my own blog now. Actually the blogging is spreading its wings fast. Your write up is a great example of it.

Leave a Reply