Thursday, February 28, 2008

HowTo: Extract Lines That Started With Matching String Using Grep

From recent post of displaying the first occurrence of line that starts with a specified matching string, now instead of the first occurrence, here's a way of stripping out all matching lines that start with the given matching string.

Reproducable Approach
Say for example, we have a text file with the below contents and we need to extract those lines shown in BOLD.

# cat testfile.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
my linux sysad blog
daily linux sysad blog
linux systems admin blog
systems admin blog
systems admin blog matching line 2
systems admin blog matching line 3
fedora systems admin blog matching line 4
linux systems admin blog matching line 5
pinoy systems admin blog matching line 6
blogging fedora sysad blog
fedora syad blog
distrowatch and linux distributions
linux systems admin
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Noticed above that the words "systems admin blog" appeared a lot of times from different lines. Our objective is to extract ALL the occurring lines that exactly started with the given query string, which is systems admin blog .

Here's how to extract line occurrences that starts with the word "systems admin blog" words. Simply

# grep "^systems admin blog" testfile.txt

Alternatively, using linux pipe

# cat testfile.txt | grep "^systems admin blog"

output:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
systems admin blog
systems admin blog matching line 2
systems admin blog matching line 3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The above commands comes in useful when you need to extract lines that starts with the given query string from a thousand lines of text files. Simple but useful when managing large text file contents.

Take note that you'll get a different results if you do

# grep "systems admin blog"

That is all folks, enjoy.

0 comments:

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