Mocky123 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, I got stuck with a inserting a line in a between text file. How am I suppose to insert a new line "horse" into text file of line 6 if my text file contain the following:

1

2

3 ################

4 cat

5 dog

6

7 ################

8 tall

9 short

10

11 ##############

12

Thanks.

Replies are listed 'Best First'.
Re: How to insert a new line?
by davido (Cardinal) on Nov 01, 2014 at 15:26 UTC

    Mocky123: It's unfortunate that the previous answers have been hasty and glib (and if they haven't, I pre-apologize for misreading their tone). Your question, while showing a lack of effort, is not uninteresting. But when people are handed some data and asked for a solution, they understandably assume that their kindness is being abused through the laziness of the person asking for help. My first reaction was the same, "Show us what you've tried, or go try something and let us know where you got stuck." ...that's really not bad advice. But I do think there's something more interesting still left to discuss, even if the question is low-effort.

    There are actually two parts to your problem. We tend to gravitate to what seems like the simplest answer. A simple answer is that you need to read the file line-by-line, printing each line out to a new file. As you arrive at 6, you need to add the text 'horse', and then print that line. Then continue on line-by-line reading and printing to the end of the file. When you're done, the new file can replace the old one.

    That's treating the file as just text, which seems simple, though it could be deceptively so.

    This file has some sort of structure. Lines 4, 5, and 6, probably correspond to lines 8, 9, and 10. There may also be a correlation with lines 1, 2, and 12; we aren't sure by your explanation of the data. If it turns out that cat => tall, dog => short, and horse => skinny, it's probably better to treat the file as a data format that can be thawed/parsed into a data-structure, and later frozen/encoded into the file format again.

    Consider, for example, JSON. Most solutions that deal with JSON just parse it into a structure as it comes in, deal with it in its datastructural format internally, and then reencode it to its JSON representation after manipulations are complete. It's not considered a generally sound practice to directly modify the JSON file without first parsing it on a semantic level.

    Your data may fall into that category, and if we knew more about it, we might provide more useful advice.

    Decode -> Manipulate Datastructure -> Encode.

    So the two parts to your problem are (1) Data representation, and (2) Semantic meaning of the data. The answer that seems the simplest at the outset may not be the simplest after all. Unless this is a one-off, it's usually simpler to separate concerns; code to deal with the freezing/thawing of the data, and code to manipulate the internal representation of the thawed data.


    Dave

Re: How to insert a new line?
by soonix (Chancellor) on Nov 01, 2014 at 08:48 UTC
Re: How to insert a new line?
by GrandFather (Saint) on Nov 01, 2014 at 03:05 UTC

    What have you tried and how did it fail? Have you read the file open documentation and other file related stuff?

    Perl is the programming world's equivalent of English
Re: How to insert a new line?
by boftx (Deacon) on Nov 01, 2014 at 02:27 UTC

    Well, you start by saying you have a homework assignment you need help with.

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re: How to insert a new line?
by NetWallah (Canon) on Nov 01, 2014 at 06:41 UTC
    Smells like homework .. buth .. here is one way:
    perl -pe '$.==6 and $_.="horse\n"' inp.file > out.file
    Assumes you want "horse" AFTER line 6.

            "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Re: How to insert a new line?
by Anonymous Monk on Nov 01, 2014 at 02:30 UTC
Re: How to insert a new line?
by rocketperl (Sexton) on Nov 01, 2014 at 06:26 UTC

    print "\nhorse\n" if $_=~m/^dog$/