Solved

parse the txt files to csv

Posted on 2014-03-22
10
222 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 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 5

Author Comment

by:magento
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 5

Author Comment

by:magento
Comment Utility
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
Comment Utility
What is in the .txt files, and what rows are you getting?
0
 
LVL 5

Author Comment

by:magento
Comment Utility
Please find the attached.
Ozo.zip
0
 
LVL 84

Accepted Solution

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

Author Comment

by:magento
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now