Everyone knows (and loves) grep, I’ve also wrote an article on it, but today we’ll see other small utility that have some things in common with it.
In particular I’ll show you: pgrep, grepcidr, ngrep, pdfgrep and taggrepper.
All are command line tools to be used with your favorite shell, I’ll show you some example for every command.
pgrep
pgrep is become a common command for the shell (or so I think), it looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout.
Basic Usage
pgrep command_name |
Finds the process you are looking for minus the pgrep itself.
example :
pgrep ssh 378 1854 |
The output will give some PIDs, this is the same as using the command:
ps -ef |grep ssh |grep -v grep | awk '{print $2}' 378 1854 |
Please note that all the criteria have to match. For example,
pgrep -u root sshd |
will only list the processes called sshd AND owned by root. On the other hand,
pgrep -u root,daemon |
will list the processes owned by root OR daemon.
grepcidr
grepcidr can be used to filter a list of IP addresses against one or more Classless Inter-Domain Routing (CIDR) specifications, or arbitrary networks specified by an address range. As with grep, there are options to invert matching and load patterns from a file. grepcidr is capable of comparing thousands or even millions of IPs to networks with little memory usage and in reasonable computation time.
Basic usage
grepcidr pattern file |
Search in file the address you put in the pattern
Each pattern, whether on the command line or inside a file, may be: CIDR format a.b.c.d/xx IP range a.b.c.d-e.f.g.h Single IP a.b.c.d
Examples:
grepcidr -f ournetworks blocklist > abuse.log |
Find our customers that show up in blocklists
grepcidr 127.0.0.0/8 iplog |
Searches for any localnet IP addresses inside the iplog file
grepcidr "192.168.0.1-192.168.10.13" iplog |
Searches for IPs matching indicated range in the iplog file
grepcidr -f list1 list2 |
Cross-reference two lists, outputs IPs common to both lists
ngrep
ngrep strives to provide most of GNU grep’s common features, applying them to the network layer. ngrep is a pcap-aware tool that will allow you to specify extended regular or hexadecimal expressions to match against data payloads of packets.
Basic Usage
If it is launched with no options, ngrep will detect and print all your network traffic.
To be more useful you can use the -d parameter to specify the network adapter and the parameter -p to indicate the port.
Examples
ngrep -d en0 port 80 |
This command will show all the traffic in transit to port 80 on eth0.
ngrep -d any 'error' port syslog |
Monitor any network-based syslog traffic for the occurrence of the word “error”. ngrep knows how to convert service port names (on UNIX, located in “/etc/services”) to port numbers.
pdfgrep
Pdfgrep is a tool to search text in PDF files. It works similar to grep, it support search for regular expressions, filename output,
page number output, optional case insensitivity.
Basic usage
pdfgrep [grep options] [file ...] |
Example
#pdfgrep "acer" acer1400.pdf Original version: http://personal.digital.it/prodotti/S1018490/acer-aspire-1400lc.html/scheda.html |
You can use the -c option to count the number of occurrences or -n to print the number of pages that contain the word.
taggrepper
taggrepper is a small tool written to “grep” tags of media files. Currently, it can be used to match some or any tags of MP3 files, Ogg Vorbis and FLAC files against specified regular expressions. It supports recursive searches as well.
Basic Usage
Some of the main options are:
--display-title display title tag of matching files --display-artist display artist tag of matching files --display-album display album tag of matching files --display-year display year tag of matching files --display-genre display genre tag of matching files --display-comment display comment tag of matching files --display-track display track tag of matching files --display-composer display composer tag of matching files --display-orig-artist display orig-artist tag of matching files --display-copyright display copyright tag of matching files --display-url display url tag of matching files --display-encoded-by display encoded-by tag of matching files
Examples
taggrepper -y 2009 -r *.mp3 |
Search within all the mp3 files the tag 2009 as year.
taggrepper -r -a Rahman ~/AV/Music_collection/ |
Find all songs in the ~/AV/Music_collection/ which have “Rahman” in their “Artist” tag.
Conclusion
I hope you liked all or some of these 5 small utility, they can used on the terminal and used in pipe ( | ) to concatenate multiple commands.
You can also do these grep commands on your web hosting server if you have access to SSH. Believe it or not even some cheap web hosting companies offer access to SSH, but most of the time you need to ask for it.
Popular Posts:
- None Found
There is also a command called grepmail that will pull an email out of an mbox file that matches
the search criteria.
See also ack at betterthangrep.com. It’s optimized for programmers.
Thanks Andy, i’ll check it.
Is not amazing, only is Linux
> ps -ef |grep ssh |grep -v grep | awk ‘{print $2}’
The will miss processes (eg. ssh grep@mybox) and is inefficient.
Use
ps -ef | grep [s]sh
instead
Thanks Roberto.
There also the zgrep command , it search pattern inside gz tar zip files…