Grep Examples
One of the most useful command line tools available to any unix administrator is grep , usually in combination with awk and perhaps even sed. Grep is an advanced text engine and helps you to include and excude content which matches patterns set by the user. Grep is a basic tool in all Unix like operating systems.
Example of use
1) Used to reduce output of a command, for example the command dmidecode which shows me allot about my physical hardware produces allot of output, I might only be interested in the size of the memory chips and not all of the output.
bigmac:~ neil$ ls -lr * | wc -l
338
I have 338 files in directories in my homedir, i want to find just those with jpg in the name, so i use grep:
bigmac:~ neil$ ls -lr * | grep jpg
-rw-r--r--@ 1 neil staff 421261 6 Sep 14:27 383417main_surveyor3_full.jpg
-rw-r--r--@ 1 neil staff 10053 22 Aug 16:05 ratedarticles.com.jpg
-rw-r--r--@ 1 neil staff 18567 22 Aug 16:05 pennpad.jpg
-rw-r--r--@ 1 neil staff 183706 26 Jul 19:12 melkweg2009.jpg
-rw-r--r--@ 1 neil staff 30217 6 Sep 02:27 htc-touch-diamond2.jpg
-rw-r--r-- 1 neil staff 23579 10 Aug 23:22 foxtab2.jpg
-rw-r--r--@ 1 neil staff 39129 10 Aug 23:09 foxtab.jpg
-rw-r--r--@ 1 neil staff 48555 19 Aug 20:03 belkin-skype-phone.jpg
2) Find all running processes on a macintosh:
bigmac:~ neil$ ps -ef | grep \.app
501 40 1 0 5:56.98 ?? 145:07.80 /System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow console
501 193 186 0 0:44.47 ?? 2:21.64 /System/Library/CoreServices/Dock.app/Contents/MacOS/Dock
501 194 186 0 1:13.21 ?? 18:44.06 /System/Library/CoreServices/SystemUIServer.app/Contents/MacOS/SystemUIServer
501 195 186 0 0:28.60 ?? 1:20.57 /System/Library/CoreServices/Finder.app/Contents/MacOS/Finder
501 223 186 0 0:01.29 ?? 0:01.64 /System/Library/CoreServices/AirPort Base Station Agent.app/Contents/MacOS/AirPort
Base Station Agent -launchd -allowquit
501 230 186 0 13:43.05 ?? 99:45.02 /Applications/Skype.app/Contents/MacOS/Skype -psn_0_61455
501 232 186 0 0:07.67 ?? 0:10.99 /Applications/ClamXav.app/Contents/Resources/ClamXavSentry.app/Contents/MacOS
/ClamXavSentry -psn_0_65552
501 233 186 0 2:02.22 ?? 3:04.94 /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks
/SpeechSynthesis.framework/Versions/A/Resources/SpeechSynthesisServer.app/Contents/MacOS/SpeechSynthesisServer -psn_0_69649
501 234 186 0 0:02.51 ?? 0:04.92 /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal -psn_0_73746
501 235 186 0 0:27.51 ?? 1:03.41 /Library/PreferencePanes/Growl.prefPane/Contents/Resources/GrowlHelperApp.app
/Contents/MacOS/GrowlHelperApp -psn_0_77843
501 275 186 0 1:36.09 ?? 4:06.90 /Applications/Adium.app/Contents/MacOS/Adium -psn_0_1065
22
501 893 1 0 0:18.51 ?? 0:51.91 /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framewo
rk/Versions/A/Support/mdworker MDSImporterWorker com.apple.Spotlight.ImporterWorker.501
501 916 186 0 0:25.37 ?? 0:58.75 /Applications/Preview.app/Contents/MacOS/Preview -psn_0_40
5603
501 983 193 0 2:03.61 ?? 12:09.28 /System/Library/CoreServices/Dock.app/Contents/Resources/DashboardClient.app
/Contents/MacOS/DashboardClient
501 1187 1 0 237:17.06 ?? 1181:03.43 /Applications/Firefox.app/Contents/MacOS/firefox-bin -foreground -foreground
501 3265 186 0 0:00.76 ?? 0:01.10 /Applications/Show Desktop.app/Contents/MacOS/Show Desktop -psn_0_1220906
501 4453 186 0 2:36.19 ??
29:19.05 /Applications/VLC.app/Contents/MacOS/VLC -psn_0_1851844
501 5443 186 0 0:00.90 ?? 0:01.11 /Library/Application Support/Google/GoogleTalkPlugin.app/Content
/MacO/GoogleTalkPlugin -psn_0_2216477
501 5484 245 0 0:00.00 ttys000 0:00.00 grep .app
3) Include all information from secure.log file, which includes Login or login (-i) , case insensitive , and exclude with case insensitive (-vi) Kerberos
bigmac:log neil$ grep -i Login secure.log | grep -vi Kerberos
Aug 31 21:27:01 bigmac loginwindow[41]: Login Window Started Security Agent
Aug 31 21:27:05 bigmac SecurityAgent[177]: Showing Login Window
Aug 31 21:27:19 bigmac SecurityAgent[177]: Login Window Showing Progress
Aug 31 21:27:20 bigmac SecurityAgent[177]: Login Window done
Aug 31 21:27:20 bigmac com.apple.SecurityServer[25]: Succeeded authorizing right 'system.login.console' by client '/System/Library
/CoreServices/loginwindow.app' for authorization created by '/System/Library/CoreServices/loginwindow.app'
Aug 31 21:27:20 bigmac loginwindow[41]: Login Window - Returned from Security Agent
Aug 31 21:27:23 bigmac com.apple.SecurityServer[25]: Succeeded authorizing right 'system.login.done' by client '/System/Library
/CoreServices/loginwindow.app' for authorization created by '/System/Library/CoreServices/loginwindow.app'
Sep 1 21:35:22 bigmac loginwindow[39]: Login Window Started Security Agent
Sep 1 21:35:24 bigmac SecurityAgent[164]: Showing Login Window
Sep 1 21:35:29 bigmac SecurityAgent[164]: Login Window Showing Progress
Sep 1 21:35:29 bigmac SecurityAgent[164]: Login Window done
Sep 1 21:35:29 bigmac com.apple.SecurityServer[26]: Succeeded authorizing right 'system.login.console' by client '/System/Library
/CoreServices/loginwindow.app' for authorization created by '/System/Library/CoreServices/loginwindow.app'
Sep 1 21:35:29 bigmac loginwindow[39]: Login Window - Returned from Security Agent
Sep 1 21:35:29 bigmac com.apple.SecurityServer[26]: Succeeded authorizing right 'system.login.done' by client '/System/Library
/CoreServices/loginwindow.app' for authorization created by '/System/Library/CoreServices/loginwindow.app'
Sep 12 21:37:27 bigmac loginwindow[40]: Login Window Started Security Agent
Sep 12 21:37:30 bigmac SecurityAgent[167]: Showing Login Window
Sep 12 21:43:13 bigmac SecurityAgent[167]: Login Window Showing Progress
Sep 12 21:43:13 bigmac SecurityAgent[167]: Login Window done
Sep 12 21:43:13 bigmac com.apple.SecurityServer[27]: Succeeded authorizing right 'system.login.console' by client '/System/Library
/CoreServices/loginwindow.app' for authorization created by '/System/Library/CoreServices/loginwindow.app'
Sep 12 21:43:13 bigmac loginwindow[40]: Login Window - Returned from Security Agent
Sep 12 21:43:15 bigmac com.apple.SecurityServer[27]: Succeeded authorizing right 'system.login.done' by client '/System/Library
/CoreServices/loginwindow.app' for authorization created by '/System/Library/CoreServices/loginwindow.app'





