Solved

Simple perl script

Posted on 2014-01-26
3
388 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
[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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 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