Search This Blog

Tuesday, April 3, 2012

Advanced Filters

The Linux commands "grep" , "sed","awk"

1) grep:
The grep program searches a file or files for lines that have a certain pattern
syntax : $grep "pattern" files

The command g/re/p, which means "globally search for a regular expression and print all lines containing it

usage:
$grep "Unix" *
$ls -ltr | grep April

grep options:

-v --> print all lines that do not match pattern
-n --> print the matched line with line number
-l --> print only the names of files with matching lines
-c --> print only count of matching lines
-i --> print that match either upper or lower

Examples with above options one by one:

$ grep -v bash /etc/passwd | grep -v nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
news:x:9:13:news:/etc/news:
ldap:x:55:55:LDAP User:/var/lib/ldap:/bin/false
radiusd:x:95:95:radiusd user:/:/bin/false

$ grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
12:operator:x:11:0:operator:/root:/sbin/nologin

$ grep -l siri /etc/passwd
/etc/passwd

$ grep -c root /etc/passwd
2

$ grep -i root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

$ grep -i  env ~/.bash* | grep -v apps
/home/appluat/.bash_history:ls -lrt *.env
/home/appluat/.bash_history:env | grep CONFIG
/home/appluat/.bash_history:env | grep - i secure
/home/appluat/.bash_history:env | grep -i secure
/home/appluat/.bash_profile:# User specific environment and startup programs

$ grep -i vnc ~/.bash*
/home/appluat/.bash_history:ps -fuappluat|grep vnc
/home/appluat/.bash_history:ps -ef|grep vnc
/home/appluat/.bash_history:vncserver


--grep and regular expressions:
1) line and word anchors
Here is the example of which exclusively display the lines start with "vnc"
$ grep ^vnc ~/.bash*
/home/appluat/.bash_history:vncserver

Example of the word ending with "secure"
$ grep secure$ ~/.bash*
/home/appluat/.bash_history:cd secure
/home/appluat/.bash_history:env | grep - i secure
/home/appluat/.bash_history:env | grep -i secure



 2) Character classes:
Here it lists the entries/containing  either of u,e words
$ grep [ue] ~/.bash*
/home/appluat/.bash_history:cat backup_apps_tier.sh
/home/appluat/.bash_history:sh backup_apps_tier.sh
/home/appluat/.bash_history:cat backup_apps_tier.sh

3) wild cards
Here it lists all words starting with "s" and ending in "e" from the system's dictionary

$ grep '\<s.*e\>' ~/.bash*
/home/appluat/.bash_history:cd ./appl/fnd/12.0.0/secure/
/home/appluat/.bash_history:cd secure
/home/appluat/.bash_history:env | grep - i secure
/home/appluat/.bash_history:env | grep -i secure



 

No comments:

Post a Comment

Transportable tablespace refresh

  1.check tablespace for the user which need to refresh -------------------------------------------------------------------  SQL> select ...