de concatenating a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting de concatenating a string
# 1  
Old 04-28-2011
de concatenating a string

I have a variable
var=string1:string2:string3

I want to get the string de-concatenated and put it as
var1=string1
var2=string2
var3=string3

Thanks in advance.

---------- Post updated at 02:18 PM ---------- Previous update was at 01:45 PM ----------

I got the solution as below:
var1=$( echo $var | cut -d":" -f1 )
var2=$( echo $var | cut -d":" -f2 )
var2=$( echo $var | cut -d":" -f3 )
# 2  
Old 04-28-2011
ksh and later have ${name#pattern} with #, ##, % %% that cut strings by globbing pattern, built in, and newer ksh have numerically driven substring:
Code:
$ a=1234567890 ; echo ${a#????}
567890
$ dtksh -c 'a=1234567890 ; echo ${a:3:4}'
4567
$

This User Gave Thanks to DGPickett For This Post:
# 3  
Old 04-28-2011
Code:
echo $var | sed 's/:/ /g' | read var1 var2 var3

or
Code:
var1=${var%%:*}
var3=${var##*:}
t=${var#*:}
var2=${t%:$var3}

or
Code:
set -- $(echo $var | sed 's/:/ /g')
var1=$1
var2=$2
var3=$3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenating(??) shell variable with string

Dear Users, I am writing a for loop in shell (BASH), and am struggling with a very specific detail: SU_DATA=/home/........./su/ for(blah blah blah) { a=1001 b=1002 suop2 $SU_DATA/$a_clean.su $SU_DATA/$b_clean.su op=sum > W1.su } Here, it looks like the variables are... (1 Reply)
Discussion started by: martm
1 Replies

2. UNIX for Dummies Questions & Answers

Concatenating

Hi, I have file called "3rdparty.dat" I want to concatenate current YYYYMMDD to it. Snd result should be like 3rdParty20111110.dat. How can i do this? Thanks in advance. (3 Replies)
Discussion started by: raj.shah.0609
3 Replies

3. Shell Programming and Scripting

need help in concatenating

Hi All , i`m writing a script , i stucked in middle . Script echo "Please Enter the INSTANCE name" read iName echo "The INSTANCE name is $iName" more /opt/IBMIHS*/conf/httpd.conf_"$iName" script end here i`m getting error as : Error /opt/IBMIHS*/conf/httpd.conf_w101:... (7 Replies)
Discussion started by: radha254
7 Replies

4. Shell Programming and Scripting

Concatenating File and String for Sendmail

I want o add a variable in addition to a file which will be send with sendmail. I have problems to find the correct syntax for concatenating this variable called $MyVariable. sendmail mai@domain.com </tmp/errormessage.txt $MyVariable] Thanks for your help! (2 Replies)
Discussion started by: high5
2 Replies

5. Shell Programming and Scripting

Concatenating and appending string based on specific pattern match

Input #GEO-1-type-1-fwd-Initial 890 1519 OPKHIJEFVTEFVHIJEFVOPKHIJTOPKEFVHIJTEFVOPKOPKHIJHIJHIJTTOPKHIJHIJEFVEFVOPKHIJOPKHIJOPKEFVEFVOPKHIJHIJEFVHIJHIJEFVTHIJOPKOPKTEFVEFVEFVOPKHIJOPKOPKHIJTTEFVEFVTEFV #GEO-1-type-2-fwd-Terminal 1572 2030... (7 Replies)
Discussion started by: patrick87
7 Replies

6. Shell Programming and Scripting

Concatenating string with numbers

Hi, I want to display the string value with number value. I dont know how to display. Can anyone help me. This is my code export A=${file_name} echo $a $b $sum | awk '{ printf "%011.f,%014.f,%014.f\n", $1,$2,$3}' >> ${MRR_OUTPUT} the out put shold be ${A}, $a, $b filename,... (2 Replies)
Discussion started by: easterraj
2 Replies

7. Programming

Concatenating array of strings into one string separated by spaces

Hi, Well as the title says, I have an array of strings (delimited by null). The length of the array is variable and length of each string is variable as well. What I need is one huge string with the original strings in the array separated by spaces. For example is an array is such that array... (12 Replies)
Discussion started by: newhere
12 Replies

8. Shell Programming and Scripting

Concatenating two files

HI I need to concatenate two files which are having headers. the result file should contain only the header from first file only and the header in second file have to be skipped. file1: name age sriram 23 file2 name age prabu 25 result file should be name age sriram 23 prabu ... (6 Replies)
Discussion started by: Sriramprabu
6 Replies

9. UNIX for Dummies Questions & Answers

concatenating string and variable

how to concatenate a string and variable like a=rahul and i want to put it into another variable 'b' as "rahul_prasath" i dont want to use another variable for "_prasath" how to do it? (1 Reply)
Discussion started by: rolex.mp
1 Replies

10. Shell Programming and Scripting

concatenating static string to records in data file

I just need to add a static ID to each output record so the users will be able to tell which group records in combined flatfiles come from I have the static ID in a bourne variable. I tried awk '{print "${GroupID}" $0}' infile > outfile But I ended up with the string ${GroupID} instead of... (5 Replies)
Discussion started by: gillbates
5 Replies
Login or Register to Ask a Question