Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
(see updates below):

Greetings Monks.

I want to write a program I can call while editing a file with VI and have it perform some actions on the lines of text I have selected, something like

:1,17! perlprogram

Where would I go to find some good examples of how to think about this? I can write something that would look though a whole file, but I want to be more selective about what gets changed. I want it to look at each line within those 17 and do things if it matches certain Regular Expressions.

This may seem hopelessly archaic (it is), but I work for a company where we are making tons of these changes by hand, and a utility that would do this for us Might make people more willing to looks at more interesting uses for Perl. I've got a couple of seds that do it now, but a one shot program would be Much better.

Thanks in advance.

(update #1)

Ha! I figured it out. Some of it anyway. This works:

#!/usr/bin/perl while (<>) { if (/'5'/) { s/[Nn]01/N01(+5) /g; s/'5'/'5';fac=5/g; } if (/'4'/) { s/[Nn]01/N01(+4) /g; s/'4'/'4';fac=4/g; } print "$_"; }

Thanks Paladin-- your example with the <> got me on the right track of profitable trial and error. And I will look more closely at the command line switches, too, sleepingsquirrel. I'd still appreciate more comments and ideas. Thanks again.

(Update #2)

Does anyone have any idea why the file so edited has a blank space at the beginning of each line? Here's a simpler (and cooler) program for reference:

#!/usr/bin/perl -w while (<>) { s/[Nn]01\(\+.\) /n01/g; s/;fac=.//g; unless (/^net/i) {print "$_";} }

I am kind of proud of figuring out a way to get it to delete lines containing "net" when

s/^net/d

didn't work (Sorry, I learned sed first.) Thanks again, everyone.

Update 3:

Blank space was in the test file I was using. Nevermind. Thanks.


In reply to making perl interactive with VI by NovMonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (6)
    As of 2020-10-22 22:11 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My favourite web site is:












      Results (230 votes). Check out past polls.

      Notices?