How to use perl to modify & combine two files

August 29, 2011 at 04:12:16
Specs: Windows Server 2008
Hi all,

I need a quick solution to do a little job. I'm a complete newbie to Perl and while I'm usually happy to take some time to learn (and believe me I will), time is against me to get this one thing done.

I have two files - HEADEROPENtimestamp.txt and DATAOPENtimestamp.txt

The timestamp is variable but will be the same on both files, and these will be the only two files in that folder with the format HEADEROPEN*.txt and DATAOPEN*.txt.

HEADEROPEN has two lines of text, and DATAOPEN has many. I need to strip the first line from both txt files, then combine them into a new file (header first, then data) called FINALOPENtimestamp.txt

I've looked at other posts on here and can see how to do bits of this, but I'm having trouble putting it all together! I'd prefer to do it through command line if possible as it will be run as part of a scheduled batch file (usually the files are already formatted and I don't have to mess with them, but that's not been possible on this occasion!)

Any help would be much appreciated :)

See More: How to use perl to modify & combine two files

Report •

September 12, 2011 at 09:34:20
I think I can help on this one jaymanson.

The first thing you need to do is get the batch file to find the filename with the timestamp variable intact, and load it into a temp file. This would go something like this:

dir /b HEADEROPEN* > filename.tmp

Now we take that tmp and load the filename into a variable

set /p filename1var=<filename.tmp

Now we use that filename to create another variable that will hold the first line of the contents of the file

set /p headerline=<%filenamevar%

Now we do the same thing for the dataopen file:

dir /b DATAOPEN* > filename.tmp

set /p filename2var=<filename.tmp

set /p dataline=<%filenamevar%

Now we just have to echo those two variables into finalopentimestamp.txt

echo headerline > finalopentimestamp.txt
echo dataline >> finalopentimestamp.txt

I think we'll need to do some more modifications for the time stamp variables, but let me know if this is on the right path.

Report •
Related Solutions

Ask Question