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

Afternoon learned ones, I have taken over a script written in bash, which is using awk cut and grep to pullout a file name from a specific line

3,"2014-02-19 14:29:05","Extracted 1 Unfulfilled Reconciliation record +s into /opt/mysql/backup/recon/201312/input/UNFULFILLED_RECONCILIATIO +N_20130225.CSV","URECONCILIATION"

I cannot use delimeters as the paths may change and text also, I need a one line to pull out any file that end in *.CSV

my research has taken me here https://blogs.oracle.com/ksplice/entry/the_top_10_tricks_of and a few others I will need to call it from bash?

If it isnt clear I want to use perl to do this hence the post :-)

Replies are listed 'Best First'.
Re: Obtain CSV Fiile from text
by hazylife (Monk) on Mar 06, 2014 at 13:36 UTC
    How's this: echo "$your_line" | perl -nle 'print if s/.*?\s(\S+\.csv).*/$1/i'

    EDIT: or, even better: perl -nle 'print if s#[^/]+(/\S+?\.csv).*#$1#i'

      Apologies to all I have been off for a couple of days, but thanks to all who came to my aid... in the end I went with..

      file=`echo $line | perl -n -l -e ' ($f) = $_ =~ m!.*/(\w+.csv)!i; print $f'

      I think my shell collegues were wary of this kind of magic :-) once again my thanks to you all

Re: Obtain CSV File from text
by Tux (Canon) on Mar 06, 2014 at 15:19 UTC

    That line comes from a CSV producer. Is it possible to ask (them) to add a field that *only* has the source file?

    • What if the file name becomes a URI?
    • What if the file name will contain spaces?
    • What if the comment changes?
    • What if the file name will contain special characters like " or ,, that are totally valid in file names?
    • What if the file name is case(in)sensitive?
    • What if the path becomes relative or starts with a drive letter?

    Assuming the line comes out the file named export.csv, you could try an overly safe approach like this:

    $ cat export.csv 3,"2014-02-19 14:29:05","Extracted 1 Unfulfilled Reconciliation record +s into /opt/mysql/backup/recon/201312/input/UNFULFILLED_RECONCILIATIO +N_20130225.CSV","URECONCILIATION" $ perl -MText::CSV_XS=csv -wE'for(@{csv(in=>"export.csv")}){say for ma +p{s/.* into //i;$_}grep/\.csv/i=>@$_}' /opt/mysql/backup/recon/201312/input/UNFULFILLED_RECONCILIATION_201302 +25.CSV $

    Requires Text::CSV_XS-1.04, but it is a one-liner now.


    Enjoy, Have FUN! H.Merijn
Re: Obtain CSV Fiile from text
by Don Coyote (Hermit) on Mar 06, 2014 at 14:02 UTC

    This matching pattern with the use of lookarounds delimited by spaces extracts the filename you require. When you say delimiters I am hoping you are referring not to the spaces between each character blocks.

    #!/usr/bin/perl use warnings; use strict; my $line = q{3,"2014-02-19 14:29:05","Extracted 1 Unfulfilled Reconcil +iation records into /opt/mysql/backup/recon/201312/input/UNFULFILLED_ +RECONCILIATION_20130225.CSV","URECONCILIATION"}; $line =~ m{ \s\S*?(?!\.CSV) # space delimited text filter \s(\S*?(?<=\.CSV)) # filename }x; print $1;
    Edit: ah, one liner as in need to pipe it or so. oops sorry thought you were after the pattern.
Re: Obtain CSV Fiile from text
by karlgoethebier (Abbot) on Mar 06, 2014 at 15:19 UTC
    echo $line |  perl -ne 'print $1 if /.+\/(.+\.csv).+/i';

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Obtain CSV Fiile from text
by GotToBTru (Prior) on Mar 06, 2014 at 17:09 UTC

    Do you also need the path with the file name? That is to say, is the result you are looking for /opt/mysql/backup/recon/201312/input/UNFULFILLED_RECONCILIATION_20130225.CSV or just UNFULFILLED_RECONCILIATION_20130225.CSV?

Re: Obtain CSV Fiile from text
by Anonymous Monk on Mar 07, 2014 at 03:59 UTC
    $var1='3,"2014-02-19 14:29:05","Extracted 1 Unfulfilled Reconciliation + records into /opt/mysql/backup/recon/201312/input/UNFULFILLED_RECONC +ILIATION_20130225.CSV","URECONCILIATION"'; $var1 =~ /( [A-Za-z0-9_\/]+\.CSV)/ ; print "$1\n";
    As above, use 'i' switch on regex if eg CSV or csv or ...
      Appending to myself re case insensitive
      $var1='3,"2014-02-19 14:29:05","Extracted 1 Unfulfilled Reconciliation + records into /opt/mysql/backup/recon/201312/input/UNFULFILLED_RECONC +ILIATION_20130225.csv","URECONCILIATION"'; $var1 =~ /( [a-z0-9_\/]+\.CSV)/i ; print "$1\n";