String variable concatenation through loop problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String variable concatenation through loop problem
# 1  
Old 03-10-2013
String variable concatenation through loop problem

Hi Team!!
Please can anyone tell me why the following line does not work properly?
Code:
str3+=$str2

it seems that str3 variable does not keep its value in order to be concatenated in the next iteration! Thus when i print the result of the line above it returns the str2 value
What i want to do is to add to the existing value of str3 the str2 value (after some trimming).
For example,
if the str2 value has the following values through the iteration
Code:
1
0 1
0 0 1
0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

i want in the end the str3 value to be
Code:
1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

Any help will be appriciated
Panos

Code:
declare -i i=0
declare -i len=0
declare -i lenOld=0

#str2=""
#str3=""
cat $2 | while read line2	
do
	echo $line2
	str2=""
	
    for word2 in $line2;
		str2=""
		i=$((0))
		cat $1 | while read line1
		do
			i=$(( i + 1 ))
			#echo $word2 " " $line1
			if [[ $word2 == *"$line1"* ]]
			then
				str2+=" 1"
				i=$((308))
			else
				str2+=" 0"
			fi
		
			if [ "$i" -eq "308" ]
			then
				
				len=${#str2}

				echo $str2
				str3+=$str2
				strTmp=${str3:$lenOld:$len}
				echo $str3 
				lenOld=$len
				break
			fi
		done
		
	done
done


Last edited by Scrutinizer; 03-10-2013 at 02:04 PM.. Reason: some more code tags
# 2  
Old 03-10-2013
I believe you could use

var=$var" 0"
# 3  
Old 03-10-2013
Quote:
Originally Posted by PikK45
I believe you could use

var=$var" 0"
Thanks for your reply but this will not help me since that what i want to do is "add" the lines of the str2 to form a single line for str3.
My problem is to create a line of 0 and 1 that describe the words found from the comparison of two files.
# 4  
Old 03-10-2013
guess that is also be possible, str3=$str3$str2
# 5  
Old 03-10-2013
Quote:
Originally Posted by PikK45
guess that is also be possible, str3=$str3$str2
I have tried all of them!!! My problem is that when the program returns to for for the next iteration str3 loses its contents!!! ...and equals again to str2
# 6  
Old 03-10-2013
That is probably because for example you are using:
Code:
cat $1 | while read line1

causing the while loop to run in a subshell. When the subshell finishes, in most shells this means the variables will be lost.
Instead you could try:
Code:
while read line1
do
  ...
done < "$1"

# 7  
Old 03-10-2013
Thanks friend that was the mistake!!
Any idea how from a variable that through an iteration takes the following values
Code:
1
0 1
0 0 1
0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

i can end up after the end of the iteration to a variable that has the following form?

Code:
1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

One solution that came to my mind was to initialize a string variable with zeros end when ever i have a match to go to the zeros variable and at the ith position to put a 1.

My question is how to go to a specific index into the string and change the value to 1.
Thanks

Last edited by Scrutinizer; 03-10-2013 at 12:18 PM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

String concatenation problem

Hi there, I'm writing a basic script where I want to make a string of 2 numeric fields from a file, which I have done, but the behavior is rather confusing. I have a file of random values such as: 1 2 3 4 5 6 7 8 9 10 and my awk code is: BEGIN { FS = " " } { str = str $1 $2 } END {... (7 Replies)
Discussion started by: HMChadwick
7 Replies

2. Shell Programming and Scripting

append to same string variable in loop

I want to append values to same string variable inside a recursive function that I have .. I do not want to write to any file but use a variable.. Can anyone please help with it? Thanks in advance. (6 Replies)
Discussion started by: Prev
6 Replies

3. Shell Programming and Scripting

how to test input variable is a string in a select loop

Okay -- I hope I ask this correctly. I'm working on my little shell script to write vendor names and aliases to files from user input. If a user choose to add to a file, he can do that as well. I'm using a select loop for this function to list all the possible files the user can choose from.... (7 Replies)
Discussion started by: Straitsfan
7 Replies

4. Shell Programming and Scripting

String / Variable Concatenation

Hi all, I'm trying to build a variable name automatically through a for loop for a script I'm working on, basically I want to build the variables named: $JVM_HOME0 or $JVM_HOME1 so that I can loop through some file copy/deletes and a server restart once completed. With the code below, I get this... (3 Replies)
Discussion started by: hydroponx
3 Replies

5. Shell Programming and Scripting

Adding string variable dynamically in for loop

Hi, I need to generate the text name dynamically in for loop, ex, VAR_COPY_FILE1= file path 1 VAR_COPY_FILE2= file path 2 VAR_COPY_FILE3= file path 3 for i in 1 2 3 do if then "do some process here" fi done (3 Replies)
Discussion started by: msubash26
3 Replies

6. Shell Programming and Scripting

String/Variable Concatenation

Hello, Trying to concatenate the following using bourne shell: # !/bin/bash # this works in bash shell e.g. get the results I am expecting fnTmp=C$cindex.$station_0.$station_1.$station_3.$ts.tmp # # under !/bin/sh # the results are not the same Any assistance would be... (8 Replies)
Discussion started by: LAVco
8 Replies

7. Shell Programming and Scripting

String concatenation not working in a loop

Hi, First post, so I hope someone can help me with this weirdness :) I have a number files with some rows of information I want to extract, at the same time I want to add to a string some details from the file. I have found two different ways of looping over rows in a file, but one method... (5 Replies)
Discussion started by: LostInTheWoods
5 Replies

8. Shell Programming and Scripting

String Concatenation

Hi All, I need to concatenate the values in the array into a variable. Currently the code is : for (( i=1 ; i <= $minCount ; i++ )) do var="${var}""${sample_file}" done The output is : /tmp/1/tmp/2/tmp/3/tmp/4/tmp/5/tmp/6/tmp/7/tmp/8/tmp/9/tmp/10 I need a space between... (1 Reply)
Discussion started by: sh_kk
1 Replies

9. Shell Programming and Scripting

Help concatenation string and variable

Hello, in my script i have this lines of code in a while cycle: .. let j=i+1 t_prod_$i = `cat myfile.csv | grep world | cut -d ";" -f$j` let i+=1 ... So if i try an echo $t_prod_$i at the end of the cycle i cannot see the right value obtained by `cat myfile.csv | grep world |... (5 Replies)
Discussion started by: drain
5 Replies

10. UNIX for Dummies Questions & Answers

string concatenation

my input file contains thousands of lines like below 234A dept of education 9788 dept of commerce 8677 dept of engineering How do i add a delimeter ':' after FIRST 4 CHARACTERS in a line 234A:dept of education 9788:dept of commerce 8677:dept of engineering (7 Replies)
Discussion started by: systemsb
7 Replies
Login or Register to Ask a Question