Find a pattern in a line, and append that matched pattern to the same line

0

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.

user245220

Posted 2013-08-13T01:31:12.180

Reputation:

Answers

1

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;

choroba

Posted 2013-08-13T01:31:12.180

Reputation: 14 741

The script works perfectly, and the one-liner is pretty good too (aside from throwing in a line-break). Much appreciated. – None – 2013-08-13T02:40:46.453