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
sed string manipulation speedieB Shell Programming and Scripting 4 11-23-2008 08:27 PM
string manipulation james6 UNIX for Dummies Questions & Answers 5 06-03-2008 10:05 AM
String Manipulation Help shadow0001 Shell Programming and Scripting 4 03-09-2008 04:35 PM
string manipulation Cactus Jack Shell Programming and Scripting 9 02-14-2008 01:14 PM
awk string manipulation zoo591 Shell Programming and Scripting 2 08-09-2006 12:13 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 10-29-2006
hai1973 hai1973 is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 3
string manipulation

Hello,

I have a korn shell string variable

str1 = "A,B,Z"

I would like to create another korn shell string variable

str2 = "letter = 'A' or letter = 'B' or letter = 'Z' "

Please help!

Thanks in advance

an UNIX newbie!
  #2 (permalink)  
Old 10-29-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
Are you looking for something like this:
Code:
str1="A,B,Z"
str2=`echo $str1 | cut -d, -f1`
Use -f2 for letter B and -f3 for letter Z.
  #3 (permalink)  
Old 10-29-2006
hai1973 hai1973 is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 3
Thanks for quick response!

The problem is that str1 may contains any number of letters separated by a comma

I think I need some sort of loop!
  #4 (permalink)  
Old 10-29-2006
ahmedwaseem2000 ahmedwaseem2000 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Bangalore
Posts: 219
can you please be more precise?? like which value do yo have to pick. is it the first one the second one or the last one?
  #5 (permalink)  
Old 10-29-2006
tayyabq8's Avatar
tayyabq8 tayyabq8 is offline Forum Advisor  
Moderator
  
 

Join Date: Nov 2004
Location: Bahrain
Posts: 578
One way to do that:
Code:
str1="A,B,C,D,E,F,G,H,I"
count=`echo $str1 | awk -F, '{print NF}'`
i=2
while ((i <= $count));
do
str${i}=`echo $str1 | cut -d, -f${i}`
(( i = i + 1));
done
Another way, using awk arrays:
Code:
str1="A,B,C,D,E,F,G,H,I,J"
echo $str1 | awk -F, '{
for (i =1; i <= NF; i++)
         a[i]=$i;
}'
You can reference array subscript later on to access values.

Regards,
Tayyab
  #6 (permalink)  
Old 10-29-2006
hai1973 hai1973 is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 3
Thanks Tayyab!

I tried your second suggestion but one more stupid question: array a is in awk, how do I reference to it in Shell.

Regards
  #7 (permalink)  
Old 10-29-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
eval `echo $str1 | awk -F, '{
for (i =1; i <= NF; i++)
        printf("a[%d]=%s\n",i,$i);
}'`
Use the above code to access array "a" in shell script.
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 08:43 PM.


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