Hi all:

Working with perl 5.8.8 on RHEL 5.x. An ftp server using vsftp. I want to get the file name but the file name could include spaces, thus there could be a varied number of fields per line.

The file name, with it's PATH, begins at the 9th field of a line. The last 9 fields are the same as they pertain to the ftp process. So I am thinking on dropping the 1st 8 fields and the last 9 fields.

If there are no spaces in the file name, there will be 18 fields on a line. If there is a space, or spaces, in a file name then there will be 19, or more, fields on a line.

But the file name will always start at the 9th field and the last 9 fields can be dropped. What is in the middle, 1 field, 2 fields, 3 fields, ... will be the file name.

I can identify the lines I need, but when I see a space in a file name, or spaces, it kinda halts my thought.

Many thanks

The code I've put together. I am testing for incoming and a pattern match, 1 of 4 possibilities.

my $vsftplog = "vsftplog"; open(VLOG, $vsftplog) || die "Cannot open log file: $!\n"; while (<VLOG>) { my @fields = split(/ /); if (( "$fields[-7]" eq "i" ) && ( $fields[9] =~ m/(\/fileA-*?.?)|(\/ +fileB-*?.?)|(\fileC-*?.?)|(\/fileD-*?.?)/ )) { print "@fields"; } }

From these lines that have been identified, cut '$fields[0]...$fields7' and cut '$fields$NF...$fields-8'. So what is left is the file name with it's PATH and any spaces if spaces exist in the file name.


In reply to parse a line with varied number of fields by raggmopp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.