Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: re-ordering lines of text in a file?by Laurent_R (Canon) |
on Nov 29, 2016 at 20:16 UTC ( [id://1176853]=note: print w/replies, xml ) | Need Help?? |
Yes, it is quite easy to do that in Perl. Probably not much more than a dozen code lines.
One way to do it would be first to read the template file and store its contents in memory (probably as an array), so that you can then read it in order for each of your data files. Then read your file list line by line, and, for each line, read the corresponding file content into a hash. Then scan the array, lookup the corresponding file content, put the file content into the right order, and dump it to a file. One thing to take into account is that you normally can't edit a file in place (well, you can with a work around, but it's better and safer not to do it). So it is probably easier to output your result into files having the same names as the input files, but in another directory. Then you remove the old files only when you're confident that they have been successfully converted. This is much safer. We would really need a sample of the input file to help further.
In Section
Seekers of Perl Wisdom
|
|