Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

making perl interactive with VI

by NovMonk (Chaplain)
on Feb 17, 2004 at 18:57 UTC ( [id://329681]=perlquestion: print w/replies, xml ) Need Help??

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

(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.

Replies are listed 'Best First'.
Re: making perl interactive with VI
by Paladin (Vicar) on Feb 17, 2004 at 19:15 UTC
    #!/usr/bin/perl print ++$i, " $_" for <>;
    Put the above in a file, make it executable, and call it within vi as :1,17!script and it adds numbers to the lines you selected. Should be simple to go from there to whatever you want perl to do to those lines.

    The line you select in vi will be fed to your script on STDIN and vi will replace those lines with whatever your script outputs.

command line switches
by sleepingsquirrel (Chaplain) on Feb 17, 2004 at 19:47 UTC
Re: making perl interactive with VI
by pizza_milkshake (Monk) on Feb 17, 2004 at 21:18 UTC
    if you're using vim, check out the 'perldo' (:h perldo) command. something like %perldo s/(\d)/$1x$1/ge will apply the perl code to each line in the range that preceeds perldo, in the case of %, all lines. it's quite handy

    perl -e"map print(chr(hex(( q{6f634070617a6d692e7273650a}=~/../g)[hex]))), (q{375542349abb99098106c}=~/./g)"

Re: making perl interactive with VI
by z3d (Scribe) on Feb 18, 2004 at 11:57 UTC
    If your actually using VIM vs. VI, you can find some good examples at http://www.vim.org/scripts/ (search for perl).



    "I have never written bad code. There are merely unanticipated features."
Re: making perl interactive with VI
by Anonymous Monk on Feb 18, 2004 at 21:58 UTC
    Have you thought about a straight vi macro? You can assign just about any series of vi commands to a single keystroke using the 'map' functionality. You can even store commonly used macros in an .exrc file.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://329681]
Approved by kvale
Front-paged by cLive ;-)
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2024-03-28 10:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found