split a variable into two


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split a variable into two
# 8  
Old 11-11-2008
Quote:
Originally Posted by srilaxmi
I have a variable with the value "hello world". I want to split this variable into two, so that i can write "hello" into one variable and "world" into another.

Use shell parameter expansion; there is no need for any external command:

Code:
var="hello world"
left=${var%% *}
right=${var#* }

# 9  
Old 11-11-2008
Quote:
Originally Posted by dennis.jacob
Another one...

Code:
set `eval echo $var`
x=$1;
y=$2;


Why eval and echo?

Code:
set -f  ## inhibit filename expansion
set -- $var
set +f
left=$1
right=$2

# 10  
Old 11-12-2008
Bug

Quote:
Originally Posted by cfajohnson

Why eval and echo?

Code:
set -f  ## inhibit filename expansion
set -- $var
set +f
left=$1
right=$2

Yes ..It is unnecessary
# 11  
Old 11-12-2008
Another (bash):

var="hello world"
declare -a array
array=( $(echo ${var}) )
echo ${array[0]}
echo ${array[1]}
# 12  
Old 11-12-2008
Quote:
Originally Posted by juheimbu
Another (bash):
Code:
var="hello world"
declare -a array
array=( $(echo ${var}) )


You don't need echo or command substitution (or declare), but you should add set -f in case there are any wildcards in $var:

Code:
set -f
array=( $var )
set +f

Quote:
Code:
echo ${array[0]}
echo ${array[1]}


Or:

Code:
printf "%s\n" "${array[@]}"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Split and Rename Split Files

Hello, I need to split a file by number of records and rename each split file with actual filename pre-pended with 3 digit split number. What I have tried is the below command with 2 digit numeric value split -l 3 -d abc.txt F (# Will Produce split Files as F00 F01 F02) How to produce... (19 Replies)
Discussion started by: techedipro
19 Replies

2. Shell Programming and Scripting

awk to split one field and print the last two fields within the split part.

Hello; I have a file consists of 4 columns separated by tab. The problem is the third fields. Some of the them are very long but can be split by the vertical bar "|". Also some of them do not contain the string "UniProt", but I could ignore it at this moment, and sort the file afterwards. Here is... (5 Replies)
Discussion started by: yifangt
5 Replies

3. Shell Programming and Scripting

[Perl] Split lines into array - variable line items - variable no of lines.

Hi, I have the following lines that I would like to see in an array for easy comparisons and printing: Example 1: field1,field2,field3,field4,field5 value1,value2,value3,value4,value5Example 2: field1,field3,field4,field2,field5,field6,field7... (7 Replies)
Discussion started by: ejdv
7 Replies

4. Shell Programming and Scripting

How to split a data assigned to a variable

The requirement is, there is a log file which contains a huge data. i need to get a particular field out of it by searching with another field. ex: 2011-03-28 13:00:07,423 : millis=231 q={ call get_data_account(?,?,?,?,?) }, params= i need to search for the word "get_data_account" in file... (1 Reply)
Discussion started by: Jassz
1 Replies

5. Shell Programming and Scripting

Split variable length and variable format CSV file

Dear all, I have basic knowledge of Unix script and her I am trying to process variable length and variable format CSV file. The file length will depend on the numbers of Earnings/Deductions/Direct Deposits. And The format will depend on whether it is Earnings/Deductions or Direct Deposits... (2 Replies)
Discussion started by: chechun
2 Replies

6. Shell Programming and Scripting

Split one Variable into three

Hi i have a variable $VAR =15 14 13, i want to split this number and store in 3 variables say $N1=15 $N2=14 $N3=13 i know its simple but my mind is not giving me answers at this point of time. i will be running the code in Solaris Box..please help (3 Replies)
Discussion started by: Shellslave
3 Replies

7. Shell Programming and Scripting

Bash:How to split one string variable in two variables?

Hello, I have a paramter $param consisting just of two literals and want to split it into two parameters, so I can combine it to a new parameter <char1><string><char2>, but the following code didn't work: tmp_PARAM_1=cut -c1 $PARAM tmp_PARAM_2=cut -c2 $PARAM... (2 Replies)
Discussion started by: ABE2202
2 Replies

8. Shell Programming and Scripting

split variable values into array

i have these values inside variable $blah BUNGA TERATAI 3 5055 ITH 1 0 0 0 1 1 JADE TRADER 143W ITH 4 0 0 0 4 4 MOL SPLENDOR 0307A ITH 3 0 0 0 3 3 so how do I split them into array with the... (4 Replies)
Discussion started by: finalight
4 Replies

9. UNIX for Dummies Questions & Answers

Split a file with no pattern -- Split, Csplit, Awk

I have gone through all the threads in the forum and tested out different things. I am trying to split a 3GB file into multiple files. Some files are even larger than this. For example: split -l 3000000 filename.txt This is very slow and it splits the file with 3 million records in each... (10 Replies)
Discussion started by: madhunk
10 Replies

10. Shell Programming and Scripting

awk and split and variable used prior

I am trying to use awk and its split function to get the number of tokens in a string that are seperated by underscores. ex_alex_is_testing_this_script_ex would return 7. It works when I directly put the string in. However, I can not get it to work when I try to refer to a variable used earlier... (2 Replies)
Discussion started by: rx82000
2 Replies
Login or Register to Ask a Question