![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| perl -write values in a file to @array in perl | meghana | Shell Programming and Scripting | 27 | 06-07-2009 05:05 PM |
| Accessing single elements of a awk array in END | timj123 | Shell Programming and Scripting | 6 | 06-20-2008 04:53 PM |
| Perl - New line in array elements | bperl | Shell Programming and Scripting | 4 | 04-03-2008 09:56 PM |
| pass each elements in array to ssytem command | jaganadh | Shell Programming and Scripting | 0 | 12-14-2007 06:57 AM |
| accessing variables declared in another perl script | gurukottur | Shell Programming and Scripting | 3 | 11-09-2006 09:22 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
[Perl] Accessing array elements within a sed command in Perl script
I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows:
Code:
$count = 0;
while ( $count < $#test )
{
`sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test[$count]} > 0`;
`cat 0 > ${test[$count]}`;
$count++;
}
`rm 0`;
|
|
||||
|
Like otheus said, why use sed in a Perl script? If you are going to use Perl, you had better learn how to use it properly. Otherwise stick with sed.
Here's a small Perl script that will do what you want and create a backup file to boot. I called this replace.pl Code:
#!/usr/bin/perl
use strict;
use warnings;
use English qw( -no_match_vars );
my $old;
my $new;
my $oldFH;
my $newFH;
my $dirDH;
my $dir = "."; #Use current directory
opendir($dirDH, $dir) or die "Cannot opendir $dir\n";
my @test = readdir($dirDH);
foreach $old (@test) {
next if $old eq '.'; #skip directory entries
next if $old eq '..';
$new = "$old.temp";
open($oldFH, "<", $old) or die "can't open $old: $OS_ERROR\n";
open($newFH, ">", $new) or die "can't open $new: $OS_ERROR\n";
while (<$oldFH>) {
$_ =~ s/BIOGRF 321/BIOGRF 332/g;
print $newFH $_ or die "can't write $new: $OS_ERROR\n";
}
close($oldFH) or die "can't close $old: $OS_ERROR\n";
close($newFH) or die "can't close $new: $OS_ERROR\n";
rename($old, "$old.orig") or die "can't rename $old to $old.orig: $OS_ERROR\n";
rename($new, $old) or die "can't rename $new to $old: $OS_ERROR\n";
}
closedir($dirDH) or die "Cannot closedir $dir\n";
exit 0;
Code:
$ ls -1 test1 test2 test3 test4 $ cat test1 test2 test3 test4 BIOGRF 321 BIOGRF 421 BIOGRF 32X BIOGRF 199 $ replace.pl $ cat test1 test2 test3 test4 BIOGRF 332 BIOGRF 421 BIOGRF 32X BIOGRF 199 $ ls -1 test1 test1.orig test2 test2.orig test3 test3.orig test4 test4.orig The guts of the code comes from The Perl Cookbook. I just wrapped it around a loop that takes all the files from the current directory and performs your replace command. In your case you just need the while loop that walks through your array called @test. |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|