匿名用户
1级
2009-09-22 回答
基於linux 的精神,这个该用sed来做吧,假设文件这样
nc10@your-5554c55be4 ~
$ cat bin/funfacts.pl
#! /usr/bin/perl
#funfacts.pl by twfccc@gmail.com, public domain
use strict;
use warnings;
use LWP::Simple;
my $url = "http://www.cs.cmu.edu/~bingbin/";
my $fact = get($url) or die "Could not open $url: $!\n";
$fact =~ s/]+>//g;
$fact =~ s/>+//g;
my @fact = split(/\n/, $fact);
my $size = @fact;
srand();
my $random = int( rand() * $size * $$ ) % $size;
my $x = $fact[$random]?$fact[$random]:$fact[$random + 1];
print "\n$x\n\n";
我想由my $url 到文件末复制到另一文件, 这样
nc10@your-5554c55be4 ~
$ sed '/\$url/,$!d' bin/funfacts.pl
my $url = "http://www.cs.cmu.edu/~bingbin/";
my $fact = get($url) or die "Could not open $url: $!\n";
$fact =~ s/]+>//g;
$fact =~ s/>+//g;
my @fact = split(/\n/, $fact);
my $size = @fact;
srand();
my $random = int( rand() * $size * $$ ) % $size;
my $x = $fact[$random]?$fact[$random]:$fact[$random + 1];
print "\n$x\n\n";
nc10@your-5554c55be4 ~
$ sed '/\$url/,$!d' bin/funfacts.pl > somejunk
nc10@your-5554c55be4 ~
$ cat somejunk
my $url = "http://www.cs.cmu.edu/~bingbin/";
my $fact = get($url) or die "Could not open $url: $!\n";
$fact =~ s/]+>//g;
$fact =~ s/>+//g;
my @fact = split(/\n/, $fact);
my $size = @fact;
srand();
my $random = int( rand() * $size * $$ ) % $size;
my $x = $fact[$random]?$fact[$random]:$fact[$random + 1];
print "\n$x\n\n";
nc10@your-5554c55be4 ~
$
这样比用vim/vi手动快多了,且不会错,对吗?呵呵