Looping in case of duplicates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping in case of duplicates
# 8  
Old 10-15-2013
Check this link as well as this one
What you are trying to do is relating to tree walk (chained list) have a look at the algorithm used to build leaf path.
This User Gave Thanks to ctsgnb For This Post:
# 9  
Old 10-15-2013
Using awk:

Code:
awk -F'`~' '
function from(n,pre,i,v,x)
{
    if(n in T) {
       v=split(T[n],x,",")
       for(i=1; i<=v;i++)
          from(F[x[i]], pre "~" L[x[i]]);
    } else print substr(pre,2);
}
{
   L[NR]=$1;
   F[NR]=$4;
   if($3 in T) T[$3]=T[$3]","NR;
   else T[$3]=NR;
}
END { from("SNA") }' infile

---------- Post updated at 12:44 PM ---------- Previous update was at 10:57 AM ----------

This can also be done with bash, however as associative arrays aren't supported I used a dummy number (9999) for SNA. Also bash will have tighter memory constraints so it will fail on smaller files than the awk solution:

Code:
#!/bin/bash
function from()
{
 local pre=$2
 if [ ${#T[$1]} -gt 0 ]
 then
     set ${T[$1]}
     while [ $# -gt 0 ]
     do
        from "${F[$1]}" "$pre~${L[$1]}"
        shift
     done
  else
     echo ${pre:1}
  fi
}

while read line
do
   set ${line//\`~/ }
   ((L[++ln]=$1))
   ((F[ln]=$4))
   [ "$3" = "SNA" ] && T[9999]="${T[9999]} $ln" || T[$3]="${T[$3]} $ln"
done < infile

from 9999

This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change first letter of a word from lower case to upper case

Hi all, I am trying to find a way to change first letter in a word from lower case to upper case. It should be done for each first word in text or in paragraph, and also for each word after punctuation like . ; : ! ?I found the following command sed -i 's/\s*./\U&\E/g' $@ filenamebut... (7 Replies)
Discussion started by: georgi58
7 Replies

2. Shell Programming and Scripting

Looping in case of duplicates ...

complinagetest () #function name { if ;then rm complins.dat fi touch complins.dat i=0 while read line do if ; then va=`grep -w "$line" datalins1.dat | awk BEGIN'{FS="\`~"}{if ( $3=="'$line'" ) {print $4}}'` i=$(($i+1)) varits=$(echo $va|awk -v varif="$i" '{print... (1 Reply)
Discussion started by: amitpaul01
1 Replies

3. Shell Programming and Scripting

multiple looping with case and funtion showing error, Please help

Hello All, I have code as follows :- while true do {opening a case1 statement} 1) {opening another case2 statement} {closing case 2} 2) Showing error for "2)" as Syntax error at line 59 : `)' is not expected. *) {closing case 1} ... (5 Replies)
Discussion started by: Renjesh
5 Replies

4. Shell Programming and Scripting

Conversion from Upper Case to Lower Case Condition based

Hello Unix Gurus : It would be really appreciative if can find a solution for this . I have records in a file . I need to Capitalize the records based on condition . For Example i tried the following Command COMMAND --> fgrep "2000YUYU" /export/home/oracle/TST/data.dat | tr '' ''... (12 Replies)
Discussion started by: tsbiju
12 Replies

5. Shell Programming and Scripting

sed ignoring case for search but respecting case for subtitute

Hi I want to make string substitution ignoring case for search but respecting case for subtitute. Ex changing all occurences of "original" in a file to "substitute": original becomes substitute Origninal becomes Substitute ORIGINAL becomes SUBSTITUTE I know this a little special but it's not... (1 Reply)
Discussion started by: kmchen
1 Replies

6. Shell Programming and Scripting

data array needs to change upper case to lower case

Hi all, i have a data array as followes. ARRAY=DFSG345GGG ARRAY=234FDFG090 ARRAY=VDFVGBGHH so on.......... i need all english letters to be change to lower case. So i am expecting to see ARRAY=dfsg345ggg ARRAY=234fdfg090 ARRAY=vdfvgbghh so on........ If i have to copy this data in... (8 Replies)
Discussion started by: usustarr
8 Replies

7. SCO

Avoiding duplicates with some special case

Hi Gurus, I had a question regarding avoiding duplicates.i have a file abc.txt abc.txt ------- READER_1_1_1> HIER_28056 XML Reader: Error occurred while parsing:; line number ; column number READER_1_3_1> Sun Mar 23 23:52:48 2008 READER_1_3_1> HIER_28056 XML Reader: Error occurred while... (0 Replies)
Discussion started by: pssandeep
0 Replies

8. Shell Programming and Scripting

Script needed to select and delete lower case and mixed case records

HELLO ALL, URGENTLY NEEDED A SCRIPT TO SELECT AND DELETE LOWER AND MIXED CASE RECORDS FROM A COLUMN IN A TABLE. FOR EXAMPLE : Table name is EMPLOYEE and the column name is CITY and the CITY column records will be: Newyork washington ... (1 Reply)
Discussion started by: abhilash mn
1 Replies

9. UNIX for Dummies Questions & Answers

lower case to upper case string conversion in shell script

How can convert a Lower case variable value to an upper case in the kron shell script. (3 Replies)
Discussion started by: dchalavadi
3 Replies
Login or Register to Ask a Question