User Tools

Site Tools


misc:perl

Suchen und Ersetzen

  FOR [/R] [c:\dir] %F IN (*.java) DO ''perl_aufruf'' %F

  perl -p -i.bak -w -e "s/old/new/" <filenames>

In Batchdateien anstelle von %F %%F verwenden.

Beispiel:

for /R . %F in (Ro?t) do c:\programme\perl\bin\perl -p -i.bak -w -e "s/guest\@10/myuid\@10/" "%F"

Kurzprogramm command-line

perl -w -n -e 'if (/.* comp=\"([a-zA-Z0-9_.]+)\".*/) { print "$1\n"; }'

Beispiel

 my ($section, $key, $val);
 $section = "<null>";

 while (<>) {

	# Delete BOTH leading and trailing whitespace from each line
	s/^\s*//;
	s/\s*$//;

	# Delete blank lines
	next if /^$/;

	# Delete comments
	next if /^;.*$/;

	# invalid section
	if (/^\[.*/) {
		$section = "<invalid section>";
	}

	# Save [[Section] lines, dont print
	if (/^\[([\w ]+)\].*$/) {
		$section = $1;
		next;
	}

	# Key=Value --> [Section] Key=Value
	if (/([\w ]+)=(.*)$/) {
		$key = $1;
		$val = $2;

		if ($key =~ /member[[[:digit:]]+/i) {
			$key =  "member999";
		}

		print "[$section] $key=$val\n";
		next;
	}

	print "!!! unrecognized line in INI-File: $_\n";
 }

Programmaufrufe einbetten

#!/usr/bin/perl -w
#
# Holt Suchseite von www.bing.com, ermittelt Background-Image und lädt das Image.
#

my $bing_url = "http://www.bing.com";
my $user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.15) Gecko/2009101601 Firefox/3.0.15 (.NET CLR 3.5.30729)";
my $home_dir = "/home/goeblco/bing";
my $work_dir = "$home_dir/tmp";
my $img_dir = "$home_dir/images";
my $timeout = 30;
my $index_html = "index.html";
my $log_file = "$home_dir/log/download.log";
my $header1 = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
my $header2 = "Accept-Language: de,en;q=0.5";
my $referer = ""; #"--header=Referer: $bing_url/";

my $wget_cmd = "wget --no-verbose \"--user-agent=$user_agent\"" .
		" \"--header=$header1\" \"--header=$header2\" --no-cookies" .
		" --timeout=$timeout --append-output=$log_file";

chdir $work_dir or die "Working directory $work_dir existiert nicht.";
		
die "$index_html existiert schon." if -e $index_html;
my $cmd = "$wget_cmd $bing_url/";
my $status = system ($cmd);

die if $status != 0;

open (SOURCE, "< $index_html") or die "Datei $index_html nicht vorhanden.";

my $imgurl = 0;

while (<SOURCE>) {

	if (/#bgDiv{background-image:url\((.*?)\)}/) {
		$imgurl = $1;
	}
	elsif (/g_img={url:'(.*?)'/) {
		$imgurl = $1;
		$imgurl =~ s/\\//g;	
	}
}
close (SOURCE);


if ($imgurl) {
	$cmd = "$wget_cmd $referer --directory-prefix=$img_dir $bing_url$imgurl";
	#print $cmd . "\r\n";
	system ($cmd);
}

unlink $index_html;

open(SENDMAIL, "|/usr/sbin/sendmail -oi -t -odq") or die "Can't fork for sendmail: $!\n";
print SENDMAIL << "EOF";
From: Heinrich Goebl <xyz\@xyz.com>
To: Heinrich Goebl <xyz\@xyz.com>
Subject: bing download $imgurl

Download erfolgreich.
EOF
close(SENDMAIL);

Hashes

my %st;
while (<>) {
    # ... $category = ...
    my $cnt = $st{$category};
    if (!$cnt) {
        $cnt = 1;
    } else {
        $cnt++;
    }
    $st{$category} = $cnt;
}

while (($key, $value) = each(%st)) {
    print $key."\t".$value."\n";
}

rename filenames (script by Larry Wall)

#!/usr/bin/perl
#
# rename script examples from lwall:
#       rename 's/\.orig$//' *.orig
#       rename 'y/A-Z/a-z/ unless /^Make/' *
#       rename '$_ .= ".bad"' *.f
#       rename 'print "$_: "; s/foo/bar/ if <stdin> =~ /^y/i' *

$op = shift;
for (@ARGV) {
     $was = $_;
     eval $op;
     die $@ if $@;
     rename($was,$_) unless $was eq $_;
}

Example:

../bin/rename.pl 's/hprichbg\?p\=rb%2F//' hprichbg\?p\=rb%2F*
../bin/rename.pl 's/index.html\?p\=//' index.html\?p\=*

misc/perl.txt · Last modified: 2012/11/24 11:42 by hgoebl