Sunday, August 26, 2007

grep multiple character from string or file

This entry focus on filtering sets of character from string or from text file using grep.

Here's a quick usage of parsing and filtering multiple character using grep.

Say we have a sample testfile.txt that contains the following text data.

# cat testfile.txt
~~~~~~~~~~~~~~~~~~~~~~~~~
1 linux command line
2###########
3$$$$$$$$$$$
this is number 4
5 terminal
~~~~~~~~~~~~~~~~~~~~~~~~~

And we want to grep those lines that contain these three characters #4$

Here's how to quick do the task.

# grep '[:#4$]' testfile.txt

would give you a resulting lines of
~~~~~~~~~~~~~~~~~~~~~~
2###########
3$$$$$$$$$$$
this is number 4
~~~~~~~~~~~~~~~~~~~~~~

As you can see from above results, each lines containta one of the following chatacters:

#
4
$


This is applicable when you need to search for specific set of characters from string or file.

If you wish to grep for multiple set of strings using grep, you read it here.

0 comments:

Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy