Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

parse the txt files to csv

Posted on 2014-03-22
10
Medium Priority
?
257 Views
Last Modified: 2014-05-26
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
0
Comment
Question by:magento
  • 5
  • 5
10 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 39948134
If I understand what you are asking, this should do it
perl -e '@ARGV=<Folder1/*.txt>; while(<>){ chomp;print "$_,".<>; close $ARGV}' > Folder1.csv
0
 
LVL 5

Author Comment

by:magento
ID: 39948274
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
0
 
LVL 85

Expert Comment

by:ozo
ID: 39948291
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?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 5

Author Comment

by:magento
ID: 39948549
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

0
 
LVL 85

Expert Comment

by:ozo
ID: 39949319
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
0
 
LVL 5

Author Comment

by:magento
ID: 39949483
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
0
 
LVL 85

Expert Comment

by:ozo
ID: 39949487
What is in the .txt files, and what rows are you getting?
0
 
LVL 5

Author Comment

by:magento
ID: 39949516
Please find the attached.
Ozo.zip
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 39950081
perl -e "@ARGV=<FOLDER1/*.txt>; while(<>){ chomp;print qq'$_,'.<>; close ARGV}" > Folder1.csv
0
 
LVL 5

Author Comment

by:magento
ID: 39950855
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
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question