Solved

parse the txt files to csv

Posted on 2014-03-22
10
231 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
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl search and replace 6 168
Removing file extension within a file. 4 93
File Find regex problem 4 62
Is it true to say that Python is successor of Perl? 7 173
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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