parse three files

Hi,

In a directory I have these text files with the following names (the format is stringinfirstfile_users.txt). So what I need to do is through the following algorithm

1. Open each file in the directory which end in _users.txt
2. Grab the string just before _users.txt so it will be for e.g MI_AP_L2_NRRT (store it in variable called group)
3.  Read the contents of the file so if it contains any text  for e.g

t1
t2
t3

the output should be to a file

MI_AP_L2_NRRT_add.scp with the strings

add user "t1" to group "MI_AP_L2_NRRT";
add user "t2" to group "MI_AP_L2_NRRT";
add user "t3" to group "MI_AP_L2_NRRT";

please note that this MI_AP_L2_NRRT comes from the variable group (which we stored from the file name)

------------------------------------------------------------------------------------------------------------





MI_AP_L2_NRRT_users.txt
MI_AP_L2_RRT_users.txt
MI_AP_NL2_NRRT_users.txt
MI_AP_NL2_RRT_users.txt
MI_EU_CapacityPlanning_users.txt
MI_EU_L2_NRRT_users.txt
MI_EU_L2_RRT_users.txt
MI_EU_NL2_NRRT_users.txt
MI_EU_NL2_RRT_users.txt
MI_NA_BIDeveloper_users.txt
MI_NA_L2_NRRT_users.txt
MI_NA_L2_RRT_users.txt
MI_NA_NL2_NRRT_users.txt
MI_NA_NL2_RRT_users.txt
MI_OthersL2_users.txt
MI_Others_users.txt

--------------------------------------------------------------------------------------------------------------------

Another option could be to generate an xml file but I will create another thread for it.

thanks
-anshu








anshumaEngineeringAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oleberCommented:
so lets do this by parts.

you need to find the files

while (my $file = <*_users.txt>) {
   ...
}



now you nead to collect the string

if ($file =~ /(.*)_users.txt/) {
   my $str = $1;
   ...
}



now you need to colect the first line of the file

open(my $FH, $file) or die "can't open file $file= $!";
my $user = <$FH>;
close($FH);
chomp $user;

And finalize with a print to the output
print "add user \"$user\" to group \"$str\";\n";

The script will print to the STDOUT, so you need to redirect to your file.
while (my $file = <*_users.txt>) {
   if ($file =~ /(.*)_users.txt/) {
      my $str = $1;
      open(my $FH, $file) or die "can't open file $file= $!";
      my $user = <$FH>;
      close($FH);
      chomp $user;
      print "add user \"$user\" to group \"$str\";\n" if $user;
   }
}

Open in new window

0
ozoCommented:
@ARGV=<*_users.txt>;
while( <> ){
  if( $ARGV ne $oldargv ){
    ($g = $ARGV) =~ s/_users\.txt//;
    open ARGVOUT,">${g}_add.scp" or warn $!;
    select ARGVOUT;
    $oldargv=$ARGV;
  }
  s/(.*)/add user "$1" to group "$g"/;
  print;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nitinsawhneyCommented:
Hi,

I am asuming here that the directory name is users/

Cheers
#!/usr/bin/perl
use strict;
 
my @files = `ls users/*_users.txt`
 
foreach my $file(@files){
    my ($group) = $file =~ /(.*)_users\.txt$/;
    open(INFILE, "<users/$file") || die "Unable to open file";
    open(OUTFILE, ">>users/$group_add.scp";
    while(<INFILE>){
        my $line = chomp($_); #asuming that t1, t2 etc. are in separate line
        print OUTFILE "add user \"$line\" to gropu \"$group\";";
    }
    close INFILE, OUTFILE;
}

Open in new window

0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

nitinsawhneyCommented:
forgot to close the parenthesis for out file.

That line should read as:  open(OUTFILE, ">>users/$group_add.scp");
0
oleberCommented:
'my $line = chomp($_); ' doesn't do what you are thinking.
0
oleberCommented:
Seems that I read the question in a wrong way so you can find a small fix in my script.


Note that I'm considering that you are in the directory of the files. If this isn't the case, you can use the File::Spec to get the filename:
($volume,$directories,$file) = File::Spec->splitpath( $path );



while (my $file = <*_users.txt>) {
   if ($file =~ /(.*)_users.txt/) {
      my $str = $1;
      open(my $FH, $file) or die "can't open file $file= $!";
      while (my $user = <$FH>) {
         chomp $user;
         print "add user \"$user\" to group \"$str\";\n" ;
      }
      close($FH);
   }

Open in new window

0
anshumaEngineeringAuthor Commented:
Hello Ozo,

Your script is working fine. Could you please tell me how can I append a ";" after every line in the output.

I need the output like this (note the semi colon)

add user "t1" to group "MI_AP_L2_NRRT";
add user "t2" to group "MI_AP_L2_NRRT";
add user "t3" to group "MI_AP_L2_NRRT";
0
Adam314Commented:
In ozo's script, change this:
    s/(.*)/add user "$1" to group "$g"/;
to this:
    s/(.*)/add user "$1" to group "$g";/;

0
anshumaEngineeringAuthor Commented:
Excellent solutions. Thanks to all of you. Kind regards to all of you

thanks
-anshu
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.