Solved

txt to csv file

Posted on 2011-03-09
5
374 Views
Last Modified: 2012-06-27
Hello all,

I need a perl script that will open all text files in a directory and parse then for 2 unique items. Example text:

hostname: xxxxxxx
time: 11:11
key: abcdefghijklmnopqrstuvwxyz
password: aaaaaaaa

I will need to get the "hostname" and "key" from every text file...around 4500 text files in this directory and place them in a .csv file with this format:


hostname,key
hostname,key
hostname,key

where hostname is the actual hostname and key it the actually key. I do not need the actual word "hostname" or "key".

I am a extremely novice perl programmer. If you could send it to me in script format that would be extremely helpful.

Thank you so much!
 
0
Comment
Question by:draven11722
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 35090053
Is this on Windows?

If so:

mungFile.pl:

while ( <> )
{
    s/[\r\n]//g;
    if ( /hostname\:\s+/ ) { $hostname = $'; }
    elsif ( /key\:\s+/ ) { print "$hostname,$'\n"; }
}

Open in new window


and on the command line in the directory (creates output one level up - you can easily change)::

del ..\myoutput.csv
for %i in (*) do perl mungFile.pl %i >> ..\myoutput.csv

Open in new window

0
 

Author Comment

by:draven11722
ID: 35090086
sjklein42, yes this is on Windows? Thanks for the quick response!

Forgive my ignorance but how would i implement this? I am new to perl. I have installed activestate activeperl on my windows box and I am able to run simple scripts.

If i could get a bit more detail I may understand a bit better.

Thanks again!
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 35090413

In your working directory

Create a text file called "mungFile.pl" with the lines from the first (PERL) codeblock above.

Create another text file called mungAllFiles.bat with these lines:

REM usage:   call mungAllFiles.bat   c:\lotsOfFiles\*   c:\work\summary.csv

del %2
for %%i in (%1) do @perl mungFile.pl %%i >> %2

Open in new window


Without changing your current folder, issue this command to scan the specified folder and put the results in the specified CSV file.

call mungAllFiles.bat   c:\lotsOfFiles\*   c:\work\summary.csv

0
 

Author Closing Comment

by:draven11722
ID: 35090546
Your awesome man! Worked like a charm. Thanks for being patient with me.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35090577
Hey no prob.  Glad to help.
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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

809 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