Solved

txt to csv file

Posted on 2011-03-09
5
372 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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
Your awesome man! Worked like a charm. Thanks for being patient with me.
0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
Hey no prob.  Glad to help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now