I'm trying to rename a set of files based on what is in the file itself. I have likely more than a thousand files to rename by the time I'm done. I have code that will get me the first part of the file name, but I need to include other elements in the file name. Here's the first part of the code I am using that I use daily on another task.

# open(INFILE,"list.txt") or die "could not open file for reading!\n";
open(OUTFILE, '>forecast_mips.txt') or die "Cannot open forecast.txt: $!";

my @output;

while(<INFILE>) {
    next if /^\s*$/;

    my @fields = split;

    foreach my $field (@fields) {
            push @output, $field
            if $field =~ /^\*?[ABMQRWY][A-Z0-9]{4}235/;

    foreach my $field1 (@fields

print OUTFILE join("\n", @output), "\n";
print join(@output),"\n";

I'm trying to include two other elements in the file name though. In the file are the Jobname which the above code produces, but I also need the Job Number, a 5 digit number, and the date of the run which will be a 5 digit Julian fixed date. The following is what the file will be based on. The bolded text is what I need in the file name. The date will need to be a Julian date, in this case 22356. So with the below, The filename would be D47YC235#03618-22356. Not sure if there's a better way in retrieving the below items and setting the file name. The if $field =~ line produces the D47YC235 from the file.

00.55.30 JOB03618  ICH70001I $$MP235  LAST ACCESS AT 00:55:30 ON THURSDAY, **DECEMBER 22, 2022**
00.55.30 JOB**03618**  $HASP373 **D47YC235** STARTED - INIT 39   - CLASS R        - SYS V4YM
  • Hi and welcome here! But: Hey, this is really a Perl programming question and should rather be asked on stackoverflow.com. (I see you already have an account over there – so it'd be really easy to ask the same over there.) Mar 7 at 17:45
  • 1
    Can be answered here, just need some clarity of sample input/expected output in a minimal and reproductible way, the question is long and confusing. Mar 7 at 17:53
  • 1) input files 2) what you need to retrieve in these files 3) expected renaming. Then we can answer. Use code blocks of filenames, try to be concise and readable Mar 7 at 17:56
  • When post edited, post a comment. Good luck Mar 7 at 20:02
  • Are there always those two lines in each file that is to be processed? Are they in the same order? How sure are you about the consistency: what if some files deviate from this in some way? Your original code for the other task seems to skip lines consisting of spaces; are those expected? Are the ** characters just your annotations indicating what text you are scraping from the file?
    – Kaz
    May 18 at 23:03

1 Answer 1


Assuming the files start with two lines in the indicated format, and we have their names on the command line, a TXR program to calculate the rename looks like this:

$ txr data.txr data
rename data -> D47YC235#03618-22356
$ txr data.txr data data data data
rename data -> D47YC235#03618-22356
rename data -> D47YC235#03618-22356
rename data -> D47YC235#03618-22356
rename data -> D47YC235#03618-22356

Where the data.txr program is:

@(next :args)
@  (next filename)
@nil JOB@job @nil @nil LAST ACCESS AT @time ON @wday, @date
@nil JOB@job @nil @code @nil
@  (bind newname `@code#@job-@{(time-parse "%b %d, %Y" date).(time-string "%y%j")}`)
@  (do (put-line `rename @filename -> @(path-cat (dir-name filename) newname)`))

This only prints the renames in shell command syntax without actually performing them for informational purposes. To actually do the rename we could use the rename-path function in TXR Lisp:

@  (do (rename-path filename (path-cat (dir-name filename) newname)))

Note that we extract the job variable from both lines. That is backreferencing: the two places have to match, otherwise that file will be skipped.

Code with Vim syntax highlighting:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .