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 > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Extract string between <> rakeshou Shell Programming and Scripting 14 11-21-2007 01:10 PM
Extract numbers from a string and store in variables davewg Shell Programming and Scripting 6 11-14-2007 05:22 AM
Extract String sehgalniraj UNIX for Dummies Questions & Answers 1 09-25-2006 12:35 PM
Extract String bestbuyernc Shell Programming and Scripting 5 11-14-2005 03:42 PM
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

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-02-2006
Sniper Pixie Sniper Pixie is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 25
extract from string variable into new variables

I have a variable which consists of a string like this:

001 aaabc 44 a bbb12

How do I extract each substring, delimited by the spaces, into new variables - one for each substring?

eg var1 will be 001, var2 will be aaabc, var3 will be 44, var4 will be a, etc?

I've come up with this:
var1=$(echo $string | awk 'NF=1') ; var2=$(echo $string | awk 'NF=2') ; var3=$(echo $string | awk 'NF=3')

but var2 = NF1 and 2, var3 = NF 1,2,3 etc
It also seems a bit long winded and impractical for operating on a lengthy string.

Last edited by Sniper Pixie; 03-02-2006 at 02:58 PM..
  #2 (permalink)  
Old 03-02-2006
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
See previous answer.
At the bash prompt...
Code:
$ var="001 aaabc 44 a bbb12"
$ set $var
$ echo $1
001
  #3 (permalink)  
Old 03-03-2006
Sniper Pixie Sniper Pixie is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 25
Thanks for your help Ygor. That's neater than the alternative I came up with.
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 08:33 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