Solved

Simple perl script

Posted on 2014-01-08
7
317 Views
Last Modified: 2014-01-27
Hi ,

As part of my learning , i like to know how to get the value splitted and format as below.

Input

Name:      Hendrickx Family
Email Address:      aaaaa@gmail.com
Country:      France
      
Nationality:      Belgian
Live In:      Big City
Number Of Children:      2

Output needed.

Name,Email Address,Country,Nationality,Live In,Number Of Children
Hendrickx Family,aaaaa@gmail.com,France,Belgian,Big City,2

Thanks,
Magento
0
Comment
Question by:magento
7 Comments
 
LVL 26

Assisted Solution

by:wilcoxon
wilcoxon earned 250 total points
ID: 39766567
Assuming one "row", this will work.  If there can be multiple entries, this will need some modification.
my (@hdr, @data);
while (<>) {
    chomp;
    my ($h, $d) = split /:\s+/;
    push @hdr, $h;
    push @data, $d;
}
print join(',', @hdr), "\n", join(',', @data), "\n";

Open in new window

0
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 39766579
perl -F':\s*' -lane '/:/and push(@x,shift @F),push @y,shift @F;END{$"=",";print "@x\n@y"}' << END_INPUT

Name:      Hendrickx Family
Email Address:      aaaaa@gmail.com
Country:      France
     
Nationality:      Belgian
Live In:      Big City
Number Of Children:      2
END_INPUT
0
 
LVL 5

Author Comment

by:magento
ID: 39776795
Hi ,

If its not too much trouble, can you teach me how to handle with multiple entries.?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 41

Expert Comment

by:Sharath
ID: 39790141
Just added an IF condition to ignore blank lines in wilcoxon's solution.
my (@hdr, @data);
while (<>) {
    if (!/^$/) {
	chomp;
    my ($h, $d) = split /:\s+/;
    push @hdr, $h;
    push @data, $d;
	}
}
print join(',', @hdr), "\n", join(',', @data), "\n";

Open in new window

wilcoxon's solution gives this.
Name,Email Address,Country,,Nationality,Live In,Number Of Children
Hendrickx Family,aaaaa@gmail.com,France,,Belgian,Big City,2

Open in new window

with the if condition,
Name,Email Address,Country,Nationality,Live In,Number Of Children
Hendrickx Family,aaaaa@gmail.com,France,Belgian,Big City,2

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 39790334
perl -F':\s*' -lane '(($x,$y)=@F)>1 and $y[$i+=defined $y[$i][$j=($x{$x}||=push@x,$x)-1]][$j]=$y;END{$"=",";print "@$_"for \@x,@y}'
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 39791541
Sharath's modification gets you part of the way there.  This should do what you want with multiple records.  It makes two assumptions:
1) Blank lines separate the records
2) All fields are present in each record

If either is untrue, it will need some more modification.
my (@hdr, @data, @row, $rec);
while (<>) {
    chomp;
    if (m{^\s*$}) {
        if (@row) {
            push @data, join(',', @row);
            @row = ();
            $rec++;
        }
    } else {
        my ($h, $d) = split /:\s+/;
        push @hdr, $h unless $rec;
        push @row, $d;
    }
}
push @data, join(',', @row) if @row;
print join(',', @hdr), "\n", join("\n", @data), "\n";

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 39791553
http:#a39790334 assumes that any given named field appears at most once in each record, so that another appearance of the same field must be in a new record.
It also assumes that no field is named "0", but if that is a problem, and you have a recent version of perl, you can use //= instead of ||=
blank lines are ignored

http:#a39791541 seems to make the additional assumption that fields appear in the same order in each record, and its assumption 1) seems to be violated by the Input example in the original question.
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Regex HELP 2 46
REXEX help Part 2 2 52
AWK: How to strip all double quotes from a file? 7 32
Help with Progress 4gl Rounding Function 6 29
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
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.
The viewer will learn how to dynamically set the form action using jQuery.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

730 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