Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

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

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-05-2013
Rashid Khan Rashid Khan is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 27 May 2013, 5:29 AM EDT
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
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:


Code:
##### buildappi    abcd_sh    nodebug.#####
##### buildappi    ijk_sh       nodebug.#####

The given string is as follows:


Code:
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.


Code:
#! /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;


Last edited by Scrutinizer; 03-05-2013 at 02:19 AM.. Reason: code tags
Sponsored Links
    #2  
Old Unix and Linux 03-05-2013
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 3 August 2015, 7:06 AM EDT
Location: India
Posts: 1,889
Thanks: 27
Thanked 539 Times in 516 Posts

Code:
perl -lne 'BEGIN{open O,">output.txt"};
if(/^xtopShared/../;$/) {
    while (/(\w+?_sh)/g) {print O "##### buildapi $1 nodebug.####"}
}; END{close O}' xtop_env.txt

Sponsored Links
    #3  
Old Unix and Linux 03-05-2013
Rashid Khan Rashid Khan is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 27 May 2013, 5:29 AM EDT
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
thanks a lot balajesuri for the reply.

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

Thanks.
    #4  
Old Unix and Linux 03-05-2013
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 3 August 2015, 7:06 AM EDT
Location: India
Posts: 1,889
Thanks: 27
Thanked 539 Times in 516 Posts

Code:
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

Sponsored Links
    #5  
Old Unix and Linux 03-05-2013
Rashid Khan Rashid Khan is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 27 May 2013, 5:29 AM EDT
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
could you please provide me the complete code so that i could understand things in a better way.

thanks.
Sponsored Links
    #6  
Old Unix and Linux 03-05-2013
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 3 August 2015, 7:06 AM EDT
Location: India
Posts: 1,889
Thanks: 27
Thanked 539 Times in 516 Posts
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.
Sponsored Links
    #7  
Old Unix and Linux 03-05-2013
Rashid Khan Rashid Khan is offline
Registered User
 
Join Date: Mar 2013
Last Activity: 27 May 2013, 5:29 AM EDT
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
i am trying to execute the following code


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.


Code:
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

Last edited by Scrutinizer; 03-06-2013 at 05:26 AM.. Reason: Please use code tags for data and code samples ; update: added extra code tags
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Oneliner ---split string to character by piping shell output to perl yifangt Shell Programming and Scripting 6 01-07-2013 10:46 AM
Match list of strings in File A and compare with File B, C and write to a output file in CSV format asnandhakumar Shell Programming and Scripting 6 10-11-2012 03:47 PM
grep exact string from files and write to filename when string present in file JC_1 Shell Programming and Scripting 4 08-06-2012 10:52 PM
PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL lowmaster Shell Programming and Scripting 0 02-07-2010 02:59 PM
split a file at a specified string jpl35 Shell Programming and Scripting 6 07-04-2002 11:41 AM



All times are GMT -4. The time now is 01:11 PM.