Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Commands & Shells

A simple text string search script

Submitted by on December 13, 2007 – 1:52 pm 4 Comments

Sometimes you need to locate a text string in various configuration files. You don’t know which files. You can just use “grep *”, but the output is rather messy and you can only search in the current directory level. The script below uses “find” to locate all files under the specified directory. The script will only search ASCII text files, thus saving you time by not scanning through huge binaries. The output is neat and easy to read.

#!/bin/ksh

if [ -f /tmp/search.out ]
then
	rm /tmp/search.out
	touch /tmp/search.out
else
	touch /tmp/search.out
fi

echo "Enter search string: c"
read STR

if [ ! $STR ]
then
	echo "You must provide a search string."
	exit 1
fi

echo "Enter directory to search: c"
read DIR

if [ $DIR ]
then
	if [ ! -d "$DIR" ]
	then
		echo "$DIR does not exist."
		exit 1
	fi
else
	echo "You must specify search directory."
	exit 1
fi

i=1
find "$DIR" -type f -print | while read FILE
do
	if [ `file $FILE | grep -ci ASCII` -eq 1 ]
	then
		clear
		echo "Searching file ${i}: $FILE"
		cat /tmp/search.out
		if [ `fgrep -c "$STR" "$FILE"` -gt 0 ]
		then
			echo "<$STR> found in $FILE" >> /tmp/search.out
		fi
		(( i = i + 1 ))
	fi
done

clear
echo "Results in $i files in $DIR: "
echo ""
if [ -f /tmp/search.out ]
then
	more /tmp/search.out
else
	echo "Nothing found."
fi

Print Friendly, PDF & Email

4 Comments »

  • Boo Cookie says:

    Hi, I’m trying to develop a script that will, given the friend id from a form input, get the html of the persons myspace profile, and serch through it to get their default picture, and display name. What could I use to do this? And if it’s simple enough, could someone develop this for me?

    Thanks,
    Tyler

  • Goe122 says:

    I want to make a site that people send me stuff they are selling ( its for my school project). I want to:

    1. Be able to put this stuff ( whether automatically or manually) into a database
    2. I want it to be able to be searched by 6 things (type of stuff, date added, dorm etc.)
    3. I the search to have filters so people can find the right stuff.

    I have looked at database tutorials but a lot of them don’t show how to have a decent search module. If someone can give me a good tutorial(s), or can tell me how to do simple ( I know this isn’t simple, but at least easy directions,), or a site that can allow you to make your own database-type situation ( with search as well). A custom site would be really good ( like Pipes!), but any help will do.

    No one sentence replies unless it is the best sentence you ever wrote. Give me some content.

  • Rishi says:

    I have a string of letters. (They happen to be DNA, but that’s irrelevant.) For analysis purposes, I need to replace the information at some of the sites. I need to do this based on their position, not the information in that position.

    I also need to ignore differences at other sites, so I can’t do a simple “wild card search and replace.” I remember using awk scripts to do something like this about a decade ago, but I don’t remember how.

    For example, if you wanted to take the following strings,

    AAAAATAAAGAAAA
    and
    AAAATTAAAGAAAA
    and
    AAAAATAAACAAAA

    and in each case, turn the 10th letter into an “N,” without changing any of the other letters, how would you do that? (The actual files are about 500 characters long, and will have about 30 position-specific replacements.)

    I’m also willing to use a web tool or other editor….MacOS or Unix terminal on a Mac.

  • Kevin says:

    I have 50 some html files.
    I’m looking for a utility which allows me to search in those files, locate a piece of information in there and then delete that row in the html.
    The best so far in searching tens of text files is the Actual Search & Replace at http://www.divlocsoft.com/.
    The limitation there is that I can’t delete the entire row. It only allows me to delete the word or phrase.
    Does anyone have any idea which utility tool might allow me to perform my desired task?

Leave a Reply

%d bloggers like this: