Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to pull a job number out of notes listed in a csv, and then append that number to the end of the line, preferably through sed, awk, grep or perl (which I have installed in Cygwin.

Here's a mockup:

"HD1231203", "1231232","fake.name","Lots of text before the job 150232 and then more"
"HD5164635", "8918123","more.fake","151243 and then some text"
"HD1541545", "8435413","last.fake","Oh look, we've got 150213 and 151487 this time!"

should become:

"HD1231203", "1231232","fake.name","Lots of text before the job 150232 and then more", "150232"
"HD5164635", "8918123","more.fake","151243 and then some text","151243"
"HD1541545", "8435413","last.fake","Oh look, we've got 150213 and 151487 this time!","150213","151487"

I've tried what little I know with sed, but I'm honestly out of my depth.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Simple Perl solution:

perl -F, -lape '$_ .= qq(,"$1") while $F[-1] =~ /([0-9]+)/g' FILE

-F, splits on comma (might break if comma is inside double quotes after the number, see below). While there are numbers in the last field, they are added to the current line.

To solve it correctly, you should process the input with Perl's Text::CSV module.

#!/usr/bin/perl
use warnings;
use strict;

use Text::CSV;

my $csv = 'Text::CSV'->new({ always_quote => 1,
                             allow_whitespace => 1,
                             eol => "\n",
                           }) or die 'Text::CSV'->error_diag;
open my $IN, '<', shift or die $!;
while (my $row = $csv->getline($IN)) {
    my @new;
    push @new, $1 while $row->[-1] =~ /([0-9]+)/g;
    $csv->print(*STDOUT, [@$row, @new]);
}
$csv->eof or $csv->error_diag;
share|improve this answer
    
The script works perfectly, and the one-liner is pretty good too (aside from throwing in a line-break). Much appreciated. –  DrPsychonaut Aug 13 '13 at 2:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.