Solved

Simple perl script

Posted on 2014-01-08
7
309 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 40

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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.
This article will show, step by step, how to integrate R code into a R Sweave 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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

708 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

14 Experts available now in Live!

Get 1:1 Help Now