s/// command in an interactive mode. 
Author Message
 s/// command in an interactive mode.

I need to use the s///g command in an interactive mode, by which I
mean, within say a while loop, I want to have the command match the
first pattern on the line, then (perhaps after showing me the line
on which the hit occurred), prompt me for a yes/no response, and
then proceed (or not) to do the substitution based on the response
and go on to the next match on the line (if it exists) and repeat the
process.
Essentially I want to break into the middle of the s///g command
with my routines to accomplish the above then continue with the
substition if called for by the response.

One possibility is to use  the m//g command, but then I am not sure
how to do what the substitution portion of the s///g command did on
the pattern that was matched by the m//g, (in the original context,
not some new variable).

Please restrict your response to a subset of perl available in the
DOS ports of perl.

Thank you very much for the help.
Henry Manaster

--
***************************************************************************

        Brooklyn, NY            *
                                *
        Disclaimer: The above is not necessarily MY opinion nor that
                                of anyone else :-)  ????!
****************************************************************************



Mon, 03 Jul 1995 08:05:12 GMT  
 s/// command in an interactive mode.

Quote:

>I need to use the s///g command in an interactive mode, by which I
>mean, within say a while loop, I want to have the command match the
>first pattern on the line, then (perhaps after showing me the line
>on which the hit occurred), prompt me for a yes/no response, and
>then proceed (or not) to do the substitution based on the response
>and go on to the next match on the line (if it exists) and repeat the
>process.
> ...

You can use the s///ge that evaluates an expression. If you specify
a procedure as your expression you cat ask before deciding on the
replace string.

I just made up this script that replaces a string (use any perl search pattern.
Ie. specify \b if you want to match a word) with another (haven't tested wether
$1, $2, ... works) in a numer of files.

#!/usr/local/bin/perl
$hilight     = "\033" . '[7m';
$end_hilight = "\033" . '[0m';

$pattern = shift;       #  First argument is search string
$repl    = shift;       #  Second argument is replace string

 {
  next unless open(INFILE, $file);
  $contents = '';
  $contents .= $_ while <INFILE>;   # Read all of file into $contents
  close INFILE;

  $replaced = 0;

  $contents =~ s/$pattern/&replace/goe;

  next unless $replaced;

  print "writing back file\n";

  rename($file, $file.".bak");

  open(OUTFILE, ">".$file);
  print OUTFILE $contents;
  close(OUTFILE);
 }
------ end of script -----

  Hope this helps
  Erik Johansen

---
    $txt=" ltrterhnuc--sor eep-\nkJ.a "; srand(53747414);
    for (1..26) { print substr($txt,rand 27,1); }  ### Is this a random write ?



Mon, 03 Jul 1995 16:57:18 GMT  
 s/// command in an interactive mode.
 [ Parts of my script went missing, so here it is again ]

Quote:

>I need to use the s///g command in an interactive mode, by which I
>mean, within say a while loop, I want to have the command match the
>first pattern on the line, then (perhaps after showing me the line
>on which the hit occurred), prompt me for a yes/no response, and
>then proceed (or not) to do the substitution based on the response
>and go on to the next match on the line (if it exists) and repeat the
>process.
> ...

You can use the s///ge that evaluates an expression. If you specify
a procedure as your expression you cat ask before deciding on the
replace string.

I just made up this script that replaces a string (use any perl search pattern.
Ie. specify \b if you want to match a word) with another (haven't tested wether
$1, $2, ... works) in a numer of files.

------ Start of script -----
#!/usr/local/bin/perl
$hilight     = "\033" . '[7m';
$end_hilight = "\033" . '[0m';

$pattern = shift;       #  First argument is search string
$repl    = shift;       #  Second argument is replace string

 {
  next unless open(INFILE, $file);
  $contents = '';
  $contents .= $_ while <INFILE>;   # Read all of file into $contents
  close INFILE;

  $replaced = 0;

  $contents =~ s/$pattern/&replace/goe;

  next unless $replaced;

  print "writing back file\n";

  rename($file, $file.".bak");

  open(OUTFILE, ">".$file);
  print OUTFILE $contents;
  close(OUTFILE);
 }

sub replace
 {
  local( $and, $left, $right ) = ( $&, $`, $' );
  local( $result );
  local( $pre, $suf );
  $pre = $1 if $left  =~ /\n([^\n]*)$/;
  $suf = $1 if $right =~ /([^\n]*)\n/;
  print $pre,$hilight,$and,$end_hilight,$suf,"\n";

  print "Replace ?";
  if (<STDIN> =~ /^y/i)
   {
    $replaced++;
    $result = $repl;
   }
  else
   {
    $result = $and;
   }
  ( $&, $`, $' ) = ( $and, $left, $right );
  $result;
 }

------ End of script -----

  Hope this helps
  Erik Johansen

---
    $txt=" ltrterhnuc--sor eep-\nkJ.a "; srand(53747414);
    for (1..26) { print substr($txt,rand 27,1); }  ### Is this a random write ?



Mon, 03 Jul 1995 22:26:46 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Interactive program with command line history/vi mode--help needed

2. Interactive perl mode for Perl5?

3. iperl - quick interactive Perl command

4. interactive command line in perl script

5. interactive shell command from perl

6. Passing arguements to interactive shell commands

7. Interactive commands: htpasswd

8. offline mode: Command to continue is???

9. system call works in command line mode but not from web

10. Enhanced cperl-mode.el (Perl editing mode for Emacs)

11. cperl-mode: Another perl mode for Gnuemacs

12. Character-mode client versus line-mode server

 

 
Powered by phpBB® Forum Software