The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
searching and storing unknown number of lines based on the string with a condition swamymns Shell Programming and Scripting 7 05-12-2008 10:02 PM
extract same word from two files tjmannonline AIX 4 04-06-2008 09:42 AM
extract numbers from a word systemali Shell Programming and Scripting 6 03-20-2006 06:09 AM
extract last word on line to new file michieka UNIX for Dummies Questions & Answers 10 05-07-2002 08:26 AM
Selecting unknown string. Cameron UNIX for Dummies Questions & Answers 2 12-18-2001 11:48 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-22-2008
Registered User
 

Join Date: Mar 2008
Posts: 11
Stumble this Post!
perl newbie: how to extract an unknown word from a string

hi,
im quite new to perl regexp. i have a problem where i want to extract a word from a given string. but the word is unknown, only fact is that it appears as the second word in the string.

Eg.
input string(s) :
char var1 = 'A';
int var2 = 10;
char *ptr;

and what i want to do is to get the variable name (var1, var2, ptr ..etc.) from an above like string.
can we use something like (w+) to match a whole word.

thankx in advance.
wolwy.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-22-2008
Moderator
 

Join Date: Dec 2003
Location: /dev/florida
Posts: 946
Stumble this Post!
An easier way might be to use the split function. The following example shows you how to use the split function to extract words from a string.

Code:
#!/usr/local/bin/perl -w

my $str = "The quick brown box";

my @words = split(' ', $str);

foreach my $word (@words) {
    print "$word\n";
}

print "All words: @words\n";
print "Second word: @words[1]\n";

exit 0;
Reply With Quote
  #3 (permalink)  
Old 03-22-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,812
Stumble this Post!
small typo in the above
Code:
print "Second word: @words[1]\n";
# should be
print "Second word: $words[1]\n";
Reply With Quote
  #4 (permalink)  
Old 03-23-2008
Registered User
 

Join Date: Mar 2008
Posts: 11
Stumble this Post!
thankx very much guys,
it worked, the split function was quite useful.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:17 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0