Wednesday, August 4, 2010

Text searching in linux

This command would search for "a string" and return 3 lines before and 4 lines after. To give it a test create a file called testfile.txt and put a bunch of random stuff in it and on a few lines put "a string" without the quotes.




grep -B3 -A4 "a string" testfile.txt >grepout.log



This would output the results to grepout.log, but you can remove the >grepout.log part to have it right on the screen. Outputting to a file is not a bad idea when searching huge amounts of files since you will probably get lot of matching stuff so afterwards it will be easier to sort through.



Here's how you'd do the same thing, but instead of searching testfile.txt you want to search all the files in /etc.



grep -R -B3 -A4 "a string" /etc/*



This is surprisingly pretty quick too, here's some of what it got on my system. Just part of what appears to be a configuration file. From this point on I know that "a string" is in that file so I can vim to it and do a search (using /a string in the vim command line) and voila, found a string, without having to manually check in each file. Funny how it's talking about regular expressions, since we'll be looking at those later.

Grep with commands


Grep is not only limited to searching for files, you can also limit command output to a specified string. For example



locate spam
grep ^/etc/

Grep with commands


Grep is not only limited to searching for files, you can also limit command output to a specified string. For example



locate spam
grep ^/etc/


The locate command is used to locate files on the hard drive. So typically locate spam would list every single file that matches spam, but that's a huge list, so using a command such as the one above will limit to /etc folder. The ^ tells it that it has to start with that. So /data/etc/spam would not match. Without the ^, it would match.



You can use grep with dir as well. If you're looking for the hosts file but don't remember if it has an extension, or if it has an s then you could use this command:



dir
grep "host"



Returns:

host.conf pam.d vsftpd.conf

hosts pam_smb.conf vsftpd.ftpusers

hosts.allow pango vsftpd.user_list

hosts.deny paper.config warnquota.conf
 
Remember, it goes by line, so this is why pam.d and vsftpd.conf and other files are listed, as long as "host" is somewhere on that line.




Another command you can use grep with is top. Top is used to show system usage (sort of like the task manager in windows) and it updates every few seconds.



So issuing top
grep "httpd" would execute top, but only show the lines which httpd is in, so every few seconds, a new line would be printed, if, and only if, httpd is in the top section of cpu usage. This is especially good if you are monitoring a program that is using too much resources since every few seconds it will print a new line with the usage info. And if it stops printing, then you know the program ceased using up enough cpu to apear in the top list.



Grep can be used in conjunction with any command, as far as I know and it has many features not mentioned so far so the next page will simply show some examples of advanced uses of grep
 
 
http://www.iceteks.com/articles.php/grep/1
 
 
 
 
Linux: Find text in a large number of files


retweetIf you need to find a string in a file, you would typically use:

grep -H "string to find" filename.ext

However, grep doesn’t handle a large number of files well. If you specify grep "string" * or even grep "string" `find ./`you may find yourself facing this error:

bash: /bin/grep: Argument list too long

If you need to search for a string in a lot of files then you can use a simple bash script to do the searching for you.



In this sample, I am looking for a string “sample string” in a directory named “./sample/”:

for i in `find ./sample/`; do grep -H "sample string" $i; done

This uses the find command to do the searching. It actually returns a list of filenames, which we can then grep one-by-one. The -H option tells grep to let us know the filename it found the string in so we can go right into that file to find the location of it.



So if you are a normal user and that you are facing the "missing files" problem in Linux, don't worry, I will show you the most common methods in solving this issue:




Find files that contain a text string



grep -lir "text to find" *



The -l switch outputs only the names of files in which the text occurs (instead of each line containing the text), the -i switch ignores the case, and the -r descends into subdirectories.



Find files containing search terms on Ubuntu



To find files containing keywords, linux has a powerful command called grep, which you can use to find the lines inside any file or a list of files.



grep -i -n 'text to search' *



List files containing text



Used to recursively search a directory for files containing a string, output the names of the files and the line number. This will search all regular files in for.



grep --with-filename --line-number `find -type f`

No comments:

Post a Comment