Link to home
Start Free TrialLog in
Avatar of magento
magento

asked on

parse the txt files to csv

Hi ,

I have a folder named "Folder1" which contains say 10 txt files.

What i am looking for is a script which will parse all 10 txt files and make a csv file with the folder name, so in this example its Folder1.csv

Each line in a csv file represents a txt file , so this csv file should have 10 rows.

The csv has 2 columns only .

Column A -> Holds 1 line of the txt file
Column B -> Holds 2nd line to end of the line in the txt file.

Is it feasible via regex?or something in perl

Thanks
Avatar of ozo
ozo
Flag of United States of America image

If I understand what you are asking, this should do it
perl -e '@ARGV=<Folder1/*.txt>; while(<>){ chomp;print "$_,".<>; close $ARGV}' > Folder1.csv
Avatar of magento
magento

ASKER

Hi Ozo,

Thank you .

Is it possible for loop multiple folders ?

Just realised that i have around 40 Folders so i need 40 csv files ,Can this be done ?

Thanks
Is there a pattern to the folder names?  Or is there a list of folder names?
Does the above do what you want for the one folder?
Avatar of magento

ASKER

Hi Ozo,

I am using windows perl so i try the below code and it didnt work .

C:\EE>dir
 Volume in drive C has no label.
 Volume Serial Number is 40B3-E568

 Directory of C:\EE

03/23/2014  06:41 PM    <DIR>          .
03/23/2014  06:41 PM    <DIR>          ..
02/14/2014  12:02 AM    <DIR>          Admin
03/23/2014  06:41 PM    <DIR>          FOLDER1
03/22/2014  07:28 PM    <DIR>          temp_del
               0 File(s)              0 bytes
               5 Dir(s)  19,940,352,000 bytes free

C:\EE>perl -e '@ARGV=<FOLDER1/*.txt>; while(<>){ chomp;print "$_,".<>; close
 $ARGV}' > Folder1.csv
> was unexpected at this time.

C:\EE>perl -e "@ARGV=<FOLDER1/*.txt>; while(<>){ chomp;print '$_,'.<>; close
 $ARGV}" > Folder1.csv
Terminating on signal SIGINT(2)

Open in new window

Windows shell uses " instead of '
so the equivalent would be
perl -e "@ARGV=<FOLDER1/*.txt>; while(<>){ chomp;print qq'$_,'.<>; close $ARGV}" > Folder1.csv
but that doesn't explain the Terminating on signal SIGINT(2)
unless there were no *.txt files in FOLDER1, or unless one of the .txt files did not have a 2nd line
Avatar of magento

ASKER

Hi Ozo,

Sorry the above code didnt get me expected result.

I have 5 txt files , so i am expecting 5 rows of data in csv file.

But i am getting 26 rows of data .

Thanks
What is in the .txt files, and what rows are you getting?
Avatar of magento

ASKER

Please find the attached.
Ozo.zip
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of magento

ASKER

Ozo,

Thanks for your time.
But i am sorry if i havent explained it correctly.

Now i got the ouput of 8 rows.
In column A i have line 1
In column B i have line 2

But i need in column B - from line2 to end of line.

Thanks