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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple greps

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I want to search a file for certain text and delete certain text from a file. I know you can do it with grep somehow but I'm not sure how. Can someone help?
example 1: search for string "dog" in file animals, if it's there, do A, else do B
example 2: delete string "dog" from file animals, if it's successful, do C, else do D
Thanks in advance!
 
why not just do it with some perl? --Derek

"Fear not the storm for this is where we grow strong."
 
Here is a quick example:

[tt]
$db = 'basicdata.txt';
open(DAT, "$db") or die ("Can not open $db: $!\n");
@data = (<DAT>); #Read file into an array.
close(DAT);
open(DAT, &quot;>$db&quot;) or die (&quot;Can not open $db: $!\n&quot;);
foreach $line (@data)
{
if ($line =~ /florida/)
{
$line =~ s/florida/jacksonville/;
}
elsif ($line =~ /atlanta/)
{
$line =~ s/atlanta/dallas/;
}
print DAT $line;
}
close(DAT);[/tt] =================
Bad Company Music
=================
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top