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

I'm interested in prepending a certain character to every line of a file. Sort of like what happens when I hit Reply in Pine and all lines are prepended with ">".

What's a good way of doing this? Thanks for your help.

Replies are listed 'Best First'.
Re: Prepending characters to lines in file
by John M. Dlugosz (Monsignor) on Aug 03, 2001 at 03:13 UTC
    Read each line:
    while (<>) {
    prepend the stuff and write it out
    print "> ", $_;
    You can do that using command-line switches and stuff:
    perl -i.bak -n -e "print '> ', $_" file1.txt file2.txt etc.etc
    Look up -i -e -n in the perlrun man page. Get a copy of the Perl Cookbook, or one of the introductary texts.

    —John

      Thanks for the help.
Re: Prepending characters to lines in file
by slayven (Pilgrim) on Aug 03, 2001 at 04:12 UTC
    i like perlvars :)
    perl -i.bak -pe '$\ = "> ";' <filename>
    $\ The output record separator for the print operator.

    slayven

      I also like perlvars, but only if they work. $\ is the output record separator, which means it is printed after each line (and thus before the next). But this leaves the first line without '> ' and introduces an extra '> ' after the last.

      And while I'm here let's throw in yet another wtdi perl -i.bak -pe 'substr($_,0,0) = "> "' file

      -- Hofmator

Re: Prepending characters to lines in file
by wardk (Deacon) on Aug 03, 2001 at 03:15 UTC
    perl -i.bak -pe 's/$_/> $_/' filename
    could be one way
      That will work as long as your lines don't contain regex metacharacters. :)

      You don't really need to match the line itself; just matching the beginning of the line works fine: perl -pi.bak -e 's/^/> /' filename

Re: Prepending characters to lines in file
by blakem (Monsignor) on Aug 03, 2001 at 03:56 UTC
    If you already have it slurped into a variable (say $data) the following line should work:
    $data =~ s/^/> /mg;

    -Blake

      Well, you could use a regular expression if you had something against $data = "> $data"; =)

      But the regex do gots the cool factor.

      perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
        Um, those aren't the same.....
        my $data = <<"EOF"; I really like jellybeans of all colors and flavors pink lemonaide blue raspberry and orange marmalade are great EOF print "First try:\n"; print "> $data\n"; print "Second try:\n"; $data =~ s/^/> /mg; print "$data\n";
        Output
        First try: > I really like jellybeans of all colors and flavors pink lemonaide blue raspberry and orange marmalade are great Second try: > I really like > jellybeans > of all colors > and flavors > pink lemonaide > blue raspberry > and orange marmalade > are great

        -Blake

Re: Prepending characters to lines in file
by pokemonk (Scribe) on Aug 03, 2001 at 11:14 UTC
    s/\n/\n>/g;