Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

txt to csv file

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

715 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