append to same string variable in loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting append to same string variable in loop
# 1  
Old 11-25-2011
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.
# 2  
Old 11-25-2011
Code:
 
$ echo ${a}
test

$ a=${a}TEST

$ echo $a
testTEST

# 3  
Old 11-25-2011
hi Itkamaraj,

Thanks for reply but this wont serve my purpose .. As i need to use that inside a recursive loop.
Let me provide example of what i need to do..
I have data in form of index but in words not numbers. I need to write that as shown
I need to go down one level till i find _ at end of string , that is why using recusive function. I will keep appending in the string variable with each call to recursive function and return to previous level once there is no more _'s.

Sample DATA::
Code:
level1   level2     level3     level4
Maths_
         geometry_
                      circles
                      triangles_
                                  equilateral-triangles
                      squares
         number theory_
                      primes
English_
         Nouns

OUTPUT
I need to write it in form:
Code:
maths
maths/geometry
maths/geometry/cirlces
maths/geometry/triangles
maths/geometry/triangles/equilateral-triangles
maths/geometry/squares
maths/numbertheory
maths/numbertheory/primes
:
:
English
English/nouns

------------------------------------------------------------

I have the function body i need help with defining the variable string .
Thanks in advance.
Moderator's Comments:
Mod Comment Please use code tags!

Last edited by vbe; 11-25-2011 at 05:43 AM.. Reason: use code tags for your code and data!
# 4  
Old 11-29-2011
help with navigating index in script

Can anyone please brainstorm and help me ?

I gave up the idea of using string variable as it keeps growing larger with each recursive function the control goes in rather than maintaining a value when control comes back at a given level .i.e.
Code:
stringvar="Maths/geometry/circles/triangles/equilateral-triangles"

I am trying to implement it by using array variable but Cant seem to make head way as of now ?Smilie
# 5  
Old 11-29-2011
Quote:
Originally Posted by Prev
hi Itkamaraj,

Thanks for reply but this wont serve my purpose .. As i need to use that inside a recursive loop.
Let me provide example of what i need to do..
I have data in form of index but in words not numbers. I need to write that as shown
I need to go down one level till i find _ at end of string , that is why using recusive function. I will keep appending in the string variable with each call to recursive function and return to previous level once there is no more _'s.

Sample DATA::
Code:
level1   level2     level3     level4
Maths_
         geometry_
                      circles
                      triangles_
                                  equilateral-triangles
                      squares
         number theory_
                      primes
English_
         Nouns

OUTPUT
I need to write it in form:
Code:
maths
maths/geometry
maths/geometry/cirlces
maths/geometry/triangles
maths/geometry/triangles/equilateral-triangles
maths/geometry/squares
maths/numbertheory
maths/numbertheory/primes
:
:
English
English/nouns

------------------------------------------------------------

