Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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","","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","","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
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.

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. – user245220 Aug 13 '13 at 2:40

You must log in to answer this question.