in reply to Re^4: Perl script help to convert .txt file to .csv
in thread Perl script help to convert .txt file to .csv
Oh. Where to begin. Okay, I'm going to (try) to respond to both your posts here.
The command line:perl -n -e" print " yourFile causes perl to read (-n) yourFile one line at a time, and execute the inline script (the -e" ... " bit) for each line, passing the line in the variable: $_.
$_ is the 'default variable' upon which many of Perl's built-in functions will operate if they are not given any arguments. Eg print
So, in the above case, all that does is print each line to the terminal. The effect is the same aas type yourFile or cat yourFile depending upon your operating system. Not so useful, but a useful simple example.
If you add the switch -MO=Deparse to the above command, Perl displays the full script equivalent that it runs on your behalf.
In the case of the above, when you type the command: perl -n -e" print " yourFile, Perl actually executes:
c:\test>perl -MO=Deparse -n -e" print " yourFile LINE: while (defined($_ = <ARGV>)) { print $_; } -e syntax OK
What that tells you is that Perl opens yourFile as the filehandle ARGV, and then reads a line at a time from that file handle and places it into $_. It then invokes the -e code, which is just print, which causes it to be printed to the screen. Where it could be redirected to another file.
In the case of the one-liner I posted, the output (with my annotations) is :
## Set the input separator to be '>'; and the output separator to be " +\n"' BEGIN { $/ = ">"; $\ = "\n"; } ## read each input record (terminated by the '>' character) into $_ LINE: while (defined($_ = <ARGV>)) { ## Remove the record terminator ('>') chomp $_; ## split the record on whitespace into @F our(@F) = split(' ', $_, 0); ## If the record was empty (the first one always will be with your dat +a), ## then skip to the next record. next unless @F; ## remove the 'length=' prefix from the second field s/length=// foreach ($F[1]); ## replace the 6th field in the array, with the concatenation of itsel +f ## and all the fields that follow it @F[5] = join('', @F[5..99]); ## Then discard all the fields that follow it. ## Ie. truncate the array after the 6th field $#F = 5; ## And print out the fields joined with ','s and prefixed with '>' ## (and ending with a newline. See output separator above.) print '>', join(',', @F); }
Either you'll read perlrun, understand the options used on the one-liner and make sense of all that and it will help you. Or you won't and it was a waste of both our time.
|
|---|