Hello Melvin, next time please hit "Reply to all", because this message was sent only to me in private. I specifically request that in my signature:
[QUOTE] > > > > Please reply to list if it's a mailing list post - http://shlom.in/reply . > > [QUOTE] Since you have not specified that this was sent in private, I am CCing this to the list. On Tue, 19 Jul 2011 15:23:11 +0530 Melvin Simon <melvinsimo...@gmail.com> wrote: > Hi, > > I did some sed combined with shell script to get it working as per my > colleague's recommendation. Well, the problem with sed is that it may not work if the first line is *below* the second line. Secondly, with shell scripts you may be prune to shell variable injection: http://shlomif-tech.livejournal.com/14671.html And to other shell-markup injection: http://shlomif-tech.livejournal.com/35301.html So be careful. > > Thanks a lot Shlomif for giving me this detailed example. > You're welcome. Regards, Shlomi Fish > Thanks, > Melvin > > On Sun, Jul 17, 2011 at 3:12 PM, Shlomi Fish <shlo...@shlomifish.org> wrote: > > > Hi Melvin, > > > > On Fri, 15 Jul 2011 01:23:10 -0700 (PDT) > > Melvin <whereismel...@gmail.com> wrote: > > > > > Hi All, > > > > > > I am a newbie to Perl. I wanted to write a below utility:- > > > > > > 1) Read a list of file names (from another file) > > > 2) Search for String1 (this is an entire line in the file names > > > obtained from the list) > > > 3) "gvim-dd" or remove that line and insert it right after another > > > line(after a string2) in the same file. > > > > > > Please could someone advice me if:- > > > > > > 1) Perl is the best option for this or should I use any shellscript? > > > > Perl would be a good option for that. > > > > > 2) If I am using Perl, can I directly use gvim commands such as "dd" > > > or "p" (to remove and paste after the String 2), or does Perl have > > > commands to directly delete and replace within files (I couldn't find > > > this info in the Perl Document with me) > > > > 1. Perl is not a text editor. You need to open a file for reading, read it > > all > > into memory and then write it again (or alternatively use a temporary file > > and > > http://perldoc.perl.org/functions/rename.html it. > > > > 2. Alternatively you can call the list-form of > > http://perldoc.perl.org/functions/system.html with some vim commands for > > help > > (using e.g: > > > > system("vim", "+/string1", "+d", "+/string2", "+put", $filename); > > ) > > > > Here is the way to do it in Perl: > > > > <CODE> > > > > #!/usr/bin/perl > > > > use strict; > > use warnings; > > > > use File::Slurp; > > > > use List::MoreUtils qw(firstidx); > > > > my ($extract_string, $put_string, $filenames_file) = @ARGV; > > > > my $filenames_list = read_file($filenames_file, {chomp => 1, array_ref => > > 1}); > > > > foreach my $filename (@$filenames_list) > > { > > my $lines = read_file($filename, {array_ref => 1}); > > > > my $idx = firstidx { index($_, $extract_string) >= 0 } @$lines; > > > > if ($idx < 0) > > { > > die "Cannot find '$extract_string' anywhere in $filename."; > > } > > > > my ($extracted_line) = splice(@$lines, $idx, 1); > > > > my $put_idx = firstidx { index($_, $put_string) >= 0 } @$lines; > > > > if ($put_idx < 0) > > { > > die "Cannot find '$put_string' anywhere in $filename."; > > } > > > > splice(@$lines, $put_idx+1, 0, $extracted_line); > > > > write_file($filename, @$lines); > > } > > > > </CODE> -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ Rethinking CPAN - http://shlom.in/rethinking-cpan Writing a BitKeeper replacement is probably easier at this point than getting its license changed. — Matt Mackall (who ended up writing a BitKeeper replacement) Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/