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

x
?
Solved

txt to csv file

Posted on 2011-03-09
5
Medium Priority
?
383 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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

886 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