Deleting a line from one place and inserting it in another place

Deleting a line from one place and inserting it in another place

am 15.07.2011 10:23:10 von melvin

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?
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)

Thanks in advance
Melvin


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Deleting a line from one place and inserting it in anotherplace

am 17.07.2011 11:42:04 von Shlomi Fish

Hi Melvin,

On Fri, 15 Jul 2011 01:23:10 -0700 (PDT)
Melvin 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:



#!/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);
}



Regards,

Shlomi Fish

--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/

There is no IGLU Cabal! None of them could pass the Turing test. But strangely
enough a computer program they coded, could.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Deleting a line from one place and inserting it in anotherplace

am 19.07.2011 12:26:09 von Shlomi Fish

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/repl=
y .
> >
[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 wrote:

> Hi,
>=20
> 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 *be=
low*
the second line. Secondly, with shell scripts you may be prune to shell var=
iable
injection:

http://shlomif-tech.livejournal.com/14671.html

And to other shell-markup injection:

http://shlomif-tech.livejournal.com/35301.html

So be careful.

>=20
> Thanks a lot Shlomif for giving me this detailed example.
>=20

You're welcome.

Regards,

Shlomi Fish

> Thanks,
> Melvin
>=20
> On Sun, Jul 17, 2011 at 3:12 PM, Shlomi Fish wro=
te:
>=20
> > Hi Melvin,
> >
> > On Fri, 15 Jul 2011 01:23:10 -0700 (PDT)
> > Melvin 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 f=
ile
> > 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:
> >
> >
> >
> > #!/usr/bin/perl
> >
> > use strict;
> > use warnings;
> >
> > use File::Slurp;
> >
> > use List::MoreUtils qw(firstidx);
> >
> > my ($extract_string, $put_string, $filenames_file) =3D @ARGV;
> >
> > my $filenames_list =3D read_file($filenames_file, {chomp =3D> 1, array_=
ref =3D>
> > 1});
> >
> > foreach my $filename (@$filenames_list)
> > {
> > my $lines =3D read_file($filename, {array_ref =3D> 1});
> >
> > my $idx =3D firstidx { index($_, $extract_string) >=3D 0 } @$lines;
> >
> > if ($idx < 0)
> > {
> > die "Cannot find '$extract_string' anywhere in $filename.";
> > }
> >
> > my ($extracted_line) =3D splice(@$lines, $idx, 1);
> >
> > my $put_idx =3D firstidx { index($_, $put_string) >=3D 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);
> > }
> >
> >


--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Rethinking CPAN - http://shlom.in/rethinking-cpan

Writing a BitKeeper replacement is probably easier at this point than getti=
ng=20
its license changed.
â€=94 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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/