Find programmers and grapic design experts at ScriptLance.com
<? DEW-CODE.COM  
Dew-Code
Welcome, Guest
Please Login or Register.    Lost Password?
Post your BASH related questions or observations here. Code snippets are always welcome.
Go to bottom Post Reply Favoured: 0
TOPIC: GREP is your friend
#257
Dewed (User)
Administrator
Posts: 220
graph
User Offline Click here to see the profile of this user
Dew-Code.com
GREP is your friend 5 Months, 3 Weeks ago Karma: 7  
If you are a Windows© user you have no doubt used it's built in search function. If you are like me, you've also set it to look for certain words inside files. Well, BASH shell users have had this all along, dating back to the dark ages in computer history (like the early 70s with teh first incarnations of Unix)

The GREP command may have an odd name, but it's a powerful ally when it comes to finding stuff inside files. Let's say you have 4 dozen html files, and one of them has a typo in it. In your haste you misspelled "Home" "Hoem" and now the real quandry, you aren't sure which file the error is actually in..

Well get to your trusty BASH command prompt and CD to your www directory, and search all the HTML files for the typo

Code:

cd www grep hoem ./*.html
If the typo is found it will show you the filename(s) it was found in. Lets say you want to see 2 preceding lines above the typo, use the -b flag like this
Code:

grep -b2 hoem ./*.html
That's b as in Before. Simple right Maybe you also want to see the 2 lines after the typo.. You guessed it! The -a flag
Code:

grep -b2 -a2 hoem ./*.html
Of course you can search for anything, in any type of file, but the power doesn't stop there. Nope, note even close. Because almost any bash command can have it's output piped to another command, even the same command again... Lets do the same search as above, but this time pipe the output to a 2nd grep command wich uses the inverse search operation. The inverse search operation matches lines that DO NOT contain the term. So you want to find the typoed word hoem, but ignore matches that also have the word "typo" in them..
Code:

grep -b2 -a2 hoem ./*.html |grep -v typo
Now you would think that uses the -1 flag but, it uses -v for inVerse This is literally just the tip of the iceberg and doesn't even mention using regular expression, which really unleash GREP's power, but hey, you just want to find your way hoem right?
 
Report to moderator   Logged Logged  
 
Last Edit: 2008/07/20 09:19 By Dewed.
 
Nothing to it but to Dew it !
Dew-Code.com
  The administrator has disabled public write access.
      Topics Author Date
    thread link
GREP is your friend
Dewed 2008/05/31 15:12
Go to top Post Reply
Powered by FireBoardget the latest posts directly to your desktop

Newsflash

Sign up for PayPal and start accepting credit card payments instantly.
Copyright Dew-Code 2008