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

x
?
Solved

Simple perl script

Posted on 2014-01-26
3
Medium Priority
?
397 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 1500 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

TCP/IP Network Protocol Cheat Sheet

TCP/IP is a set of network protocols which is best known for connecting the machines that make up the Internet. The truth is that TCP/IP is one of the oldest network protocols and its survival is mainly based on its simplicity and universality.

Question has a verified solution.

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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…

688 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