Hey,
On Mon, 15 Apr 2002, Amy Tanner wrote:
> What's the simplest way to do the following in perl:
>
> Take as input a file. Search for a line containing a particular string.
> Then append to that line.
#!/usr/bin/perl
#
# AppLine.pl - Append stuff to line.
#
$LOOKFOR="Line Has This In It";
$APPEND="Append this";
while($line=<STDIN>) {
if ($line =~ /$LOOKFOR/) {
chomp ($LINE); # remove linefeed
$line="$line$APPEND\n";
}
print $LINE;
}
----
Say you want to look in myfile.dat, you go
% cat myfile.dat | ./AppLine.pl > newfile.dat
Not tested, and you can do this better and use commanline arguments, but
it's probably close.
-Yaron
--