Add text to beginning every line

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Add text to beginning every line

    Hi

    Hope someone can help with this. I used to be able to do it, but now have
    forgotten. I want to prepend some text to every line in a file. The program
    below:

    #!/usr/bin/perl
    # prepend.pl - adds line number to beginning of every line in a file
    use strict;
    use warnings;

    while (<>) {
    s/^/Line: $. /;
    }

    Then, on the command line I type: perl prepend.pl somefile.txt, but
    somefile.txt does not have the changes(Line: <linenumber>).

    Thanks for any explanation you may offer.

    Chris


    Chris Charley Guest

  2. Similar Questions and Discussions

    1. vi, adding words to the beginning of each line
      in vi-ing a file, i have a list of lines and they have different alphabets when they begin a new line. now i wanted to add this "more" to the...
    2. text line spacing and line tool
      1.) Using InDesign 2.0.2 - When aligning text (in one particular text frame)to a base grid, the text will always skip one line even though the base...
    3. What does a semicolon do at the beginning of a line?
      Was browsing the documentation on reading a configuration file and found this. What does a semicolon do at the beginning of a line? ; <?php DO...
    4. Text.line
      Hi, I need to do a data transfer from an file .txt to cast member, but I need between the transfer, be make a division of text and in this before...
    5. Write line at beginning of file?
      Eric Pement <pemente@northpark.edu> wrote: What version of perl are you referring to?
  3. #2

    Default Re: Add text to beginning every line


    "Chris Charley" <charleyNOSPAM@pulsenetNOSPAM.com> wrote in message news:...
    [snip]
    > while (<>) {
    > s/^/Line: $. /;
    > }
    Should be (works correctly):

    while (<>) {
    s/^/Line: $. /;
    print;
    }

    Then the command could be: perl prepend.pl somefile.txt > somefile.new
    which would correctly print to the 'new' file the somefile.txt file with the
    line numbers prepended.

    Chris


    Chris Charley Guest

  4. #3

    Default RE: Add text to beginning every line


    You're reading in the file, but not changing it. You're only changing it in
    memory. Check out the responses to the other post called "make changes to a
    file, then reading in those changes"

    -----Original Message-----
    From: Chris Charley [mailto:charleyNOSPAM@pulsenetNOSPAM.com]
    Sent: Monday, October 06, 2003 11:20 AM
    To: [email]beginners@perl.org[/email]
    Subject: Add text to beginning every line


    Hi

    Hope someone can help with this. I used to be able to do it, but now have
    forgotten. I want to prepend some text to every line in a file. The program
    below:

    #!/usr/bin/perl
    # prepend.pl - adds line number to beginning of every line in a file use
    strict; use warnings;

    while (<>) {
    s/^/Line: $. /;
    }

    Then, on the command line I type: perl prepend.pl somefile.txt, but
    somefile.txt does not have the changes(Line: <linenumber>).

    Thanks for any explanation you may offer.

    Chris



    --
    To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    Tim Johnson Guest

  5. #4

    Default RE: Add text to beginning every line

    Hi,

    Chris Charley <charleyNOSPAM@pulsenetNOSPAM.com> wrote:
    > Should be (works correctly):
    >
    > while (<>) {
    > s/^/Line: $. /;
    > print;
    > }
    >
    > Then the command could be: perl prepend.pl somefile.txt >
    > somefile.new
    > which would correctly print to the 'new' file the
    > somefile.txt file with the line numbers prepended.
    I would even suggest

    perl -i.bak -pe 's/^/Line $.: /' <filenames>

    This will do the replacement on one or more files
    whose names oyu give on the command line. Perl
    will then create backup files with the .bak extension
    and modify the original files.

    HTH,
    Thomas
    Thomas bätzler Guest

  6. #5

    Default Re: Add text to beginning every line

    Chris Charley wrote:
    > "Chris Charley" <charleyNOSPAM@pulsenetNOSPAM.com> wrote in message news:...
    > [snip]
    > > while (<>) {
    > > s/^/Line: $. /;
    > > }
    >
    > Should be (works correctly):
    >
    > while (<>) {
    > s/^/Line: $. /;
    You don't need a regular expression to do this. This should suffice
    $_ = "Line: $. $_";

    I hope you are aware of the fact that $. does not loop back to 0 when you pass
    multiple files in the argument list.
    When the call is 'perl prepend.pl somefile1.txt somefile2.txt', $. will not
    loopback to 0 when the processing of 'somefile1.txt' finishes and
    'somefile2.txt' starts.
    perldoc perlop #Search for 'null filehandle'
    >
    > print;
    > }
    >
    > Then the command could be: perl prepend.pl somefile.txt > somefile.new
    > which would correctly print to the 'new' file the somefile.txt file with the
    > line numbers prepended.
    >
    > Chris
    >
    > --
    > To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    > For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    Sudarshan Raghavan Guest

  7. #6

    Default Re: Add text to beginning every line

    On Mon, 06 Oct 2003 14:20:05 -0400, Chris Charley wrote:
    > Then, on the command line I type: perl prepend.pl somefile.txt, but
    > somefile.txt does not have the changes(Line: <linenumber>).
    Try writing 'perl prepend.pl somefile.txt > newfile.txt' instead.


    --
    Tore Aursand <tore@aursand.no>

    Tore Aursand Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139