I'm pretty sure there are literally tens of (possibly easier) ways to accomplish this, but I've always used the following snippet of bash code to search for text in files (the backslash at the end of the first line indicates continuation and should not be typed in; the two lines below are really one big command, but they are split into two separate lines so it doesn't screw up the page layout on lower resolutions):
for i in *.txt; do echo $i:; cat -n $i | \ grep -i 'search_string'; done
First, "*.txt" is expanded to a list of all files ending in '.txt'. For example, "one.txt two.txt three.txt". Then, $i is set to one of the files (in the order they are listed) at each iteration of the for loop. The commands between the 'do' and 'done' are executed for each file. The name of the current file is printed. Then, the output of `cat -n $i`, which is the contents of file $i (with each line prefixed with the line number), is piped to `grep -i 'search_string'`. This searches for the given text in file $i (ignoring case).
Of course, you can change the above snippet to search for whatever you want, in whatever files you specify. For example, to search for the string 'while' in all C files (.c) in the current directory, change "*.txt" to "*.c" and 'search_string' to 'while'. Or if you want to search in both C files and header files (.h), then use "*.c *.h". You can even use the find utility, as shown here:
for i in `find /some/path -iname "*.html" -print`; \ do echo $i:; cat -n $i | grep -i 'search_string'; done
This is exactly the same as the first command, except "*.txt" was replaced with `find ...`. The back-ticks (`) are substituted with the output of the command they enclose, in this case, the find command. The find command used here prints the absolute paths of all HTML files (i.e. ending in .html) in /some/path, including those in sub-directories, sub-sub-directories, ..etc. These files are then searched for 'search_string'.
Feel free ask questions or post what you personally use when searching for text in files.
Loading ...
August 21st, 2008 at 6:07 pm
nice article man, for the for loop you used in the first example. I think, there is many ways of doing it… putting it in a loop helps perform certain actions as well.. you could also do it with egrep or just grep
e.g
egrep ’search_string’ *.txt
or
cat *.txt|grep ’search_string’
yousef, I was wondering if you can write something about permissions in unix/linux, is it possible to create a shell script that can be ran to perform a super user tasks but allow a normal user to run it. hmm its complicated a little, but this i faced a lot at my work environment and all the alternative - walk around - solutions we used are a not very stable
August 21st, 2008 at 6:28 pm
Ah! egrep! Now that’s a much better (and shorter) way to accomplish the same thing
Though, like you said, using a loop allows me to do anything I want with the files, not just search them for text. `cat *.txt | grep ’search_string’` works, but it doesn’t tell you what file the string was found in (there’s the -l switch, but it’ll omit the actual lines that matched).
Anyway, thanks for the tip regarding egrep, I’ll certainly get myself used to `egrep -n ’search_string’ *.txt`
As for the permissions issue you’re having, I’ll do some research and write an entry about it (my initial thought is to use SUID, but I’ll have to do a little bit more research).
August 21st, 2008 at 7:50 pm
Hmm the solution we are currently using is that we created a user with super access and a user without. Then, we handed the login information for the super user to the ordinary user, but we made a script in the preloaded files (.bashrc or .profile) that when the user logs in he/she gets a menu (script written in bash).
the menu, has certain operations, for example hit one to do this and hit two to do that and 99 to exit (..etc) so the user will end up having access to the super user but he/she will not be able to go into the prompt and issue commands, all activities will be through the menu. hmm, I was thinking maybe there is an easier way to do this? hope to read something about it soon
August 22nd, 2008 at 1:25 pm
Of course, since the user can change .bashrc (and any other file for that matter since he/she has super-user privileges), he/she can easily avoid the menu and do whatever he/she pleases!
Sudo is a SUID application that seems to be perfect for what you are trying to do. Have you tried it? If you provide me with more details about what your script does, I can help you with configuring /etc/sudoers (the sudo configuration file).