Append values before a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append values before a string
# 1  
Old 04-11-2009
Question Append values before a string

hi all,
i have variables a and b with values, like
a="/var/tmp/new.sh /var/tmp/new2.sh"
b="/TEST"

how i need to append the value "/TEST" before the values for the variable "a" so that i get the output as

/TEST/var/tmp/new.sh /TEST/var/tmp/new2.sh

plz help me

Regards,
NG
# 2  
Old 04-11-2009
Hi,
this should work:
a="/var/tmp/new.sh /var/tmp/new2.sh"
b="/TEST"
for i in $a; do
echo "$b$i"
done


Bye
# 3  
Old 04-11-2009
If your first variable would contain only one value it would be a simple concatenation:

Code:
a="/my/content"
b="/additional"

c="${a}${b}"
print - "$c"

You might consider changing your variable $a to an array where each element contains exactly one value. You could use the above mechanism then:

Code:
a[1]="/my/content1"
a[2]="/my/content2"
a[3]="/my/content3"
a[4]="/my/content4"

b="/additional"
i=1

while [ $i -le ${#a[*]} ] ; do
     c="${b}${a[$i]}" ; print - "$c" # only display the changed value
     # a[$i]="${b}${a[$i]}"          # alternatively store it back to the array
     (( i += 1 ))
done

If you want to stick with your way of storage content use a for-loop to cycle through all the elements of your variable and add the additional content to every element one at a time:

Code:
a="/my/content1 /my/content2 /my/content3 /my/content4"
b="/additional"
i=""
newstring=""

for i in $a ; do
     c="${b}${i}" ; print - "$c"    # only display the changed value
     # newstring="${newstring}${c}" # alternatively concatenate to a new string
done

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

<< generate alphabets and append in the input values >>

Hi Team, Pls help to get the desired output. I have a input like below nodecount=10 host=na7-db1-1-chi nodecount can be 10 or 8 based on this we need a output (in single line) like below na7-db1-1-chi:A na7-db1-2-chi:B na7-db1-3-chi:C na7-db1-4-chi:D na7-db1-5-chi:E... (4 Replies)
Discussion started by: kamauv234
4 Replies

2. Shell Programming and Scripting

Append values of duplicate entries

My input file is: LOC_Os01g01870 GO:0006139 LOC_Os01g01870 GO:0009058 LOC_Os01g02570 GO:0006464 LOC_Os01g02570 GO:0009987 LOC_Os01g02570 GO:0008152 LOC_Os01g04380 GO:0006950 LOC_Os01g04380 GO:0009628 I want to append the duplicate values in a tab/space... (2 Replies)
Discussion started by: Sanchari
2 Replies

3. Shell Programming and Scripting

Find string in file and append new string after

Hi All, I'm trying to insert a string into a file at a specific location. I'd like to add a string after the parent::__construct(); in my file. <?php if (! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Controller extends CI_Controller { function... (6 Replies)
Discussion started by: jjkilpatrick
6 Replies

4. UNIX for Dummies Questions & Answers

How to append values to a string?

Hi, Requesting some help with a problem I am facing with string function in UNIX. I wish to create 2 string variables: 1st header string containing output_1, output_2, .. , output_<n> and 2nd data string containing the filename separated by colon (":") and corresponding filesize separated by... (6 Replies)
Discussion started by: vkumbhakarna
6 Replies

5. Shell Programming and Scripting

Append a searched string with another string using sed

Hi, I need to replace and append a string in a text if grep is true. For eg: grep ABC test.txt | grep -v '\.$' | awk {'print $4'} | sed "s/ ? How do I replace all instances of "print $4" using sed with another sring? Eg of the string returned will be, lx123 web222 xyz Want to... (8 Replies)
Discussion started by: vchee
8 Replies

6. UNIX for Dummies Questions & Answers

Append a string on the next line after a pattern string is found

Right now, my code is: s/Secondary Ins./Secondary Ins.\ 1/g It's adding a 1 as soon as it finds Secondary Ins. Primary Ins.: MEDICARE B DMERC Secondary Ins. 1: CONTINENTAL LIFE INS What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Discussion started by: newbeee
4 Replies

7. AIX

How to append spaces to string values?

i/o file: abc,efg,xyz Required o/p file: "abc (Value + blank spaces=16) " ,"efg (Value +blank spaces=15) " ,"xyz (Value+ blank spaces =20) " In short input file value stores in result file with " i/p Value " added with spaces and are of fixed size like 16,15,20 How to do using... (2 Replies)
Discussion started by: AhmedLakadkutta
2 Replies

8. Shell Programming and Scripting

Need help with shell, trying to append or separate values in a string

Ok. I for the life of me cant figure out how to do this. I need Help. So here is what I'm trying to do. I have a block of text. They are FIPS codes for counties. Below is the block. There are probably a few ways to do this. The first line starting with ARC021....... this line is a list of... (2 Replies)
Discussion started by: chagan02
2 Replies

9. UNIX for Dummies Questions & Answers

use awk to append values

Hi, I have a file like this: tag1:value1 tag2:value2 tag3:value3 tag1:value1 tag2:value2 tag3:value3 tag1:value1 tag2:value2 tag3:value3 and what i want is: value1 value2 value3 value1 value2 value3 (15 Replies)
Discussion started by: nickrick
15 Replies

10. Shell Programming and Scripting

Search a string and append text after the string

Hi, I have a file like this... <o t="Batch" id="8410" p="/" g="32"> <a n="name"> <v s="DBBA1MM"/> </a> <a n="owner"> <v r="/Administrator"/> </a> <a n="rights"> <v s="95"/> </a> <a n="debugLevel"> <v s="3"/> </a> <a n="avsStoreLoc"> <v... (8 Replies)
Discussion started by: kesu2k
8 Replies
Login or Register to Ask a Question