Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

K Shell Script grep.... 1

Status
Not open for further replies.

SpongeBob1

Programmer
Mar 1, 2002
49
US
Hi,

I wish to open a file using a K Shell script & search & return a particular word in the file. Does anyone have any ideas how this might be done?

Thanks.
 
Do you mean search and return or search and replace?

Search and return

Create the following file

#!/usr/bin/ksh

echo "Enter Dir containing File"
read dir
echo "Enter filename to search"
read file
echo "Word or phrase to search for"
read word

cat $dir/$file|grep $word

To search and replace

Create the following file

call this one ask.sh

#!/usr/bin/ksh

echo "Enter Dir containing File"
read dir
echo "Enter filename to search"
read file
echo "Word or phrase to replace"
read word
echo "Replace with"
read replace

sed "s/$word/$replace/g" $dir/$file > new.txt

remember to chmod 700 them all to give yourself read write execute permissions.

Mike
--
| Mike Nixon
| Unix Admin
| ----------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top