String Manipulation Question....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String Manipulation Question....
# 1  
Old 10-03-2005
How To Break Up a String into Components in Shell Scripting

Say I've got a string like: data1,data2,data3,data4.

How would I be able to break up the string, so that I have four variables w/ the values data1 data2 data3 data4.

Also, how could I read a string character by character.

I know you can read a sentence word by word by using the
for var in $othervar, but how would you read a string character by character.

Thanks in Advance,
AJ Allmendinger

Last edited by TheRocket; 10-03-2005 at 12:48 PM..
# 2  
Old 10-03-2005
String Manipulation Question....

Say I've got a string like: data1,data2,data3,data4.

How would I be able to break up the string, so that I have four variables w/ the values data1 data2 data3 data4.

Also, how could I read a string character by character.

I know you can read a sentence word by word by using the
for var in $othervar, but how would you read a string character by character.

Thanks in Advance,
AJ Allmendinger
# 3  
Old 10-03-2005
There's surely a more elegant way to do this, but if you have only 4 fields
you can do it with cut:

var1=`cut -d',' -f1 $string`
var2=`cut -d',' -f2 $string`

and so on (the cut command cuts a string into fields delimited by the character
you provide with the -d option).
(the symbols ` surrounding the cut commands are backquotes, they don't
show properly on my browser)
# 4  
Old 10-03-2005
You can set IFS=,
However setting IFS can cause problems with other programs.

Another way:
Code:
while read a b c d 
do
    print $a $b $c $d
done <`awk ' BEGIN { FS=, }
        { for(i=1;i<NF;i++) {printf " %s", i } printf "\n" }
      '  datafilename `

# 5  
Old 10-03-2005
In ksh, use:
Code:
#!/usr/bin/ksh
IFS=', <tab>'             #in place of <tab> insert actual tab char
string=data1,data2,data3,data4
echo $string | read d1 d2 d3 d4
echo $d1 $d2 $d3 $d4

# 6  
Old 10-03-2005
This is a duplicate post and violates rule 4:
Quote:
(4) Do not 'bump up' questions if they are not answered promptly. No duplicate or cross-posting and do not report a post where your goal is to get an answer more quickly.
Here is the link for the other thread.
# 7  
Old 10-03-2005
I have merged the threads.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

String manipulation.

If a have a variable with a first and last name. and say the variable looks like this... FIRST LAST how could process the variable to look like First .L bash 3.2 (osx) (3 Replies)
Discussion started by: briandanielz
3 Replies

2. Shell Programming and Scripting

Deleting part of a string : string manipulation

i have something like this... echo "teCertificateId" | awk -F'Id' '{ print $1 }' | awk -F'te' '{ print $2 }' Certifica the awk should remove 'te' only if it is present at the start of the string.. anywhere else it should ignore it. expected output is Certificate (7 Replies)
Discussion started by: vivek d r
7 Replies

3. Shell Programming and Scripting

String manipulation

Hello Could you help with small script: How to split string X1 into 3 string String X1 can have 1 or many strings X1='A1:B1:C1:D1:A2:B2:C2:D2:A3:B3:C3:D3' This is output which I want to have: Z1='A1:B1:C1:D1' Z2='A2:B2:C2:D2' Z3='A3:B3:C3:D3' (5 Replies)
Discussion started by: vikus
5 Replies

4. Shell Programming and Scripting

Help With String Manipulation

Hi Guru's, I need some help with data manipulation using shell scripting. I know how to replace the whole string but not part of the string. The value after aa= should be replaced with the value in the mail leaving ,OU=111,OU=222,DC=333 as is. Below are the inputs and expected outputs. Input:... (17 Replies)
Discussion started by: Samingla
17 Replies

5. Shell Programming and Scripting

String Manipulation

How u convert string "hi pravin how are you?" to "Hi Pravin How Are You?" (4 Replies)
Discussion started by: proactiveaditya
4 Replies

6. Shell Programming and Scripting

I need help with string manipulation

First of all I am VERY new to this so bare with me and try and explain everything even if it seems simple. Basically I want to read a line of text from a html file. See if the line of text has a certain string in it. copy an unknown number of characters (the last 4 characters wiil be ".jpg" the... (1 Reply)
Discussion started by: c3lica
1 Replies

7. Shell Programming and Scripting

String manipulation

Hi, i am just gettin exposed to UNIX. Could anyone of u help me out with dis problem..? i have a variable 'act' which has the value as follows, echo $act gives -0- -0- -----0---- 2008-06-04 -0- -0- echo "$act" | awk '{print ($act)}' gives, -0- -0- -----0---- 2008-06-04 -0- -0- I... (2 Replies)
Discussion started by: jerrynimrod
2 Replies

8. Shell Programming and Scripting

String Manipulation Help

Hey Guys, Right i know how to alter a word to begin with a capital letter, i know how to remove unwanted characters and replace them with the relevant character however i don't now if there is a way to do them all in one line. Code: echo -n ${string:0:1} | tr a-z A-Z #convert first letter... (4 Replies)
Discussion started by: shadow0001
4 Replies

9. Shell Programming and Scripting

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! (13 Replies)
Discussion started by: hai1973
13 Replies

10. Programming

string manipulation in C

Hi all, i have the following string as input : "<iframe src="http://abcdef.com/asd/aaa/awerftya0480000008ave/direct;wi.120;hi.600/01?page=" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" topmargin="0" leftmargin="0" allowtransparency="true" width="120" height="600"> <script... (1 Reply)
Discussion started by: trinath
1 Replies
Login or Register to Ask a Question