1

I posted a question a month or so ago about how to print out a listing of files and all occurrences of a regular expression in those files. The aim being to get a file like this:

    file1 A12345
    file2 A12399
    file2 A12599
    file3 A11223

and so on.

After some help from Miller and anttix (thanks again!), the expression wound up being:

perl -lne 'print "$ARGV $1" while(/\<myxmltag>(..............)<\/myxmltag>/g)' *.XML >  /home/myuser/myfiles.txt

Now I'd really like to be able to add the file's create date (in any format), so my result would be something like this (as before: exact format, order of fields etc. does not matter):

    file1 A12345 01/01/2013
    file2 A12399 02/03/2013
    file2 A12599 02/03/2013

I've looked around and had no luck figuring out how to do this in a one-liner. Lots of examples involving full Perl programs with opendir and variables and so on; but as someone who hasn't written Perl in 20 years, I'm trying to be minimalistic.

I attempted to add $ARGV[9], @ARGV[9] and similar but none of them did the job.

Any suggestions on what to try?

phuclv
  • 37,963
  • 15
  • 156
  • 475
JOATMON
  • 89
  • 2
  • 11
  • If you are sticking to a one-liner for simplicity then you are misguided. A proper program will be much more readable, and simpler to debug and modify. A one-liner generally doesn't have the benefits of indentation to reveal its structure, nor the watchful eye of `use strict` and `use warnings` to help you to avoid trivial mistakes – Borodin May 05 '14 at 16:51
  • In linux there is no such thing as a create date. There is change time, modification time and access time. Also what Borodin said. – mofoe May 05 '14 at 16:53
  • mofoe is correct. However, if nobody has changed the owner, group, or other file info (stored in the inode) since creation, the change time will be the creation time. So change time might suffice for your purposes. – Len Jaffe May 05 '14 at 18:06
  • Thanks - yeah "create date" isn't too precise, but modification time is close enough as that's when the file would have been created. I'd been playing around with stat as mpapec suggested but haven't been able to get that to work, either it gives an error or it just prints "stat whatever", e.g. "perl -lne 'print (stat ARGV)' *.txt | head" gives me a bunch of strings of long numbers (possibly system file IDs?). I'll check the suggestions below. – JOATMON May 05 '14 at 18:10

1 Answers1

1

Assuming you really want to stick with a one liner and you want the modification time, try this:

perl -MTime::localtime -lne 'print "$ARGV  $1 ". ctime((stat "$ARGV" )[9]) while(/\<myxmltag>(..............)<\/myxmltag>/g)' *

This imports the Time::localtime module. From that module ctime() is used to convert the result of (stat filename)[9] (the modification time is the 9th element in the array returned by stat()) from a unix time stamp into a more readable format.

Another variant would be to use strftime() from the POSIX module:

   perl -MPOSIX -lne 'print "$ARGV $1 " . strftime("%d/%m/%y", localtime((stat "$ARGV")[9])) while(/\<myxmltag>(..............)<\/myxmltag>/g)' *
mofoe
  • 3,634
  • 1
  • 16
  • 16
  • Thanks! The first version (with -MTime) worked perfectly. The second option for some reason reported the same date value for everything (and I'm pretty sure the files were not created in 1969, LOL). I'm also pretty sure I was doing something wrong! Still, knowing about the two variants gave me something to start with for an urgent deadline, and also something to learn more about. Again, thanks!! – JOATMON May 09 '14 at 18:45
  • Sorry. Fixed. Thanks for noting! – mofoe May 09 '14 at 23:44