Sponsored Content
Top Forums Shell Programming and Scripting [Perl] Accessing array elements within a sed command in Perl script Post 302243017 by SFNYC on Friday 3rd of October 2008 12:05:25 PM
Old 10-03-2008
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;

Output:

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.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl - New line in array elements

Hello, I have a comma delimited input feed file. The first field has directory location and the second field has file name. Ex of input feed: /export/appl/a,abc*.dat /export/appl/b,xyz*.dat /export/appl/c,pmn*.dat Under each directory, there would be many files like... . . .... (4 Replies)
Discussion started by: bperl
4 Replies

2. Shell Programming and Scripting

Accessing single elements of a awk array in END

How do I access one of the indices in array tst with the code below? tst=sprintf("%5.2f",Car / 12) When I scan thru the array with for ( i in tst ) { print i,tst } I get the output of: vec-7 144 But when I try this in the END print tst It looks like it's not set. What am... (6 Replies)
Discussion started by: timj123
6 Replies

3. Shell Programming and Scripting

Accessing array elements

Hi, My doubt is how to access array elements.. Situation is as below: #!/bin/ksh set -x typeset -i x=0 typeset -i y=0 typeset -i BID=0 typeset -i count=0 while ] ; do x=`expr $x + 1`; hwmgr show scsi > scsi.tmp while read line; do set... (1 Reply)
Discussion started by: mansa
1 Replies

4. Shell Programming and Scripting

Perl:Use of array elements in pattern matching

I need to use array elements while pattern matching. @myarr = (ELEM1, ELEM2, ELEM3); following is the statement which I am using in my code. Basically I want to replace the ELEM1/2/3 with other thing which is mentioned as REPL here. if (condition) { s/(ELEM1|ELEM2|ELEM3): REPL: /; } I... (3 Replies)
Discussion started by: deo_kaustubh
3 Replies

5. Shell Programming and Scripting

sed command in perl script

What is wrong with this line in a perl script? $amc_data = `sed -n '/\/,/\/p' "$config_file"` I ran the above from command line and it works fine from unix command prompt. The code should produce output between the and tags. The config_file is as follows: Sun ... (2 Replies)
Discussion started by: som.nitk
2 Replies

6. Shell Programming and Scripting

Perl Array Elements Replacement

Hello, I have the following perl array: @longname = (Fasthernet0/0 Fasthernet0/1 Serial0/1/0 Serial0/2/1 Tunnel55 Tunnel77) with the followinh array: @shortname = (Fa0/0 Fa0/1 Se0/1/0 Se0/2/1 Tu55 Tu77) in other words, I need to remove the following from each element in the array... (4 Replies)
Discussion started by: ahmed_zaher
4 Replies

7. Shell Programming and Scripting

Problem accessing array elements

Hi all, I can’t resolve an array element access issue on (Linux/pdksh) .. So I’m positing for advice.By the way - a friend demonstrated to me - same script works as expected under Solaris. I have been working on a documentation project where many *.jpg screen shots are used in the... (4 Replies)
Discussion started by: njdpo
4 Replies

8. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

9. Shell Programming and Scripting

Array elements comparison using perl

Experts, I am looking to compare elements of 2 array using perl. Below is not the actual code but logic wise something like this. my $version = "MYSQlcl-5.2.4-264.x86_64"; <-- split this word into array as (5 2 4 264) ( which is to extract only the version number from the package name) my... (1 Reply)
Discussion started by: solaix14
1 Replies

10. Shell Programming and Scripting

Storing the Linux command output to an array in perl script

Hi I am trying to store the output of a command into an array in perl script. I am able to store but the problem is i am unable to print the array line with one line space. i mean i inserted the \n in loop ...but not getting the result. I have written like this #!/usr/bin/perl @a =... (2 Replies)
Discussion started by: kumar85shiv
2 Replies
Perl::Critic::Policy::Modules::RequireNoMatchVarsWithUseUseriContributed Perl DoPerl::Critic::Policy::Modules::RequireNoMatchVarsWithUseEnglish(3)

NAME
Perl::Critic::Policy::Modules::RequireNoMatchVarsWithUseEnglish - "use English" must be passed a "-no_match_vars" argument. AFFILIATION
This Policy is part of the core Perl::Critic distribution. DESCRIPTION
Due to unfortunate history, if you use the English module but don't pass in a "-no_match_vars" argument, all regular expressions in the entire program, not merely the module in question, suffer a significant performance penalty, even if you only import a subset of the variables. use English; # not ok use English '-no_match_vars'; # ok use English qw< $ERRNO -no_match_vars >; # ok use English qw($OS_ERROR); # not ok In the last example above, while the match variables aren't loaded into your namespace, they are still created in the "English" namespace and you still pay the cost. CONFIGURATION
This Policy is not configurable except for the standard options. AUTHOR
Elliot Shank "<perl@galumph.com>" COPYRIGHT
Copyright (c) 2008-2011 Elliot Shank. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module. perl v5.16.3 2014-06-09 Perl::Critic::Policy::Modules::RequireNoMatchVarsWithUseEnglish(3)
All times are GMT -4. The time now is 04:56 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy