Solved

Simple perl script

Posted on 2014-01-26
3
386 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
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 …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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…

910 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

24 Experts available now in Live!

Get 1:1 Help Now