I have the function body i need help with defining the variable string .
Thanks in advance.
Moderator's Comments:
Mod Comment Please use code tags!
Code:
# awk '/^[Aa-zZ]*_$/{delete d;k=0};NR>1{gsub(/ */,"",$0);
if(match($0,"[Aa-zZ]*[Aa-zZ]*_$",a))
{d[x]=d[x]"/"a[0];sub(/_/,"",d[x]);sub(/^\//,"",d[x]);gsub(/ */,"",d[x]);k++;print d[x];w=0;}
else{w++;for(i=1;i<w;i++){sub(/\/[Aa-zZ]*$/,"",d[x])};d[x-1]=$0;sub(/ */,"",d[x-1]);
print d[x]"/"d[x-1]}w}{for(i=1;i<w;i++){sub(/\/[Aa-zZ]*$/,"",d[x])}}' infile
Maths
Maths/geometry
Maths/geometry/circles
Maths/geometry/triangles
Maths/geometry/triangles/equilateral-triangles
Maths/geometry/squares
Maths/numbertheory
Maths/numbertheory/primes
English
English/Nouns

# 6  
Old 11-29-2011
Dear ygemici,
Can you explain what awk is doing.

Thanks in Advance.
Regards,
Ajay
# 7  
Old 11-29-2011
Quote:
Originally Posted by CaapAjayShukla
Dear ygemici,
Can you explain what awk is doing.

Thanks in Advance.
Regards,
Ajay
firstly i m not be sure that's the way is best solution but i just tried something.
well,let me try to explain as much as i ..
i removed "k" variable because of it's unnecessary..maybe more revisions can be done..
Code:
"/^[Aa-zZ]*_$/"

-> [[ if pattern has only like "string_" then delete array named "d".
[[part of `delete array` actually will be used later for new starts (for example Maths_ or "English_" string]]
Code:
"NR>1"

-> [[ when the line number is greater than 1,then process on the records ]]
Code:
"gsub(/ */,"",$0);"

-> [[ if the record ($0) has spaces then remove its. ]]

Code:
"if(match($0,"[Aa-zZ]*[Aa-zZ]*_$",a))"

-> [[ if pattern matchs "string_" ]]
then
[[ a[0]=string_ and create array named "d" and d[x] elemnt = d[x] and a[0] (a[0] equals "string_" like "geometry_") ]]

Code:
sub(/_/,"",d[x]); -> [[ remove "_" ]]
sub(/^\//,"",d[x]); -> [[ remove "/" from beginning of the record ]]
gsub(/ */,"",d[x]); -> [[ remove all spaces if has ]]

Code:
print d[x];w=0;}

-> [[ print d[x] element and w is zero ( just think as a write/print control) ]]

Code:
else
{w++;

-> increase `w` (w is actually both print_control and index for added records (from d[x]=d[x]"/"a[0] )
Code:
for(i=1;i<w;i++)

-> [[ if we dont have "string_" (like "circles" line) my goal must be remove last of the array element (d[x]) (`w` times)
here is what `w` for index for "with "_" or non"_" strings ]]

Code:
{sub(/\/[Aa-zZ]*$/,"",d[x])};

-> [[ remove end of the array element
[[for example my goal must be `Maths/geometry/squares` to `Maths/numbertheory`
so must remove 2 times and add ("numbertheory") to end of array element []d[x][]
[[(actually array is not mandatory to use,but i like arrays Smilie)]]

Code:
d[x-1]=$0;

-> [[ and new record is d[x-1] [another array element]]]
Code:
sub(/ */,"",d[x-1])

-> [[remove spaces if has ]]
Code:
print d[x]"/"d[x-1]}w}

-> [[print both d[x] and d[x-1] [[ end of if loop ]]
Code:
{for(i=1;i<w;i++){sub(/\/[Aa-zZ]*$/,"",d[x])}}

-> [[ remove last of the array element (d[x]) (`w` times) from the d[x]
so we get a new array with main d[x] and re-start from it for same operations and both states which for new or else (else portion of if-else) ]]

regards
ygemici

Last edited by ygemici; 11-29-2011 at 03:15 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append has prefix in while loop

I was using below script to grep one file. I need to append the output using prefix Data of all-Vms-1.txt server-1 frame-1 LUN001 server-2 frame-1 LUN002 Data of all-vm-unix.txt server-1 24 server-2 50 Script used while read -r g h ; do cat all-Vms-1.txt |grep... (5 Replies)
Discussion started by: ranjancom2000
5 Replies

2. Shell Programming and Scripting

How to pre-append a variable string to each line in a file?

How to pre-append a variable string to each line in a file contains both single and double quotes? The variable string has no quotes in it. thank you very much. :confused: (8 Replies)
Discussion started by: dtdt
8 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. Shell Programming and Scripting

String variable concatenation through loop problem

Hi Team!! Please can anyone tell me why the following line does not work properly? 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... (8 Replies)
Discussion started by: paladinaeon
8 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. Shell Programming and Scripting

append blank spaces at the end of a variable string

Hello, could you please help with this one. I have an input file like this: 123,4567,89000 123456789,9876543,12 and for the output I need strings to be with the fixed length, let's say 15, and if the string is -lt 15 to be populated with blanks at the end until it reach 15, like this: 123 ,4567... (1 Reply)
Discussion started by: apenkov
1 Replies

7. 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

8. 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

9. 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

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