Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

parse the txt files to csv

Posted on 2014-03-22
10
Medium Priority
?
254 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 84

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 84

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 84

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 84

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 84

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

661 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