Grep :
grep command is one of the most frequently used UNIX
command stands for "Global
Regular Expression Print" like find, chmod or tar command in Unix. grep
command in Unix operating system e.g. Linux, Solaris, BSD, Ubuntu or IBM AIX is
used to search files with matching patterns, by using grep command in Unix you
can search a file which contains a particular word or particular pattern.
grep -v you can list down all files which doesn't contains a word i.e.
excluding files which matches a pattern
grep -c will print count of matching pattern in a file
etc. One of the popular example of grep command is to find empty files and directories
in Unix.
grep command in Unix
Example 1:
Finding relevant word and exclusion of irrelevant word. Most of the time I look for Exception and Errors in log files and some time I know certain Exception I can ignore so I use grep -v option to exclude those Exceptions
Finding relevant word and exclusion of irrelevant word. Most of the time I look for Exception and Errors in log files and some time I know certain Exception I can ignore so I use grep -v option to exclude those Exceptions
grep Exception logfile.txt | grep -v
ERROR
This grep command example will search for word "Exception" in logfile.txt and print them but since we have piped out of first grep command to second grep command which will exclude all lines which match world "ERROR". To make this grep example more concrete let's see another example, here we have a file which contains three lines as shown below :
$ cat example.txt
UNIX operating system
UNIX and Linux operating system
Linux operation system
UNIX operating system
UNIX and Linux operating system
Linux operation system
Now we want to search all lines in file example.txt which contains word UNIX but same time doesn't contain world Linux.
$ grep UNIX example.txt
UNIX operating system
UNIX and Linux operating system
UNIX operating system
UNIX and Linux operating system
Now to exclude all lines which contains Linux we will apply another grep command in this output with option -v to exclude matching word as shown in below grep command :
$ grep UNIX example.txt | grep
-v Linux
UNIX operating system
UNIX operating system
Grep command Example 2:
If you want to count of a particular word in log file you can use grep -c option to count the word. Below example of command will print how many times word "Error" has appeared in logfile.txt.
grep -c "Error" logfile.txt
If we apply this grep command on our example file to find how many lines contains word e.g. UNIX has occurred in the file :
$ grep -c
UNIX example.txt
2
Difference b/w find and grep commands:
Find command is used to search for a file with
a specific file name in a set of files.
Syntax: find .-name "fileE" -print
Ex:
find command is to used find files from the directory
ex :find . -name raju (Here raju is the file name)
find . -type f
(it will list all the files in curent directory)
find .-perm 777 (it
wil list all the files whose having permissions with 777)
--------------------------------------------------------------------------------------------------------------------------------
grep command is used to search pattren within a
file.
Syntax: grep "pattren" filename
Ex: it used to search the contents inside
the file
for suppose take a file by name raju
it contains
hat
bat
soap
SOAP
cat raju
hat
bat
soap
SOAP
now if u want see the word hat where it is
is located
grep -i hat *
grep -i hat raju
grep -a hat raju
$ find . -type grep "unix"
$ find . -type grep -i "unix"
...this command will search all file (from
all directories not only from current directory) having word unix. While if use
only command grep "unix", it will search only in current directory.
==================================================================================
To find the string in all the files
grep <string> *
Search
for a given string in all files recursively
$ grep -r "raju" *
To find the string in the files in all the sub directories
find . -type f -print | grep <string>
To know our AIX Version is http or openssl
root@nabdcweb2:/#lslpp -l | grep http
root@nabdcweb2:/#lslpp -l | grep openssl
openssl.base
0.9.8.1800 COMMITTED Open Secure Socket Layer
openssl.license
0.9.8.1800 COMMITTED Open Secure Socket License
openssl.man.en_US 0.9.8.1800 COMMITTED
Open Secure Socket Layer
openssl.base
0.9.8.1800 COMMITTED Open Secure Socket Layer
To find which java files was stuck
bash-3.2$ps -ef | grep java
bash-3.2$ps -ef | grep TEST
(TEST is an Appserver domain name)
bash-3.2$ps -ef | grep ora_
To kill the process use below command
bash-3.2$kill -9 processid
Examples
of grep commands
1)grep - globally search for a regular expression and print all lines
containing it.
tsd1 $ ls -ltr | grep "Aug" (This command will show the details of folder
which was in august month)
o/p:
-rwxrwxrwx 1 tsd1 psoft 351 Aug 8 2011
peopletools.properties
drwxrwxrwx 4 tsd1 psoft 96 Aug 8 2011
appserv
-rwxrwxrwx 1 tsd1 psoft 17 Aug 23 2011
obfuscate.txt
drwxrwxrwx 3 tsd1 psoft 96 Aug 26 2011
oradiag_tsd1
2)if you want both upper and lower case -- use option -i
tsd1 $ ls -ltr | grep -i "People" (This command will search for the
particular folder like People)
O/P:
-rwxrwxrwx 1 tsd1 psoft 351 Aug 8 2011
peopletools.properties
3) if you want zero or more of seaching .*
tsd1 $ ls -ltr | grep -i ".*java" (This command will search for java files or
folders that started with name of java)
O/P:
-rwxrwxrwx 1 tsd1 psoft 9750 Jan 4 01:00
java3.log
-rw-rw-r-- 1 tsd1 psoft 0 Jan 15 01:01
java2.log.lck
-rwxrwxrwx 1 tsd1 psoft 0 Jan 15 01:01
java2.log
-rw-rw-r-- 1 tsd1 psoft 0 Jan 15 01:01
java1.log.lck
4) The sort option +4n skips four fields (fields are separated by
blanks) then sorts the lines in numeric order.
ls -l | grep "Aug" | sort +4n
O/P:
-rwxrwxrwx 1 tsd1 psoft 17 Aug 23 2011
obfuscate.txt
drwxrwxrwx 3 tsd1 psoft 96 Aug 26 2011
oradiag_tsd1
drwxrwxrwx 4 tsd1 psoft 96 Aug 8 2011
appserv
-rwxrwxrwx 1 tsd1 psoft 351 Aug 8 2011
peopletools.properties
5) pg command - to see page by page
$ pg filename
(If any page is having more pages then this is used to view that page by
page by pressing enter key)
$To
search the word in multiple files
# cat file1 file2 file3 | grep /word search
How
to know RAM Size in AIX or LINUX
# lsconf | grep Memory
bash-3.2$ lparstat -i | grep Memory
O/P:
Online Memory : 23296 MB( In
the entire server this 22.75 GB Ram we are utilizing)
Maximum Memory : 30720 MB
Minimum Memory : 18432 MB
Memory Mode : Dedicated
Total I/O Memory Entitlement : -
Variable Memory Capacity Weight : -
Memory Pool ID : -
Physical Memory in the Pool : -
Unallocated Variable Memory Capacity Weight: -
Unallocated I/O Memory entitlement : -
Memory Group ID of LPAR : -
Desired Memory : 23296 MB( This
is allocate RAM Size of the entire server)
Target Memory Expansion Factor : -
Target Memory Expansion Size
: -
==================================================================================
To
search the word in the text file
bash-3.2# grep -r "column" /home/script.sh (It will search the
"column" word in "script.sh" file & this will show the
path of the script.sh file)
bash-3.2# grep "column" /home/script.sh (It will search the
"column" word in "script.sh" file this will not show the
path)
bash-3.2# grep -r "LAST" /home/ (It will search the
"column" word in in the entire /home folder)
Search
for the given string in a single file
Syn: grep
"literal_string" filename
grep 'word' filename
grep 'word' file1 file2 file3
grep 'string1 string2' filename
==================================================================================
To
see the environment variable for particular path:
bash-3.2$ env |grep PS_SERVDIR
O/P: PS_SERVDIR=/home/psoft/CACHE
To
search particular word in that file in unix
#grep -i "word you want to search" /home/psoft/smit.log--------------->It will search in smit.log file
Command
to check the shared memory segment
ipcs –m | grep 0x741
Check
whether shared memory segments are created again with following command.
ipcs -m | grep 0x741 (with root user)
To
know more information on grep
and find commands
$ man find
$man grep
shutdown
command examples :
Shutdown the system
and turn the power off immediately
# shutdown -h now
Shutdown the system
after 10 minutes
# shutdown -h +10
Reboot the system
using shutdown command
# shutdown -r now
Force the filesystem
check during reboot.
# shutdown -Fr now
No comments:
Post a Comment