moving variables to another variable as a new avriable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting moving variables to another variable as a new avriable
# 1  
Old 06-12-2009
moving variables to another variable as a new avriable

Somehow I can't get it for this basic bash problem. maybe someone can help.

What I try to do is:

a="world"
b="hello"

how can I move this into $c so that I can replace "helloworld" with "world hello" in sed like:

sed "s/\helloworld/ ${c}...

I tried several combinations but all with a wrong result:

c="${a};${b}"

echo $c;
hellod
# 2  
Old 06-12-2009
You have not written what exactly you want in c. May be this works for you...
Code:
#!/bin/bash
a="world"
b="hello"
c=$a" "$b
echo $c

# 3  
Old 06-12-2009
Thanx for the fast reply, but this does not work for me. I get the same result like in my example.

but I think I know why.
a="world"
b="hello"

a and b are lines randomly taken from a file, I think the problems come with the blancs in it:

a=$(perl -e 'srand; rand($.) < 1 && ($line = $_) while <>; print $line;' /text/part1.TXT)

if a line taken from part1.TXT is:
whatever line
I see this in the shell. I think a ' is missing somehow...

++ perl -e 'srand; rand($.) < 1 && ($line = $_) while <>; print $line;' /text/part1.TXT
' a='whatever line
+ ^
there is no ' at the end
# 4  
Old 06-12-2009
try this

c=`echo "$a $b"`
echo $c
# 5  
Old 06-12-2009
Quote:
Originally Posted by amitranjansahu
try this

c=`echo "$a $b"`
echo $c
This works well if I set the variables like:

a="world"
b="hello"

but if I make this:
a=$(perl -e 'srand; rand($.) < 1 && ($line = $_) while <>; print $line;' /text/part1.TXT)

all things are messed up...

can it be that this randomly reading lines thing also carry linefeed or carriage returns with it, what I can't see but messes all tings up???

-----Post Update-----

Quote:
try this

c=`echo "$a $b"`
echo $c
&
try this

c=`echo "$a $b"`
echo $c
booth solutions work perfect. thank you guys!

Ofcourse I should take care that the file format should be correct, if I play with text files in unix.

My files were in dos format, so everything I tried did not work... I converted to unix and all is allright now... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving files to a directory with a variable name

I have this shell script which i am going to package into an application: cd /Users/william/Desktop Cleaning; mkdir "date"; cp ~/Desktop/* ~/"date"; how would i point to the directory with the name unknown to cp? (2 Replies)
Discussion started by: ws1
2 Replies

2. Shell Programming and Scripting

Moving cat values to a variable

I have a file created as ABC!DEF@2014.txt My if condition is based on 2014 so I need to move it to variable. So while I can do this on console screen - ls ABC* -l > test.txt cat test.txt | cut -f 2 -d "@" | cut -f 1 -d "." to get the value - 2014 I am a bit at loss how to achieve this... (4 Replies)
Discussion started by: rajiv_kool
4 Replies

3. UNIX for Dummies Questions & Answers

Moving files to a folder with a variable name

hello there- first post here- maybe someone can help- Basically I am trying to copy the contents of a folder to a different folder that has a variable name. the content I want to copy would be in a folder on my desktop called: myfolder the variable folder would look something like: ... (3 Replies)
Discussion started by: infothelonghaul
3 Replies

4. Shell Programming and Scripting

Variable variables

Hello, Can you please help here? DAY=$1 MONTH_MONDAY_YEAR = 1 2 3 4 for i in ${MONTH_${DAY}_YEAR} do echo ${i} done ./test.sh MONDAY ./test.sh: line 3: MONTH_MONDAY_YEAR: command not found ./test.sh: line 10: ${MONTH_${DAY}_YEAR}: bad substitution (6 Replies)
Discussion started by: vino_hymi
6 Replies

5. Shell Programming and Scripting

How to set a variable name from another variables value?

Experts, I want to set value of variables like this in bash shell: i=5 ; L=100 I want variable d5 (that is d(i) ) to be assign the value of $L , d$i=$L ; echo $d5 Not working Thanks., (3 Replies)
Discussion started by: rveri
3 Replies

6. Shell Programming and Scripting

Moving files from one directory to another based on 2 date variables

Hi All, I am currently coding for a requirement(LINUX OS) where I am supposed to move a file (Lets Call it Employee.txt) from Directory A to Directory B based on 2 date fields as below, Date_Current = 20120620 Date_Previous = 20120610 Source Directory : /iis_data/source Target... (11 Replies)
Discussion started by: dsfreddie
11 Replies

7. Shell Programming and Scripting

Help needed in perl; moving or copying the variables

Hi All, I have a perl script blc.pl; i have five variables a,b,c,d and e in this script. Now I want to copy the values of these variables into a data file,say abc.dat.. Can anybody please tell me how to do this? Any help is appreciated.. Thanks in advance (2 Replies)
Discussion started by: puneetkanchi
2 Replies

8. Shell Programming and Scripting

adding counter to a variable while moving in a loop

The scenario is like this : I need to read records from a file one by one and increment counter1, if a certain field matches with a number say "40"..the script should increment the counter2 and also extract a corresponding field from the same line and adding them one by one and redirecting the the... (5 Replies)
Discussion started by: mady135
5 Replies

9. Shell Programming and Scripting

Moving Content from one file to another with variables

Greetings All, Need some help, your input is appreciated. Scenario: I have five files, each has one line with comletely different content. I execute the command to combine all the five lines of content into a single file. Goal: I would like to take that single file that has the... (3 Replies)
Discussion started by: royarellano
3 Replies

10. Shell Programming and Scripting

how to seperate a variable in 2 variables

Dear all, i dont know how to split one variable value in 2 variable. please send me any example. variable1= "abcde developer" now i want to seperate the values and seperator is space. (6 Replies)
Discussion started by: intikhabalam
6 Replies
Login or Register to Ask a Question