Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

add character to all lines starting with..

by Anonymous Monk
on Sep 24, 2003 at 15:59 UTC ( [id://293893]=perlquestion: print w/replies, xml ) Need Help??

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

Most noble superiors,

I have the honour to request how I (benighted novice that I am) having been unable to mangle what I've seen in 'categorized questions' and 'FAQ', might :

add a per-cent sign to the end of all lines beginning /index

My tonsure comes on mightily with tuggings of the forelock in anticipation.

sativa

  • Comment on add character to all lines starting with..

Replies are listed 'Best First'.
Re: add character to all lines starting with..
by broquaint (Abbot) on Sep 24, 2003 at 16:11 UTC
    If it's just a one off then you can take advantage of perl's command-line switches e.g
    perl -pi -e 'm{^/index} and s/$/%/' file
    Or if you want to do it programmatically and are working with relatively small files then make use of Dominus' marvellous Tie::File
    use Tie::File; tie my @fl, 'Tie::File', "filename.here" or die "ack: $!"; for(@fl) { m{^/index} and s/$/%/; }
    And if you're working with large files then this might be a better solution
    open(my $in_fh, '<', "input.filename") or die "ack: $!"; open(my $out_fh, '>', "output.filename") or die "ack: $!"; while(<$in_fh>) { m{^/index} and $_ .= "%"; print {$out_fh} $_; } close $in_fh; close $out_fh; rename( "output.filename", "input.filename" ) or die "ack: $!";

    HTH

    _________
    broquaint

    update: have fixed code to DTRT, thanks to delirium

      Broquaint,

      All these are going to add the percent sign after $/, no?

      I'd use -l from the command line (e.g., Abigail's solution), or something like

      $_=~s/$/%/;

      replacing the

      $_.='%';

      (Update: autochomp will take care of this in Tie::File)

Re: add character to all lines starting with..
by Abigail-II (Bishop) on Sep 24, 2003 at 16:05 UTC
    perl -wpi -le '$_ .= "%" if m!^/index!' file

    Abigail

Re: add character to all lines starting with..
by sunadmn (Curate) on Sep 24, 2003 at 16:23 UTC
    This sounds like it should be very trivial, but without any code or an example file it is hard to give you the exact way to do this.

    I would suggest giving us a clear example of what your file looks like so we can work on it from there.
      Sunadmn, you're very kind,

      The file is a bibliography file some of whose lines start /index while many others also have a / at the beginning.

      My problem is to add a %-sign at the end of the /index lines.

      I know that it might be simply done in Perl, I just have such little experience yet that trying to piece it together in the on-line manual has been rather a lengthy process.

      Is this enough detail ?

      TIA

      sativa

        I believe that my fellow monks should have given you enough to work with here if not please let us know.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-19 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found