Thread perl one liner : string suchen und ersetzen wert (unix) (17 answers)
Opened by bora99 at 2012-05-08 15:07

FIFO
 2012-05-09 07:13
#158236 #158236
User since
2005-06-01
469 Artikel
BenutzerIn

user image
2012-05-08T21:15:13 bora99
würdet ihr den perl one liner öfter aufrufen pro eintrag oder dies
mir einem perl aufruf abdecken, wie ... 's/eintrag1/.../; s/eintrag2/.../;


Ehrlich gesagt würde ich zumindest im produktiven Umfeld ein kleines Skript schreiben und das irgendwo archivieren, so dass die Änderungen später noch nachvollzogen werden können. Z.B. so (ungetestet):

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/perl
use warnings;
use strict;

my $file = 'datei';
my $tmpfile = "$file.tmp";

my %changes = (
    'eintrag1' => 'XXXX',
    'eintrag2' => 'foo',
    # etc.
);

open(my $fh, '<', $file) or die $!;
open(my $th, '>', $tmpfile) or die $!;

LINE:
while (my $line = <$fh>) {
    KEY:
    for my $key (keys %changes) {
        if ($line =~ s/($key\s*=\s*)[^#\s]+/$1$changes{$key}/) {
            print $th $line;
            next LINE;
        }
    }
    print $th $line;  # kein Match
}

close($fh) or die $!;
close($th) or die $!;
unlink $file;          # oder als Backup speichern
rename $tmpfile, $file;


Gruß FIFO

Editiert von FIFO: Falsches Semikolon hinter KEY-Label korrigiert
Last edited: 2012-05-10 00:24:55 +0200 (CEST)
Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it? -- Brian Kernighan: "The Elements of Programming Style"

View full thread perl one liner : string suchen und ersetzen wert (unix)