CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to extract string info from a bigger string

 



vimo
New User

Apr 3, 2013, 4:36 PM

Post #1 of 6 (521 views)
How to extract string info from a bigger string Can't Post

hi all,

this is the info
ce_pr_switch ABCD_EFG_IJK \
-domain ABC_CX \
-input_supply_port { xaz xx_xyxyx_1 } \
-output_supply_port { xbz asdfadfa_123_afdf } \
-control_port { xcz a_top/tp_t_n } \
-control_port { xdz a_top/tp_t_n } \
-on_state { xez d_e { (e_n && e_in) } } \
-off_state { xfz { !(e_in && en_in) } }

requirement is
1. whenever I encounter ce_pr_switch , I goto extract the info which is present in the right side of the -control_port i.e. only the hierarchy (a_top/tp_t_n) for both the control port..
2. This ce_pr_switch might be in many files, goto check in all files and extract this info alone.

Need help in extracting that info alone !! How to do, its urgent help out..


Rahul6990
Novice

Apr 3, 2013, 11:09 PM

Post #2 of 6 (513 views)
Re: [vimo] How to extract string info from a bigger string [In reply to] Can't Post

Please provide your effort and where are you stuck.


Laurent_R
Veteran / Moderator

Apr 3, 2013, 11:31 PM

Post #3 of 6 (509 views)
Re: [vimo] How to extract string info from a bigger string [In reply to] Can't Post

Just one very quick (and not so clean) way to do that:


Code
while (<>) { 
push @results, $1 if /-control_port \{ ([^}]+)};
}


You might need to escape the '-' at the beginning of the regex, (i.e. to have "/ \- control..."), not sure, I don't have documentation with me and cannot run a test right now (I am in public transportation on cell phone network).


Zhris
Enthusiast

Apr 4, 2013, 11:29 AM

Post #4 of 6 (492 views)
Re: [Laurent_R] How to extract string info from a bigger string [In reply to] Can't Post

Vimo,

Your requirements are quite broad / open to intepretation. More specific detail may be required to provide the right solution.

Just to correct / extend Laurent's rough example:


Code
while (<>) {  
push @results, $1 if ( ( /^ce_pr_switch/ ... /^[^-]/ ) && ( /^-control_port \{ \w{3} (.+?) \}/ ) );
}


It uses the range operator to process blocks between line that begins with ce_pr_switch TO line that does not begin with - ( may be better to check for lines without backslash on end i.e. /[^\\] (\r|\n)*$/ ). This was to suit your requirement "whenever I encounter ce_pr_switch" assuming that there are multiple blocks per file / will ensure that we do not process isolated -control_port lines.

With regards to "goto check in all files", I can only assume you refer to checking all files across your entire system. File::Find will likely be suitable.

Chris


(This post was edited by Zhris on Apr 4, 2013, 11:58 AM)


vimo
New User

Apr 4, 2013, 12:48 PM

Post #5 of 6 (477 views)
Re: [Laurent_R] How to extract string info from a bigger string [In reply to] Can't Post

Thanks a lot Laurent & Chris...

The requirement is like,

1. There will be a directory and inside that directory there will be N-number of files and folders.

2. The script basically should search recursively in all files and folders for ce_pr_switch word.

3. The example would be like, as given below:

===========================================
ce_pr_switch ABCD_EFG_IJK \
-domain ABC_CX \
-input_supply_port { xaz xx_xyxyx_1 } \
-output_supply_port { xbz asdfadfa_123_afdf } \
-control_port { xcz a_top/tp_t_n } \
-control_port { xdz a_top/tp_t_n } \
-on_state { xez d_e { (e_n && e_in) } } \
-off_state { xfz { !(e_in && en_in) } }
===========================================
* It starts with the word ce_pr_switch and the back slash in the the line basically means that its going to continue in the next line [Basically its a one single line and instead of typing it fully in a single line, its been appended to the next line via a back slash (\).] and at the end of the line there won't be a back slash, indicating that its the end of the line.

4. So, once when it encounters the word called ce_pr_switch, it should check in the next coming lines whether it has control_ports or not.

5. And if it finds any control_port then, extract the hierarchy which is present in the right side of the control_port. For example in the above code it is a_top/tp_t_n

6. It should basically look for the control_ports coming in the line along with ce_pr_switch and look for the hierarchy in the right side.

7. The script should not look in any of the stand alone control_port lines.

8. It basically should print in a output file, as example given below.

File Name Stch Name Control Port
xyz.txt ABCD_EFG_IJK a_top/tp_t_n

As I am totally new to perl, finding it diffucult. Please do the needful help.

Regards

In Reply To


(This post was edited by vimo on Apr 4, 2013, 12:49 PM)


BillKSmith
Veteran

Apr 4, 2013, 1:50 PM

Post #6 of 6 (469 views)
Re: [vimo] How to extract string info from a bigger string [In reply to] Can't Post

I think I understand your requirements. You want to search the specified directory (and all its subdirectories) for files which contain the desired data. Output selected fields from every matching record.

Input data records span multiple lines. A backslash as the last character indicates that the record is continued on the next line.

Use File::Find to find candidate files. Read logical records from those files. Report data from desired records.
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives