Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simple perl script

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

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve 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…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

876 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