The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search for string in a file and extract another string to a variable daikeyang Shell Programming and Scripting 6 03-20-2009 08:45 PM
extract a string from main string madhu.it Shell Programming and Scripting 1 10-06-2008 04:05 AM
extract a sub string from a main string madhu.it Shell Programming and Scripting 5 10-04-2008 02:22 AM
perl newbie: how to extract an unknown word from a string wolwy_pete Shell Programming and Scripting 3 03-23-2008 10:41 AM
How to extract a portion of a string from the whole string ds_sastry UNIX for Dummies Questions & Answers 2 09-29-2001 10:40 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-01-2009
xlynx3 xlynx3 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 3
Perl Extract String

Hi,
I have a string like

"something is good wanted (bla bla)"

I need to get the world "wanted" from this string and "assign it to a variable".. but it's not a static word so i want to get that word by searching the pattern as follows

<space>desiredword<space>(

and i tried to get that word by
Code:
$string =~ m/\s.*?\s\(/gi;
How can i do this?

Many thanks in advance..

Last edited by Yogesh Sawant; 07-05-2009 at 10:19 AM.. Reason: added code tags
  #2 (permalink)  
Old 07-01-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,382
only two space before and after desired word is not enough if its static..
  #3 (permalink)  
Old 07-01-2009
xlynx3 xlynx3 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 3
actually the pattern is constant but word length can be changed

space - word - space - (

is a fix pattern..
  #4 (permalink)  
Old 07-01-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,382
hmmm this sed pattern may help
Code:
sed 's/\(.*\) \(.*\) (\(.*\)/\2/g' filename
  #5 (permalink)  
Old 07-01-2009
xlynx3 xlynx3 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 3
thanks for your solution. I'll try it ASAP..
  #6 (permalink)  
Old 07-01-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
perl:

Code:
$string = 'something is good wanted (bla bla)';
my ($match) = $string =~ /\s(\w+)\s\(/;
print $match;
  #7 (permalink)  
Old 07-02-2009
zilux zilux is offline
Registered User
  
 

Join Date: Apr 2009
Location: netherland
Posts: 2
try this

Code:
my $s = "something is good wanted (bla bla)";
if ( $s =~ /^.*\s(.*)\s\(/ ) {
    print "$1";
}

Last edited by Yogesh Sawant; 07-05-2009 at 10:18 AM.. Reason: added code tags
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 08:39 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0