how to concatenate values of two variables with an underscore(_) in between


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to concatenate values of two variables with an underscore(_) in between
# 1  
Old 06-16-2009
how to concatenate values of two variables with an underscore(_) in between

Hi,
I'm new to shell programming.

I have two variables a and b
a=val1
b=val2

could anyone kindly post the shell script to concatenate the values of variable a and b with an underscore(_) in between?
The final output should be val1_val2.
# 2  
Old 06-16-2009
c="${a}_${b}"
echo "${c}"
# 3  
Old 06-16-2009
Code:
$ c="${a}_${b}"
$ echo $c
val1_val2

# 4  
Old 06-16-2009
There is some discripency in the result that I am getting in the below shell script.
I have a configuration file having data as
fileTest.conf:
----------------
file=abc
value=123

The Shell Script is:
------------------
ram=`grep 'file=' fileTest.conf | awk -F"=" '{print $2}'`
shyam=`grep 'value=' fileTest.conf | awk -F"=" '{print $2}'`
echo $ram
echo $shyam
c="${ram}_${shyam}"
echo "$c"
Output:
-------
abc
123
_123

The output I want is
abc
123
abc_123
Could someone please let me know what the problem is in the script and how should be the correct script look like?
# 5  
Old 06-16-2009
There is nothing wrong with your script if you are using either ksh or bash.
What shell are you using?
# 6  
Old 06-16-2009
i'm using bash
# 7  
Old 06-16-2009
Quote:
Originally Posted by badrimohanty
There is some discripency in the result that I am getting in the below shell script.
I have a configuration file having data as
fileTest.conf:
----------------
file=abc
value=123

The Shell Script is:
------------------
ram=`grep 'file=' fileTest.conf | awk -F"=" '{print $2}'`
shyam=`grep 'value=' fileTest.conf | awk -F"=" '{print $2}'`
echo $ram
echo $shyam
c="${ram}_${shyam}"
echo "$c"
Output:
-------
abc
123
_123

The output I want is
abc
123
abc_123
Could someone please let me know what the problem is in the script and how should be the correct script look like?


Code:
You have a "\n" in $ram and problem will be solved if you modify the first awk to be like below

awk -F"=" '{printf"%s",$2}'`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate column values when header is Matching from multiple files

there can be n number of columns but the number of columns and header name will remain same in all 3 files. Files are tab Delimited. a.txt Name 9/1 9/2 X 1 7 y 2 8 z 3 9 a 4 10 b 5 11 c 6 12 b.xt Name 9/1 9/2 X 13 19 y 14 20 z 15 21 a 16 22 b 17 23 c 18 24 c.txt Name 9/1 9/2... (14 Replies)
Discussion started by: Nina2910
14 Replies

2. Shell Programming and Scripting

Concatenate values in the first column based on the second column.

I have a file (myfile.txt) with contents like this: 1.txt apple is 3.txt apple is 5.txt apple is 2.txt apple is a 7.txt apple is a 8.txt apple is a fruit 4.txt orange not a fruit 6.txt zero isThe above file is already sorted using this command: sort -k2 myfile.txtMy objective is to get... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

3. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

4. Shell Programming and Scripting

Concatenate two variables and form the third variable

Hi Guys, I was scratching my head for this for half a day... finally not successful :confused: Following is the problem I have a variable $ var1=123 $ var2-234 $ var3=345 and another Variable $ i=1 Now i wanted to save these into a Variable as shown below for i in 1 2 3 do... (5 Replies)
Discussion started by: ramprabhum
5 Replies

5. Shell Programming and Scripting

Concatenate last field values for all occurences

Hello all, Maybe you can help me with an awk script to get what I need. I have the input file with format below: REQUEST|79023787741690|738227864597|985 REQUEST|79024002151717|738229423534|985 REQUEST|79024002151717|738229423534|*985 NDS-REQUEST|79024002151717|738229423534 ... (4 Replies)
Discussion started by: Ophiuchus
4 Replies

6. Shell Programming and Scripting

Help cannot concatenate Ksh variables ?

Cannot combine these two strings into one line, either as a 3rd variable or echo or printing ? Frustrating. for i in `cat /scripts/pathList.dat` do OldRepo= grep Oldhostname ${i}/.svn/entries | tail -1 NewRepo= grep Oldhostname ${i}/.svn/entries | tail -1 | sed '/Oldhostname/... (41 Replies)
Discussion started by: pcpinkerton
41 Replies

7. Shell Programming and Scripting

concatenate variables

I need to know how to concatenate variables in Debian. I am making a interactive script where it ask the user for info to add a user I pull the first letter from the first middle and last name into individual variables now i want to put them all in one variable so i can put it into useradd command ... (4 Replies)
Discussion started by: HackerSeabass
4 Replies

8. Shell Programming and Scripting

concatenate variables problem

Hello, I have a tricky problem: I have a $file with a variable number of occurrences of "ORA-" (in this case two) .......... EXP-00008: ORACLE error 3113 encountered ORA-03113: end-of-file on communication channel EXP-00056: ORACLE error 1403 encountered ORA-01403: no data found... (9 Replies)
Discussion started by: Laurentiu
9 Replies

9. Shell Programming and Scripting

concatenate consecutive field values

Hi, I have a file like this A Bob A Sam A John B David C Paul C Sandra If the consecutive field values in column one is same, then concatenate the corresponding strings. So, I need an output like this, A Bob_Sam_John B David C Paul_Sandra I usually work with excel but... (3 Replies)
Discussion started by: polsum
3 Replies

10. Shell Programming and Scripting

How to use values in variables

Hi Legends, Please help me in solving the below: I have the file size and file name using `ls` and awk command. ls -ltr *.pf |awk '{print $5, $9}' 2000003072 ABC.pf 2000003072 DEF.pf 2000003072 GHI.pf 56000588 JKL.pf 2000003072 MNO.pf 2000003072 PQR.pf Now, I want to take the... (3 Replies)
Discussion started by: sdosanjh
3 Replies
Login or Register to Ask a Question