Beefy Boxes and Bandwidth Generously Provided by pair Networks
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??


in reply to re-ordering lines of text in a file?

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.

  • Comment on Re: re-ordering lines of text in a file?

Replies are listed 'Best First'.
Re^2: re-ordering lines of text in a file?
by moddingforfun (Initiate) on Nov 30, 2016 at 14:57 UTC

    In response to your request for better samples and further info...

    Game Website http://www.fleetops.net

    Further explanation of the goal .... When you open a book to read, you expect to find certain things in a certain order. Preface / Forward first. Table of Contents next. After that, the chapters of the book, starting with Chapter 1. etc. That in essence is what I am trying to achieve with these files.

    The template is the ?skeleton?framework?blueprint?form? for the desired end result.

    Un-formated/non-reordered files :

    The full template file :

    Hand Edited File : rom_generix_beamr6.odf

    Look at the "value" of the following variable's in the samples.

    possiblecraftnames = hulltargethardpoints = weaponhardpoints1 = weaponhardpoints2 = etc.

    In attempts to use a bash shell script ( while read / grep AND/OR sed / >> to different directories ) the "value" part of the key pair turned out to be the downfall.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1176853]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-24 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found