Atanas-VI

DeleteSpaces.perl

Dec 4th, 2020 (edited)
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.19 KB | None | 0 0
  1. # ################################################
  2. # TRIM A FILE
  3. # ################################################
  4.  
  5. # Declare the subroutines
  6. sub trim($);
  7.  
  8. # Perl trim function to remove whitespace from the start and end of the string
  9. sub trim($)
  10. {
  11.     my $string = shift;
  12.     $string =~ s/^\s+//;
  13.     $string =~ s/\s+$//;
  14.     return $string;
  15. }
  16.  
  17. @Zeilen = ("");    # Speicher für alle Datensaetze
  18. $i = 0;
  19. open(ZEILEN, "<$ARGV[0]") || die "Datei nicht gefunden\n";
  20. while(<ZEILEN>)              # Datei einlesen
  21.  {
  22.     push(@Zeilen,$_);
  23.     $i++;                        # Datensatzzähler erhöhen
  24.  }
  25. close(ZEILEN);
  26. $Anzahl = $i - 1;              # Anzahl Datensätze merken
  27.  
  28. open(ZEILENCLEANED, ">$ARGV[1]");   # Datei zum Schreiben für gertrimmte Zeilen öffnen
  29. for(@Zeilen)                        # solange Zeilen vorhanden
  30.  {
  31.     $zeile=$_;
  32.     $zeile=&trim($zeile);
  33.     $laenge=length($zeile);
  34.     if(($zeile =~ /\S/)) {
  35.       print ZEILENCLEANED $zeile;   # Aktuelle Zeile getrimmt in den Output schreiben
  36.       print ZEILENCLEANED "\n";
  37.       $i++;
  38.     }
  39.  }
  40. print $Anzahl," Datensaetze geschrieben in $ARGV[1]\n";   # Nur zur Kontrolle: auf Standardausgabe
  41. close(ZEILENCLEANED);
Add Comment
Please, Sign In to add comment