Solved

txt to csv file

Posted on 2011-03-09
5
379 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
[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
  • 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

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

617 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