0

I am looking for some assistance in perl, please. I have most of the code built, but I am finding one part particularly challenging.

If FileA:

tag1=value1
tag2=value2

and FileB:

value1=<tag1>
value2=<tag2>

pseudo code:

open file 1
open file 2
 read line of data from file 1 while data exists
   change the equal sign to a space ( tag1=value1 becomes tag1 value1)
   separate the line into two variables

[magic happens here where I change the value in FileB to the actual value from FileA (see example below)]

close file 2
close file 1

So, I have tried several things, researched the heck out of this using Uncle Google (and here). I know there is a simple way of doing this using a single command line (

prompt> gawk '{sub(/=/," ")}1' [path]/[FileA] | 
    gawk '{system ("perl -pi -e \x27s/"$1"/"$2"/g\x27 [path]/[FileB]")}' 

), but I don't want to do it that way, but instead I am trying to make it happen inside my perl program because I like making things harder on myself it seems :-p.

So, for example, if FileA contains

<tag1>=192.192.2.3
<tag2>=5400

and FileB contains

connect IP=<tag1>
connect port=<tag2>

at the end of this program, I want FileB to contain

connect IP=192.192.2.3
connect port=5400

I understand how to perform substitutions in the program, BUT, I am having difficulty getting it to update the file.

Any hints would be welcomed; I don't even need a full solution, just something to point me in the right direction.

This is not homework.

4
  • Welcome to the site. Please take a look at the editing help and consider revising the overall formatting of your question, because currently it is very difficult to read. Also, if your question is primarily on how to implement this in Perl, it might be better posted over at StackOverflow as it is then rather a programming question than a Unix/Linux-centric one.
    – AdminBee
    Jul 28, 2021 at 15:26
  • 2
    I have attempted to improve the formating of your question but please review and adjust if necessary Jul 28, 2021 at 15:30
  • If you require a solution to be in a particular language, please say so in the question. I removed the perl tag for now.
    – Jeff Schaller
    Jul 28, 2021 at 16:18
  • 1
    I do require it to be in perl, as that is what I am supposed to use.
    – Dragon
    Jul 30, 2021 at 15:05

4 Answers 4

0

Sounds like you want to build a hash table from data in the first file and then use it to modify the second. Roughly speaking, this might give you some ideas

my %my_tags;

while (<$fh1>) {
    chomp;
    my ($tag, $value) = split /=/;
    $my_tags{$tag} = $value;
}

while (<$fh2>) {
    s/(\w+)=(\S+)/$1=$my_tags{$2}/;
    print;
}

If you need more magic in the substitution, look up how to use /e, the evaluate flag.

0

What I would do in this case is the following:

read the tag-file (A) into a hash %tag
read the text-file into an array @text
for keys tag-hash 
    substitute all tags in the array

write the array @text to the textfile

or:

#!/usr/bin/perl
use strict;

my @text;
my %tags;
open (my $TAGS,'<',"fileA") or die "horribly";
while(<$TAGS>){
    if (/<(.*)>=(.*)/){
        $tags{$1}=$2;
    }
}
close $TAGS;
open (my $TXT,'<',"fileB") or die "no B";
@text=<$TXT>;
close $TXT;

for my $key (keys %hash) {
    for (@text){
        s/<$key>/$tags{$key}/;
    }
}

open (my $TXT,'>',"fileB") or die "cannot write a bee";
for (@text){
    print $TXT $_;
}

This code is written untested. You must at least look at the new-lines, that may be a mess. But you said "I don't even need a full solution, just something to point me in the right direction." :-)

0

One way using perl's command line options , They make it function in a quasi-awk manner. You can read up on them in perldoc perlrun:

perl -F= -lane '
  @ARGV and %h=(%h,@F),next;
  print s|=\K.*|$h{$&}//$&|re; 
' fileA fileB

connect IP=192.192.2.3
connect port=5400
0

Thank you, everyone! I will be trying your examples, just so I can learn alternatives to the one that I figured out (with the assistance of my coworkers):

system ( "sed -i s/'${var_one}'/'${var_two}'/g $outfile1" ) ;

For clarity, $var_one and $var_two came from the step "separate the line into two variables".

Thanks, again, everyone;

peace, clark 'the dragon' willis

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.