Logic needed to recursive looping in the script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Logic needed to recursive looping in the script
# 1  
Old 07-27-2011
Logic needed to recursive looping in the script

Hello

i have a requirement where in a file i will get string. The length could be from 1 to 20. if the string is less than 6 characters ( ex: ABCD) . i need to append 'X' on right hand side to make it 6 characters (ex: ABCDXX). if suppose i get the same string from the file as ABCDXX then i should not touch this string, where as i need to append one more X to the original ABCD to make it ABCDXXX and again i need to search if ABCDXXX again exists in the file. if it exists again i should not touch that string in the file where as i need to append ABCDXXXX to make it distinct and this will continue.

Here is example in the File.

Code:
ABCD ---should be converted to ABCDXX to make it six characters
ABCDX-- should be converted t0 ABCDXX to make it six characters
ABCDXX--should reamin the same.

in the above case we have 3 ABCDXX strings after conversion. to make it unique
in the above case ABCD--ABCDXX--ABCDXXX should be converted like this.


ABCDXX-- from the file should not be touched as it satisfies the 6 character condition
and ABCDX--ABCDXX--ABCDXXXX should be converted like this.

this is how the final file will look like
Code:
ABCD------> ABCDXXX
ABCDX------> ABCDXXXX
ABCDXX-----> ABCDXX

Please help me with the logic

Thanks
dsdev

Last edited by radoulov; 07-28-2011 at 05:00 PM.. Reason: Code tags!
# 2  
Old 07-27-2011
Code:
$ cat infile
ABCD
ABCDX
ABCDXX

$ awk 'NR==FNR&&length($1)>=6{a[$1]} 
     NR>FNR{if (length($1)<6) { new=substr($1 "XXXXX",1,6);
                          while (new in a) {i++;new=substr($1 "XXXXXXXXXXXXXXXXX",1,6+i)}
                          a[new];print new;i=0
                        }
      else{print $1}
     }' infile infile

ABCDXXX
ABCDXXXX
ABCDXX


Last edited by rdcwayx; 07-27-2011 at 09:35 PM..
# 3  
Old 07-28-2011
wonderful. thank you very much.

can you please explain me the code.

Thanks
dsdev

---------- Post updated at 10:53 AM ---------- Previous update was at 10:21 AM ----------

Sorry for asking this

i want to generate on output file by putting the initial value and the new value geenrated separated by comma.
Code:
input file               outputfile
ABC                      ABC, ABCXXXX  
ABCD                    ABCD,ABCDXXXXX
ABCDX                  ABCDX,ABCDXXXXXX
ABCDXX                ABCDXX,ABCDXX
ABCDXXX              ABCDXXX,ABCDXXX
ABCDXXXX            ABCDXXXX,ABCDXXXX
ABCXXX                ABCXXX,ABCXXX
A                         A,AXXXXX
B                         B,BXXXXX

Thanks for your help
dsdev

Last edited by radoulov; 07-28-2011 at 05:01 PM.. Reason: Code tags!
# 4  
Old 07-28-2011
Code:
awk 'NR==FNR&&length($1)>=6{a[$1]} 
     NR>FNR{if (length($1)<6) { new=substr($1 "XXXXX",1,6);
                          while (new in a) {i++;new=substr($1 "XXXXXXXXXXXXXXXXX",1,6+i)}
                          a[new];print $1,$1","new;i=0
                        }
      else{print $1,$1","$1}
     }' input_file input_file

# 5  
Old 07-28-2011
Thankyou itworks.

dsdev
# 6  
Old 07-29-2011
Quote:
Originally Posted by panyam
Code:
awk 'NR==FNR&&length($1)>=6{a[$1]} 
     NR>FNR{if (length($1)<6) { new=substr($1 "XXXXX",1,6);
                          while (new in a) {i++;new=substr($1 "XXXXXXXXXXXXXXXXX",1,6+i)}
                          a[new];print $1,$1","new;i=0
                        }
      else{print $1,$1","$1}
     }' input_file input_file

sorry to ask this again
in the input file i have 2 columns and the logic should be applied on col2 and the 2 columns are separated by comma in the input file.The outfile should have 2 columns in the file and 3rd derived column separated by comma.






Code:
input file
outputfile
1,ABC
1,ABC, ABCXXXX
2,ABCD
2,ABCD,ABCDXXXXX
3,ABCDX
3,ABCDX,ABCDXXXXXX
4,ABCDXX
4,ABCDXX,ABCDXX
5,ABCDXXX
5,ABCDXXX,ABCDXXX
6,ABCDXXXX
6,ABCDXXXX,ABCDXXXX
7,ABCXXX
7,ABCXXX,ABCXXX
8,A
8,A,AXXXXX
9,B
9,B,BXXXXX


Thanks again
dsdev

Last edited by dsdev_123; 07-29-2011 at 04:30 PM.. Reason: Code tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Programming

Missing Logic Looping Through Switch Statement

Having trouble with the logic when looping over this switch case again: for (j = 0; data != 0; j++){ switch(data){ case 'c': output = ranit(r_brace_array); break; case 'h': output = ranit(pipe_array); break; ... (6 Replies)
Discussion started by: Azrael
6 Replies

2. UNIX for Dummies Questions & Answers

Looping Logic, Need to implement

I need to implement a looping logic. If then Go to /path1/file* Get all the filename starting with file* and store it in a array count file number and store it in variable like run Ex: I found 3 file with starting file* so my run = 3 means my loop should run three time May be... (1 Reply)
Discussion started by: Nsharma3006
1 Replies

3. UNIX for Dummies Questions & Answers

Help needed - find command for recursive search

Hi All I have a requirement to find the file that are most latest to be modified in each directory. Can somebody help with the command please? E.g of the problem. The directory A is having sub directory which are having subdirectory an so on. I need a command which will find the... (2 Replies)
Discussion started by: sudeep.id
2 Replies

4. Shell Programming and Scripting

Logic needed

input : employee_id, salary ------------------- 10, 1000 20, 2000 30, 3000 40, 5000 output: employee_id, salary, next_row_salary ------------------------------------ 10, 1000, 2000 20, 2000, 3000 30, 3000, ... (3 Replies)
Discussion started by: HemaV
3 Replies

5. Shell Programming and Scripting

Recursive looping through files and directories

hi; i need a script which will go to all directories and subdirectories and print the filenames as follow; here i m printing only files listing in current directory reason i m doing this is coz i want to perform some operations according to filename achieved so cant use find command;... (4 Replies)
Discussion started by: ajaypadvi
4 Replies

6. UNIX for Dummies Questions & Answers

logic needed to extract lines

Hi All, Are there any logic to extract the only lines between first two *TM* (which is marked in blue)? VOL1HST99 0 HDR1A999999S 999999HST99 HDR2F001200012001 UHL1 0729609000001 000000 *TM*^^^^^^^^^^^^^^^^^^^^^^ 01012610252171017301805000... (2 Replies)
Discussion started by: ganapati
2 Replies
Login or Register to Ask a Question