2016-04-01 15 views
0

Scenerio1:Bazı durumlarda boş olabilecek başka bir dosyada listelenen desenler hariç, giriş dosyalarındaki satırları silinsin mi?

File1:

exclude1 
exclude2 
exclude3 

File2 (dosya uzunlukta olabilir, bazen boş bir dosya değişir):

statement1 that has no excludes 
statement2 that has exclude3 
statement3 that has no excludes 
statement4 that has no excludes 
statement5 that has exclude1 
statement6 that has exclude2 
statement7 that has no excludes 

çıkışı:

statement1 that has no excludes 
statement3 that has no excludes 
statement4 that has no excludes 
statement7 that has no excludes 

Scenerio2:

File1: (boş dosya)

empty file 

File2:

statement1 that has no excludes 
statement2 that has no excludes 
statement3 that has no excludes 
statement4 that has no excludes 

çıkışı:

statement1 that has no excludes 
statement2 that has no excludes 
statement3 that has no excludes 
statement4 that has no excludes 

komut: Bu komut dosyası scenario1 için iyi çalışıyor

open (IN58, "<file2.txt") or die; 
open (IN59, "<file1.txt") or die; 
open (OUT42, ">output.txt") or die; 
my @excludes = <IN59>; 
chomp @excludes; 
my $excludes = join ' |',@excludes; 
while (<IN58>) { 
next if /${excludes}/; 
print OUT42 $_ ; 
} 
close (IN58); 
close (IN59); 
close (OUT42); 

, ne zaman xclude dosyası (ör. file1) boş hale gelir, boş çıktı dosyası üretiyor ve istediğim gibi çalışmıyor. Koddaki herhangi bir düzeltme son derece yararlıdır.

cevap

0

i bir dosyadan eşleşmeleri ayıklamak için negatif grep kullanmış İşte bu

deneyin burada if condition dosya boş olup olmadığını kontrol etmek için kullanılır.

Dosya boşsa koşul karşılanmaz, bu nedenle @br'daki değerler değişmez. Koşul tatmin edildiyse, @br'daki değerler yeni değerlerle değiştirilir. Burada

use warnings; 
use strict; 
open my $fh1, "<", "f1.txt" or die"$!"; 
my @ar = <$fh1>; 
my $exculed = join("|",@ar); 
$exculed=~ s/\n|\s//g; #your input have some spaces so i used substitution instead of chomp 

open my $fh2, "<", "f2.txt"; 
open my $nw, ">", "newfile.txt"; 
my @br = <$fh2>; 
@br = grep{!/$exculed/g} @br if ($exculed ne ""); 
print $nw @br; 
+0

Bu mantık diğer tasarımlar için işe yarıyor olsa da, kelimesi hariç tutulduğunda çalışma DFN1C1 ve File2 satırları aşağıdaki gibi tek satırda aşağıdaki gibidir); \ po [6] \: DFN1C1 bağlantı noktası haritası (D => \ po [5] _net_1 \, CLK => CLK16M_c, CLR => \ int_boardsel \, Q => \ po [6] _net_1); v13_D_9_pad: OUTBUF bağlantı noktası haritası (D => v13_D_9_c, PAD => v13_D_9); –

+0

@sumathigokul regex'ten $ 'ı çıkarın ve deneyin, sonradan yayınlayın – mkHun

2

hüner istisnalar için test hakkında verimli olmak - eğer bunu yapabilirsiniz yolu anahtar kelimelerden bir normal ifade inşa ederek ve daha sonra hiç 'maç' herhangi satırları 'reddetme'.

Yani:

#!/usr/bin/perl 
use strict; 
use warnings; 

my @excludes = qw (exclude1 
    exclude2 
    exclude3); 

my $exclude_regex = join("|", map {quotemeta} @excludes); 
$exclude_regex = qr/$exclude_regex/; 


while (<DATA>) { 
    print unless /$exclude_regex/; 
} 


__DATA__ 
statement1 that has no excludes 
statement2 that has exclude3 
statement3 that has no excludes 
statement4 that has no excludes 
statement5 that has exclude1 
statement6 that has exclude2 
statement7 that has no excludes 

Şimdi buradaki sorun elbette - öylesine etkili bir şey maç olacak boş bir 'maç', - sen 'joker' o noktada eşlemeleriniz. (ve her şeyi hariç).

bu işleme en kolay yolu, kibrit asla bir 'varsayılan' desen, ekleyerek - örneğin boş bir satır:

my $exclude_regex = join("|", '^$', map {quotemeta} ( @excludes)) ; 

Bu boş satırlar ve birine sahiptir şey filtreleyeceğini

!/usr/bin/perl 
use strict; 
use warnings; 

open(my $data,  '<', "file2.txt") or die; 
open(my $excludes, '<', "file1.txt") or die; 
open(my $output, '>', "output.txt") or die; 

chomp(my @excludes = <$excludes>); 
my $exclude_regex = join("|", '^$', map {quotemeta} (@excludes)); 
$exclude_regex = qr/$exclude_regex/; 
print $exclude_regex, "\n"; 

select $output; 
while (<$data>) { 
    print unless m/$exclude_regex/; 
} 
:

(?^:^$|exclude1|exclude2|exclude3) 

dosya okuma bit eklemek için: senin gibi bir normal ifade üreten, kelimeleri hariç

Ve siz 'regex montaj' boşluk var gibi gibi size hariç normal ifade değiştirmeyi düşünün isteyebilirsiniz: hafifçe sonra (eşleştirme desende kelime sınırlarını içerecektir

$exclude_regex = qr/\b$exclude_regex\b/; 

rağmen, olur 'boş satır' eşleşmesini kır ve artık eşleşmeyecek - ama yine de yer tutucu olarak çalışacak. Burada sözcük filehandles açık

  • 3 argüman konum iyi iken

    ,

  • use strict;use warnings; zorunlu düşünülmelidir.
  • Hariç tutulan dosyanız regex meta karakterlerini içeriyorsa ne olacağını düşünün. Bu nedenle, quotemeta'un buradaki içeride olması, söz konusu öğelerin söz dizimi olarak ele alınmasıdır .... ancak hariç tutma dosyanızdaki normal ifadeyi desteklemeyi faydalı bulabilirsiniz.
+0

Nice answer! Varsayılan model fikrini beğendim. –