[EMAIL PROTECTED] wrote:
Hi,

Hello,

We receive a text file with the following entries.

"000001","item1","apple one","apple two","apple three"
"000002","item2","body one","body two","body three"
"000003","item2","body one","body two","body three"
"000004","item2","body one","body two","body three"
"000005","item1","orange one","orange two","orange three"
"000006","item2","body one","body two","body three"
"000007","item2","body one","body two","body three"
"000008","item2","body one","body two","body three"
"000009","item2","body one","body two","body three"
"000010","item2","body one","body two","body three"

How do I use perl to convert the above to the following?  I'm a novice
perl user.

"apple three","body one","body two","body three"
"apple three","body one","body two","body three"
"apple three","body one","body two","body three"
"orange three","body one","body two","body three"
"orange three","body one","body two","body three"
"orange three","body one","body two","body three"
"orange three","body one","body two","body three"
"orange three","body one","body two","body three"

$ echo '"000001","item1","apple one","apple two","apple three"
"000002","item2","body one","body two","body three"
"000003","item2","body one","body two","body three"
"000004","item2","body one","body two","body three"
"000005","item1","orange one","orange two","orange three"
"000006","item2","body one","body two","body three"
"000007","item2","body one","body two","body three"
"000008","item2","body one","body two","body three"
"000009","item2","body one","body two","body three"
"000010","item2","body one","body two","body three"' | \
perl -lne'
    my @data = split /,/;
    if ( $data[ 1 ] eq q/"item1"/ ) {
        $field1 = $data[ 4 ];
        }
    elsif ( $data[ 1 ] eq q/"item2"/ ) {
        print join q/,/, $field1, @data[ 2, 3, 4 ];
        }
'
"apple three","body one","body two","body three"
"apple three","body one","body two","body three"
"apple three","body one","body two","body three"
"orange three","body one","body two","body three"
"orange three","body one","body two","body three"
"orange three","body one","body two","body three"
"orange three","body one","body two","body three"
"orange three","body one","body two","body three"




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to