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
extract numbers from a word systemali Shell Programming and Scripting 14 03-30-2009 03:47 AM
searching and storing unknown number of lines based on the string with a condition swamymns Shell Programming and Scripting 7 05-13-2008 01:02 AM
extract same word from two files tjmannonline AIX 4 04-06-2008 12:42 PM
extract last word on line to new file michieka UNIX for Dummies Questions & Answers 10 05-07-2002 11:26 AM
Selecting unknown string. Cameron UNIX for Dummies Questions & Answers 2 12-18-2001 03:48 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-22-2008
wolwy_pete wolwy_pete is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 13
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.
  #2 (permalink)  
Old 03-22-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,912
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;
  #3 (permalink)  
Old 03-22-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
small typo in the above
Code:
print "Second word: @words[1]\n";
# should be
print "Second word: $words[1]\n";
  #4 (permalink)  
Old 03-23-2008
wolwy_pete wolwy_pete is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 13
thankx very much guys,
it worked, the split function was quite useful.
Sponsored Links
Closed Thread

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 05:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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