pdahal has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks! Can anyone help me how can I concatenate two fields of a CSV file?

Replies are listed 'Best First'.
Re: Combining two fields of a CSV file
by Corion (Patriarch) on Jul 02, 2017 at 07:43 UTC

    Concatenating two strings is done with the . operator:

    my $concatenated = $left_field . $right_field;

    To give you more specific advice, can you please show us what code you already have and where it fails to do what you need?

    Maybe perlfaq4 on arrays has some hints for a good approach.

    For reading a CSV file, please use Text::CSV_XS.

Re: Combining two fields of a CSV file
by Tux (Canon) on Jul 03, 2017 at 09:48 UTC
    $ cat test.csv foo,bar,baz,qux 1,a,AA,Fubberwurmenslijm 2,b,BB,Keveroog 3,c,CC,Boomslanghuid $ perl -MText::CSV_XS=csv -wE'csv(in=>csv(in=>"test.csv",bom=>1,on_in= +>sub{$_{merged}=$_{bar}.$_{baz}}))' qux,bar,merged,foo,baz Fubberwurmenslijm,a,aAA,1,AA Keveroog,b,bBB,2,BB Boomslanghuid,c,cCC,3,CC $ perl -MText::CSV_XS=csv -wE'csv(in=>"test.csv",bom=>1,headers=>[qw(f +oo bar baz qux merged)],on_in=>sub{$_{merged}=$_{bar}.$_{baz}})' foo,bar,baz,qux,merged 1,a,AA,Fubberwurmenslijm,aAA 2,b,BB,Keveroog,bBB 3,c,CC,Boomslanghuid,cCC

    Enjoy, Have FUN! H.Merijn
Re: Combining two fields of a CSV file
by Marshall (Canon) on Jul 03, 2017 at 08:48 UTC
    A field of a CSV file is a "single thing".
    That "single thing" can be a single numeric value or a single string.
    Concatenation of two fields can be complex if you mean to put that into a resulting CSV file.

    "field1","field2"
    could be?: ""field1""field2""
    Or "field1field2"?

    Use the Text::CSV_XS module to write fields which may contain quote characters. This quotes within quotes stuff can get complicated. Let the module do it. Again, A field of a CSV file is a "single thing".

    update: Maybe I was not clear? Weird escaping rules can come into play when combining two text fields like "abc 123" and "xyz 456" into a single CSV field. The easiest way is "abc 123 xyz 456". However it is possible for the text field to have 2 separately identifiable quoted strings within it - that involves "quotes within quotes". If you want that, things get more complicated.

Re: Combining two fields of a CSV file
by thanos1983 (Parson) on Jul 02, 2017 at 22:29 UTC
Re: Combining two fields of a CSV file
by BillKSmith (Monsignor) on Jul 02, 2017 at 22:57 UTC
    In the simple case where you do not need the module, perl's command switches (refer: perlrun) make this task trivial. Use -a for automatic splitting -e, to make it a oneliner, -i for file backup, -F to specify the split character, -p to print the output, and > to redirect the output to a file.

    UPDATE: Sorry, This reply was previously posted in the wrong thread. It has been moved to the correct place.

    Bill
Re: Combining two fields of a CSV file
by karlgoethebier (Abbot) on Jul 03, 2017 at 07:35 UTC

    The inevitable one liner:

    perl -E 'say join ":", ( split /,/, "nose,foo,cuke,bar" )[ 0, 2 ]'

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: Combining two fields of a CSV file
by Anonymous Monk on Jul 03, 2017 at 07:07 UTC

    for a simply csv, without field separator inside the fields, you can try a "one liners" solution. eg.

    cat z.csv
    name,surname,points Albert,Einstein,133 Marie,Curie,145 Thomas,Edison,165
    perl -a -F, -lne 'print $F[0].$F[2]' z.csv
    namepoints Albert133 Marie145 Thomas165
Re: Combining two fields of a CSV file
by locked_user sundialsvc4 (Abbot) on Jul 02, 2017 at 14:21 UTC

    The Text::CSV_XS module shown above would probably be your most-reliable solution.   To elaborate, you would read each line of data, use this module to separate the line into an array of values, then combine the strings as desired ... perhaps, into a new array.   Then, use the same module to construct a new line of CSV-formatted data from the array, which you write line-by-line to a new file.   (Perhaps, a temporary file.)   Notice that this technique purposely does not modify the original input file, allowing the program to be safely re-run while you debug it.   The module does all the dirty work (which can be tricky) of decoding and re-encoding the CSV data.   You simply concern yourself with the values.