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
How to: Parse text string into variables using Korn shell shew01 Shell Programming and Scripting 7 05-23-2008 07:01 AM
Parse String Using Sed racbern Shell Programming and Scripting 4 04-23-2008 09:14 AM
how to parse this string hcliff Shell Programming and Scripting 13 04-02-2008 01:43 AM
How can I parse a record found in /etc/passwd into variables? shew01 Shell Programming and Scripting 7 12-17-2007 08:23 AM
How to parse config variables from external file to shell script pradsh Shell Programming and Scripting 2 07-09-2007 11:21 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-21-2005
Registered User
 

Join Date: Feb 2005
Posts: 4
Stumble this Post!
How to parse a string into variables

I'm working in korn shell and have a variable which contains a string like:
aa_yyyymmdd_bbb_ccc_ddd.abc. I want to treat the _ and . as delimiters and parse the string so I end up with 6 values in variables that I can manipulate. My original plan was to use
var1=`echo $sting1 | cut -c1-c2` but now the requirements have changed so the position is no longer fixed.

Please help.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-21-2005
bhargav's Avatar
Registered User
 

Join Date: Sep 2004
Location: USA
Posts: 511
Stumble this Post!
Code:
$ echo "aa_yyyymmdd_bbb_ccc_ddd.abc"
aa_yyyymmdd_bbb_ccc_ddd.abc
$ echo "aa_yyyymmdd_bbb_ccc_ddd.abc"
aa_yyyymmdd_bbb_ccc_ddd.abc
$ echo "aa_yyyymmdd_bbb_ccc_ddd.abc" | cut -d"." -f 2
abc
$ echo "aa_yyyymmdd_bbb_ccc_ddd.abc" | cut -d"_" -f 1
aa
$ echo "aa_yyyymmdd_bbb_ccc_ddd.abc" | cut -d"_" -f 2
yyyymmdd
$ echo "aa_yyyymmdd_bbb_ccc_ddd.abc" | cut -d"_" -f 3
bbb
$ echo "aa_yyyymmdd_bbb_ccc_ddd.abc" | cut -d"_" -f 4
ccc
$ echo "aa_yyyymmdd_bbb_ccc_ddd.abc" | cut -d"_" -f 5
ddd.abc
$ echo "aa_yyyymmdd_bbb_ccc_ddd.abc" | cut -d"_" -f 5 | cut -d"." -f 1
ddd
Reply With Quote
  #3 (permalink)  
Old 02-21-2005
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,242
Stumble this Post!
$ oldIFS=$IFS
$ IFS=._
$ set aa_yyyymmdd_bbb_ccc_ddd.abc
$ set $1
$ echo $1
aa
$ echo $2
yyyymmdd
$ echo $3
bbb
$ IFS=oldIFS
Reply With Quote
  #4 (permalink)  
Old 02-22-2005
Registered User
 

Join Date: Feb 2005
Posts: 4
Stumble this Post!
Thanks for the help!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:44 PM.


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