Hi,
better use split for this type of stuff. It lets you specify the number of fields that you want to read and fills up the last one (field 6 here) with the rest of the line.
#!/usr/bin/perl use strict; use warnings; open (DATA,"</var/log/messages") || die 'Unable to open log file'; open (TMP, ">/tmp/messages.csv") || die 'Unable to write to file'; while (<DATA>) { chomp; my (@cols) = split(/\s+/, $_, 6); print TMP join(',',@cols), "\n"; } close TMP; close DATA; __DATA__ Oct 1 13:23:25 smoothwall-swe3 kernel: Allocating PCI resources start +ing at 10000000 (gap: 08000000:f6c00000) Oct 1 13:23:25 smoothwall-swe3 kernel: Built 1 zonelists Oct 1 13:23:25 smoothwall-swe3 kernel: Kernel command line: BOOT_IMAG +E=SmoothWall ro root=804 ramdisk_size=8192 no-scroll panic=30 Oct 1 13:23:25 smoothwall-swe3 kernel: Enabling fast FPU save and res +tore... done. Oct 1 13:23:25 smoothwall-swe3 smoothd: Loading Plugins for Module "/ +usr/lib/smoothd/sysinstall.so
prints:
Oct,1,13:23:25,smoothwall-swe3,kernel:,Allocating PCI resources starti +ng at 10000000 (gap: 08000000:f6c00000) Oct,1,13:23:25,smoothwall-swe3,kernel:,Built 1 zonelists Oct,1,13:23:25,smoothwall-swe3,kernel:,Kernel command line: BOOT_IMAGE +=SmoothWall ro root=804 ramdisk_size=8192 no-scroll panic=30 Oct,1,13:23:25,smoothwall-swe3,kernel:,Enabling fast FPU save and rest +ore... done. Oct,1,13:23:25,smoothwall-swe3,smoothd:,Loading Plugins for Module "/u +sr/lib/smoothd/sysinstall.so

Regards,
svenXY

In reply to Re: perl substitution difficulty by svenXY
in thread perl substitution difficulty by nanouk

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.