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?
 
ozoConnect With a Mentor Commented:
@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
 
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
 
nitinsawhneyConnect With a Mentor Commented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
oleberConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.