Solved

Simple perl script

Posted on 2014-01-26
3
385 Views
Last Modified: 2014-01-29
I need a perl script to create a csv file in the below format

I am using Active perl in windows.

input

i am subject
username:some username1 and text
Date:30 sep , 2010
message: some message1
username:some username2 and text
Date:30 Aug , 2010
message: some messages2
i am subject2
username:some username3 and text
Date:30 sep , 2010
message: some message3

output:

Subject,username,Date,message
i am subject,some username1 and text,30 sep , 2010,some message1
i am subject,some username2 and text,30 Aug , 2010,some message2
i am subject2,some username3 and text,30 sep , 2010,some message3

Thanks
0
Comment
Question by:magento
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39810649
perl -F: -alne 'BEGIN{@h=qw(Subject username Date message);$"=",";print "@h"}$h{$F[0]}=$F[1]or$h{Subject}=$F[0];print "@h{@h}"if /message:/' <<ENDINPUT
 <<ENDINPUT
i am subject
username:some username1 and text
Date:30 sep , 2010
message: some message1
username:some username2 and text
Date:30 Aug , 2010
message: some messages2
i am subject2
username:some username3 and text
Date:30 sep , 2010
message: some message3
ENDINPUT
0
 
LVL 5

Author Comment

by:magento
ID: 39810758
Hi Ozo,


I copy paste the code and it produce the below error

C:\Expert-Exchange\perl>perl -F: -alne 'BEGIN{@h=qw(Subject username Date messa
e);$"=",";print "@h"}$h{$F[0]}=$F[1]or$h{Subject}=$F[0];print "@h{@h}"if /messa
e:/' <<ENDINPUT
Can't find string terminator "'" anywhere before EOF at -e line 1.

C:\Expert-Exchange\perl> <<ENDINPUT
<< was unexpected at this time.

C:\Expert-Exchange\perl>i am subject
'i' is not recognized as an internal or external command,
operable program or batch file.

C:\Expert-Exchange\perl>username:some username1 and text
The filename, directory name, or volume label syntax is incorrect.

C:\Expert-Exchange\perl>Date:30 sep , 2010
The system cannot accept the date entered.
Enter the new date: (mm-dd-yy) message: some message1
The system cannot accept the date entered.
Enter the new date: (mm-dd-yy) username:some username2 and text
The system cannot accept the date entered.
Enter the new date: (mm-dd-yy) Date:30 Aug , 2010
The system cannot accept the date entered.
Enter the new date: (mm-dd-yy) message: some messages2
The system cannot accept the date entered.
Enter the new date: (mm-dd-yy) i am subject2
The system cannot accept the date entered.
Enter the new date: (mm-dd-yy) username:some username3 and text
The system cannot accept the date entered.
Enter the new date: (mm-dd-yy) Date:30 sep , 2010
The system cannot accept the date entered.
Enter the new date: (mm-dd-yy) message: some message3
The system cannot accept the date entered.
Enter the new date: (mm-dd-yy) ENDINPUT

Open in new window


Not sure how to use it . My text file is large.

is it possible to work like give input as 1.txt and output as 1.csv ?

Thanks
0
 
LVL 5

Author Comment

by:magento
ID: 39816879
Hi ,

Please advice. Thanks
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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.
The viewer will learn how to count occurrences of each item in an array.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

12 Experts available now in Live!

Get 1:1 Help Now