Sponsored Content
Top Forums Shell Programming and Scripting append to same string variable in loop Post 302577525 by ygemici on Tuesday 29th of November 2011 09:11:28 AM
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

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 09:36 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy