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.
$ echo ${a}
test
$ a=${a}TEST
$ echo $a
testTEST
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::
level1 level2 level3 level4
Maths_
geometry_
circles
triangles_
equilateral-triangles
squares
number theory_
primes
English_
Nouns
OUTPUT
I need to write it in form:
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.
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.
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 ?
# 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
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..
"/^[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]]
"NR>1"
-> [[ when the line number is greater than 1,then process on the records ]]
"gsub(/ */,"",$0);"
-> [[ if the record ($0) has spaces then remove its. ]]
"if(match($0,"[Aa-zZ]*[Aa-zZ]*_$",a))"
-> [[ if pattern matchs "string_" ]]
then
[[ a[0]=string_ and create array named "d" and d elemnt = d and a[0] (a[0] equals "string_" like "geometry_") ]]
sub(/_/,"",d[x]); -> [[ remove "_" ]]
sub(/^\//,"",d[x]); -> [[ remove "/" from beginning of the record ]]
gsub(/ */,"",d[x]); -> [[ remove all spaces if has ]]
print d[x];w=0;}
-> [[ print d element and w is zero ( just think as a write/print control) ]]
else
{w++;
-> increase `w` (w is actually both print_control and index for added records (from d=d"/"a[0] )
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) (`w` times)
here is what `w` for index for "with "" or non"" strings ]]
{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
[[(actually array is not mandatory to use,but i like arrays :))]]
d[x-1]=$0;
-> [[ and new record is d[x-1] [another array element]]]
sub(/ */,"",d[x-1])
-> [[remove spaces if has ]]
print d[x]"/"d[x-1]}w}
-> [[print both d and d[x-1] [[ end of if loop ]]
{for(i=1;i<w;i++){sub(/\/[Aa-zZ]*$/,"",d[x])}}
-> [[ remove last of the array element (d) (`w` times) from the d
so we get a new array with main d and re-start from it for same operations and both states which for new or else (else portion of if-else) ]]
regards
ygemici