Re: Adding a line in a file inside many directories



On Jun 28, 12:00 pm, vedpsi...@xxxxxxxxx (Ved) wrote:
Now I have to print a single line at say line number 20 in each of 150
number of kat.s file.

Hey, another chance to show off my favorite Perl module, IO::All.

Situation is bit complex (atleast for a beginer like me).

Actually it is so simple (with IO::All) that you're gonna think I'm
pulling your leg...

#!/usr/bin/perl
use strict;
use warnings;
use IO::All;

my $dir = '/tmp/clpm'; #root directory of data
my $line = 19; #print line 20 (first line is line zero)
my $file = 'kat.s'; #match this filename

map {print "@$_[$line]\n"}
io($dir)->filter(sub{$_->filename eq $file})->all_files(0);

__END__



That's it. Really.

IO::All is a proxy to a bunch of I/O modules. In this case, it is
doing two things:

io($dir)->filter(sub{$_->filename eq $file})->all_files(0);

This uses File::Find to recurse all directories below $dir and find
all files named 'kat.s'.

map {print "@$_[$line]\n"}

This uses Tie::File to treat the file like an array; I print the line
I want (line #20, which is array element #19).

IO::All provides all of your basic error-handling services, so no need
to futz around with that.

Cheers!


--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)

.