#! /usr/local/bin/perl # #script opens and parses CSV files #removes carriage return #and writes all into a new text file # #tested: --ok! use strict; use warnings; use Text::CSV; #use Encode; # probably not needed :encoding(utf8) #Input CSV filename my $file = $ARGV[0]; if (!$ARGV[0]) { $file = './data/IDs.csv'; #Default } if (!-f $file) { print "Kann Datei $file nicht finden: $!\n"; exit 1; } #Parsing CSV local $/ = "\r\n"; #add windows carriage return to perl's eol (newline) my $csv = Text::CSV ->new ({binary =>1, eol => $/}); open (my $fhCSV, '<', $file) or die "Kann $file nicht öffnen $!\n"; open (my $fhOUT, '>', './data/IDs.txt') or die "Kann Datei nicht öffnen: $!\n"; while (my $line = <$fhCSV>) { if ($csv->parse($line)) { my @fields = $csv->fields (); chomp (@fields); print $fhOUT "@fields\n"; } else { warn "Line could not be parsed: $line\n"; } } print "CSV parsed and saved as text file: /data/IDs.txt!"; close $fhCSV; close $fhOUT;