Perché il grep include ancora.E .. Nella mia lista di file anche se non corrisponde al regex lo do?

StackOverflow https://stackoverflow.com//questions/9629843

Domanda

Quello che sto cercando di fare è ottenere una raccolta di tutti i grafici UNICODE UTF16.Ho scaricato tutti i file PDF da http://unicode.org/charts/pdf/ e ha deciso di usarePerl per sbarazzarsi di tutti i grafici speciali o utf32 con il seguente script:

#!/usr/bin/perl

opendir(my $dir, ".");
my @files = grep {!/^U[0-9,A-F]{4}\.pdf/ && !/utf16only.pl/} readdir($dir);
for $f (@files)
{
    print "deleting $f...\n";
    #unlink $f;
}
closedir($dir);
.

Quando eseguo lo script, ottengo la seguente uscita:

C:\Users\Evan\Downloads\Unicode 6.1 Charts>utf16only.pl
deleting ....
deleting .....
deleting 10FF80.pdf...
deleting ErrorLink.pdf...
deleting U10000.pdf...
deleting U100000.pdf...
deleting U10080.pdf...
deleting U10100.pdf...
deleting U10140.pdf...
deleting U10190.pdf...
deleting U101D0.pdf...
deleting U10280.pdf...
deleting U102A0.pdf...
deleting U10300.pdf...
deleting U10330.pdf...
deleting U10380.pdf...
deleting U103A0.pdf...
deleting U10400.pdf...
deleting U10450.pdf...
deleting U10480.pdf...
deleting U10800.pdf...
deleting U10840.pdf...
deleting U10900.pdf...
deleting U10920.pdf...
deleting U10980.pdf...
deleting U109A0.pdf...
deleting U10A00.pdf...
deleting U10A60.pdf...
deleting U10B00.pdf...
deleting U10B40.pdf...
deleting U10B60.pdf...
deleting U10C00.pdf...
deleting U10E60.pdf...
deleting U10FF80.pdf...
deleting U11000.pdf...
deleting U11080.pdf...
deleting U110D0.pdf...
deleting U11100.pdf...
deleting U11180.pdf...
deleting U11680.pdf...
deleting U12000.pdf...
deleting U12400.pdf...
deleting U13000.pdf...
deleting U16800.pdf...
deleting U16F00.pdf...
deleting U1B000.pdf...
deleting U1D000.pdf...
deleting U1D100.pdf...
deleting U1D200.pdf...
deleting U1D300.pdf...
deleting U1D360.pdf...
deleting U1D400.pdf...
deleting U1EE00.pdf...
deleting U1F000.pdf...
deleting U1F030.pdf...
deleting U1F0A0.pdf...
deleting U1F100.pdf...
deleting U1F200.pdf...
deleting U1F300.pdf...
deleting U1F600.pdf...
deleting U1F680.pdf...
deleting U1F700.pdf...
deleting U1FF80.pdf...
deleting U20000.pdf...
deleting U2A700.pdf...
deleting U2B740.pdf...
deleting U2F800.pdf...
deleting U2FF80.pdf...
deleting U3FF80.pdf...
deleting U4FF80.pdf...
deleting U5FF80.pdf...
deleting U6FF80.pdf...
deleting U7FF80.pdf...
deleting U8FF80.pdf...
deleting U9FF80.pdf...
deleting UAFF80.pdf...
deleting UBFF80.pdf...
deleting UBOOP.pdf...
deleting UCFF80.pdf...
deleting UDFF80.pdf...
deleting UE0000.pdf...
deleting UE0100.pdf...
deleting UEFF80.pdf...
deleting UF0000.pdf...
deleting UFFF80.pdf...
.

Le prime 2 linee sono ancora generatori di . e generacodictagcode e, penso perché sto cercando di scollegare .., sta rimuovendo un gran numero di file che non volevo rimuovere.Non sono sicuro se il problema risiede con i miei regex, ., grep o readdiretagcode, ma rimuove più file di quanto dovrebbe.

È stato utile?

Soluzione

This line:

grep {!/^U[0-9,A-F]{4}\.pdf/ && !/utf16only.pl/}

Includes only files that match neither regex. That includes . and ... To exclude those two, you'd have to extend that:

grep {!/^U[0-9,A-F]{4}\.pdf/ && !/utf16only.pl/ && !/^\.{1,2}$/}

Altri suggerimenti

It's your regex:

grep {!/^U[0-9,A-F]{4}\.pdf/ && !/utf16only.pl/} readdir($dir);

This says "match all files that do not match U[0-9,A-F]{4}.pdf (note - do you really want that comma in there?), and also aren't utf16only.pl.

Since . and .. do not match U[0-9A-F]{4}.pdf and don't match utf16only.pl, they are also deleted.

Add a !/^\./ into your grep to also exclude these files from the deletion list:

grep {!/^U[0-9A-F]{4}\.pdf/ && !/^\./ && !/utf16only.pl/} readdir($dir);

Note - I changed your [0-9,A-F] to [0-9A-F], because I don't think your file names will have commas in them.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top