Separate variable value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Separate variable value
# 1  
Old 11-26-2015
Separate variable value

Friends, I have the following problem

Code:
var=30|500;

I need to keep those values in the variable other unique pattern that I have is |
so should be

Code:
var2=30;
var3=500;

I tried to use my split does not work
Example failed

Code:
var2=split("|",$var);
var3=split("|",$var);

# 2  
Old 11-26-2015
Hello tricampeon81,

Could you please try following and let me know if this helps. Though not sure about your complete requirement but as per your question this may help you.
Code:
var2=`echo $var | cut -d"|" -f1`
var3=`echo $var | cut -d"|" -f2`

Output will be as follows.
Code:
echo $var2
30 
echo $var3
500

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 11-26-2015
Quote:
Originally Posted by RavinderSingh13
Hello tricampeon81,

Could you please try following and let me know if this helps. Though not sure about your complete requirement but as per your question this may help you.
Code:
var2=`echo $var | cut -d"|" -f1`
var3=`echo $var | cut -d"|" -f2`

Output will be as follows.
Code:
echo $var2
30 
echo $var3
500

Thanks,
R. Singh


Perfect that's what nesecitaba once again thank you very much !!!!
# 4  
Old 11-26-2015
Please don't mix threads and be careful not to waste resources by applying suboptimal solutions. Do it right the first time.

For your (sub-) problem above, use shell's parameter expansion (pre/suffix substitution):
Code:
var="30|500"
echo ${var#*|}
500
echo ${var%|*}
30

# 5  
Old 11-26-2015
Quote:
Originally Posted by RudiC
Please don't mix threads and be careful not to waste resources by applying suboptimal solutions. Do it right the first time.

For your (sub-) problem above, use shell's parameter expansion (pre/suffix substitution):
Code:
var="30|500"
echo ${var#*|}
500
echo ${var%|*}
30



I am so sorry

and they gave me the solution of all my theme thank you very much to all, sorry if some inconveninte genre
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tar with variable date in separate shell

Hi, I am trying to Zip a folder inside a shell script like below. It successfully taring but it only returns Null inside the variables. Searched a lot but no clue to finding the root cause. testno=1; date=20171128; DIR=/root/ sh -c 'cd $DIR; tar cvf "AWS.${testno.${date}.tar" "./AWS"' ... (5 Replies)
Discussion started by: pradyumnajpn10
5 Replies

2. Shell Programming and Scripting

Separate a hash variable into 2 parts in Perl

Dear Perl users/experts, Could somebody help me how to solve my problem, I have a hash variable that I want to convert into dot file (graphviz). I know how to convert it to dot file but I need some modification on the output of the hash variable before convert it to dot file. Eeach key of... (1 Reply)
Discussion started by: askari
1 Replies

3. Shell Programming and Scripting

Separate fields

awk 'NF==2{s=$1;next}{$(NF+1)=s}1' sort.txt > output.txt A_16_P32713632 chr10 90695750 90695810 ACTA2 A_16_P32713635 chr10 90696573 90696633 ACTA2 A_16_P32713680 chr10 90697419 90697479 ACTA2 The command above outputs the data as a string separated by a space in 1 field. I can not... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

How to separate a statement based on some delimiter and store each field in a variable?

Hi, Variable1 = MKT1,MKT2,MKT3,MKT4 Now i want to store each of these value seperated by comma to a array and access each of the values. Also find out number of such values seperated by comma. Variable1 can have any number of values seperated by comma. Thanks :) (3 Replies)
Discussion started by: arghadeep adity
3 Replies

5. UNIX for Dummies Questions & Answers

Want to separate one column

I have one command which provide following output related to file system and disk space utilization Filesystem kbytes used avail %used Mounted on /dev/lvol3 131072 73407 54088 58% / /abc/lvol1 59797 30314 2350300 ... (8 Replies)
Discussion started by: Nakul_sh
8 Replies

6. Shell Programming and Scripting

Awk variable in a separate file

Hi all, I have a requirement to put all the varibles used in an awk command in a separate file. This is because i have arround 100 variables used in an awk command. So i want to put all the variables used for the awk command in a separate file. Please help me on this. Thanks in adv. (6 Replies)
Discussion started by: gani_85
6 Replies

7. Shell Programming and Scripting

Trying to take a string and break each letter into a separate variable

I am trying to make a script that takes a word and each letter up and turns it into a separate variable. My code currently does not work but I feel I just need to tweak one thing that I am unsure of. (ex: if forum was typed in letter1=f; letter2=o; letter3=r;...) Thank you count=1; ... (7 Replies)
Discussion started by: crimputt
7 Replies

8. Shell Programming and Scripting

Separate fields

Hi everyone! I have a field like that: I need to keep I don't know how to use the Capital character like a separator and how to keep only this one... I guess sed could do something like that... Thanks;) (3 Replies)
Discussion started by: Castelior
3 Replies

9. Shell Programming and Scripting

Cant separate variables

Hey guys, new problem....im not being able to seperate variables. the code runs like this... OPTIONS=$(awk '{print $2}' /etc/fstab} a=$(zenity --list --text "Mount points selection" --radiolist --column "choice" --column "mountpt" FALSE $OPTIONS); echo $a Note: the result i get is that... (2 Replies)
Discussion started by: dplate07
2 Replies

10. Shell Programming and Scripting

How to separate a variable

Hi, This is what happened after I came back from vacation: I don't remember how to do some simple things anymore. I have a variable being passed in Kshell script. I only know the variable is like string: "str1 str2 str3 ....", and they are separated with blank spaces. I need to separate each... (4 Replies)
Discussion started by: whatisthis
4 Replies
Login or Register to Ask a Question