#!/usr/bin/perl use 5.010; use strict; use warnings; use utf8; use open qw(:std :utf8); #Convert a csv file to text file format #https://ronaldduncan.wordpress.com/2009/10/31/text-file-formats-ascii-delimited-text-not-csv-or-tab-delimited-text #the csv file is expected to use double quotes as the delimiter character and utf-8 encoded sub is_complete_line { my ($line) = @_; my $inside_double_quotes; my $opening_double_quotes = 0; my $closing_double_quotes = 0; foreach(split('', $line)) { next if $_ ne '"'; if ($inside_double_quotes) { $inside_double_quotes = ! $inside_double_quotes; $closing_double_quotes++; next; } $opening_double_quotes++; $inside_double_quotes = ! $inside_double_quotes; } $opening_double_quotes == $closing_double_quotes; } sub convert_to_ttf { my ($line) = @_; my $inside_double_quotes; my $converted_line = ''; foreach(split('', $line)) { if ($_ eq '"' && $inside_double_quotes) { $inside_double_quotes = 0; next; } if ($_ eq '"' && ! $inside_double_quotes) { $inside_double_quotes = 1; next; } if ($_ eq "," && ! $inside_double_quotes) { $converted_line .= "\x{1F}";#unit separator next; } if ($_ eq "\n" && ! $inside_double_quotes) { $converted_line .= "\x{1E}";#record separator next; } $converted_line .= $_; } $converted_line; } open my $fh, '>', "output.txt" or die "Couldn't create the output file.\n"; my $line = ''; while (<>) { $line .= $_; if (is_complete_line($line)) { print $fh convert_to_ttf($line); $line = ''; } } close $fh; print "Conversion done.\n";