Regex to split a string and write the output in another file.

hi,
i am trying to write a script to generate ouput in the following format:

##### buildappi    abcd_sh    nodebug.#####
##### buildappi    ijk_sh       nodebug.#####

The given string is as follows:

xtopSharedDLLs = "abcd_sh def_sh ijk_sh " \
                       + "jkl_sh any_sh anyvaria_sh appintf_sh 
jhjkfhk_sh jkdfljklljl_sh " \
                       + "klskdfkkl_sh jsldsdfj_sh hjksdfhuio_sh dimutils_sh 
imdnkhf_sh ouasidfjash_sh kjdfklj_sh hkasdfhuasfih_sh " \
                       + "klanhsdfklahslkf_sh hjasdfjkah_sh ";

The logic behind is that i tried to reach at xtopSharedDllls first and split the values in Tokens and write the output in another file in the format mentioned above.

however I am looking for some help to generate any proper script for that.

please assist me as i am a beginner in perl script.

I have tried to generate the following code.
Please let me know my mistakes and solutions for the same.

#! /usr/bin/perl -w
#

#use strict;
#use warnings;
#
#
	
	$filename= "xtop_env.txt";
	
	$my_outfile = "output.txt";
	
	open (HANDLE,"<$filename");
	
	open (OUTPUT, ">$my_outfile"); 
	
	
	while(<HANDLE>)
	{
				
                if ($_ =~ m/$xtopSharedDLLs/)
			    @mytoken = split($_);
				for each $mytoken(@mytoken)
				 {
					if ($mytoken =~ m/\=\"\\/)
					next;
					print "OUTPUT @mytoken\n";
				 }
				last if(/;/);
				close OUTPUT;
				 
	}
	
	close HANDLE;
perl -lne 'BEGIN{open O,">output.txt"};
if(/^xtopShared/../;$/) {
    while (/(\w+?_sh)/g) {print O "##### buildapi $1 nodebug.####"}
}; END{close O}' xtop_env.txt

thanks a lot balajesuri for the reply.

it would be a great help if you could explain me a little about the logic.

Thanks.

perl -lne # Invoke perl with options -l -n and -e.
-l for adding \n each time it prints something. 
-n to read the input file line by line. 
-e to tell the shell that the following instructions are commands to be sent to the perl interpreter.
'BEGIN{open O,">output.txt"}; # BEGIN block runs only once at the start of the program. Opens the file output.txt in handle O for writing.
if(/^xtopShared/../;$/) { # Find lines between (and including) patterns /^stopShared/ and /;$/. In the world of perl, this is known as flipflop operator
    while (/(\w+?_sh)/g) {print O "##### buildapi $1 nodebug.####"} # Pattern to match each of abcd_sh, ijk_sh, etc.. Continues to match iteratively.
}; END{close O} # END block runs only once during program execution just like BEGIN block and here we close the filehandle O.
' xtop_env.txt # Input file to be read

could you please provide me the complete code so that i could understand things in a better way.

thanks.

Ok, you had churned a perl code in post #1 and there's an alternate version in post #2. See if you can conglomerate them into what you want. Let us know your findings and road blocks.

i am trying to execute the following code

#! /usr/bin/perl -w
#

# use strict;
#use warnings;
#
#
	
	$filename= "xtop_env.txt";
	
	$my_outfile = "output.txt";
	
	open (HANDLE,"<$filename");
	
	 
	
	
	while(<HANDLE>)
	{	    
		"perl" -lne 'BEGIN{open O,">output.txt"};
               {
			       
                        if(/^xtopShared/../;$/)
			      {
                                      while (/(\w+?_sh)/g) 
	                             {            
                                      print O "##### buildapi $1 nodebug.####"};
                                       }
			   } 
			      
	               
                  END{close O}' xtop_env.txt
				  
				 
	}
	
	close HANDLE;

however following erros occur everytime.

String found where operator expected at e:\my_work_docs\perl scripts\fman.pl lin
e 40, near "END{close O}'"
  (Might be a runaway multi-line '' string starting on line 30)
        (Missing semicolon on previous line?)
Bareword found where operator expected at e:\my_work_docs\perl scripts\fman.pl l
ine 40, near "END{close O}' xtop_env"
Unquoted string "txt" may clash with future reserved word at e:\my_work_docs\per
l scripts\fman.pl line 43.
syntax error at e:\my_work_docs\perl scripts\fman.pl line 40, near "END{close O}
'"
Execution of e:\my_work_docs\perl scripts\fman.pl aborted due to compilation err
ors.

please suggest

#! /usr/bin/perl -w
use strict;

my $filename = "xtop_env.txt";
my $my_outfile = "output.txt";

open (HANDLE, "< $filename");
open (OUTPUT, "> $my_outfile");

while (<HANDLE>)
{
    if(/^xtopShared/../;$/)
    {
        while (/(\w+?_sh)/g)
        {
            print OUTPUT "##### buildapi $1 nodebug.####"};
        }
    }
}

close OUTPUT;
close HANDLE;

thanks a Ton!!!!!!!!

it worked.

---------- Post updated 03-06-13 at 01:08 PM ---------- Previous update was 03-05-13 at 03:46 PM ----------

hi..

I have tried to take the input through commmand line.
And want to check if the input file is valid, script should perform its action.

other wise a message "please enter a valid file name should appear".

please suggest..

Search google for "perl command line arguments".

---------- Post updated at 03:10 PM ---------- Previous update was at 02:35 PM ----------

trying to execute the following code.

compilation hangs...

$Input_filename=$ARGV[0];
	
if ($Input_filename ne "$ABCD/dataval/xtop_env.txt")
{
    {print "Usage: $ABCD/dataval/xtop_env.txt\n"};
    exit;
}

Do you get any compilation errors? If so, please post them as well.

No, I dont get any compilation error as such. however the compilation gets stuck.

I have come across a new issue where i need to check whether the file, which has been entered by the user phyically exists or not..

please suggest how to go about it..

  1. I am not able to understand when you say "compilation gets stuck".
  2. To check if a file exists or not, search and learn "perl file tests".
  3. And, I would suggest you to start reading a good book on perl. My personal favourite is Learning Perl.

hi balajesuri,
Actually the complier had some issues that's it way getting stuck while compilation.

The script is running fine now.There is a minor issue but it will be resolved soon.

Thanks for your assistance..

:slight_smile: :slight_smile:

thanks.