Challenging scenario


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Challenging scenario
# 1  
Old 12-16-2011
Challenging scenario

Hi,

My input file contains

Code:
1,2
2,4
3,6
4,9
9,10

My expected output is

Code:
1,10
2,10
3,6
4,1
9,10

the logic is we need to chain the records, find out the target.

How we can achieve this in unix?

Thanks
# 2  
Old 12-17-2011
Shouldn't output be:
Code:
1,10
2,10
3,6
4,10
9,10

?
# 3  
Old 12-17-2011
@pandeesh: How do you arrive at output from input? What's the logic?
# 4  
Old 12-17-2011
Quote:
Originally Posted by bartus11
Shouldn't output be:
Code:
1,10
2,10
3,6
4,10
9,10

?
Yes.You are correct. That's 4,10.

---------- Post updated at 11:58 PM ---------- Previous update was at 11:56 PM ----------

Quote:
Originally Posted by balajesuri
@pandeesh: How do you arrive at output from input? What's the logic?
You need t chain through the input.
For example, take the first record
Code:
1--->2--->4-->9---->10

So the output will be 1,10.

I hope you get it.

thanks
# 5  
Old 12-17-2011
Try this script:
Code:
#!/usr/bin/perl
open I,$ARGV[0];
while (<I>){
  chomp;
  @x=split /,/;
  $a{$x[0]}=$x[1];
}
for $i (keys %a){
  $t=$a{$i};
  $b{$i}=$a{$i};
  $q=1;
  while ($q){
    if ($a{$t}){
      $q=1;
      $b{$i}=$a{$t};
      $t=$a{$t};
    } else {
      $q=0;
    }
  }
}
for $i (keys %b){
  print "$i,$b{$i}\n";
}

Run it as: ./script.pl file
This User Gave Thanks to bartus11 For This Post:
# 6  
Old 12-17-2011
I see bartus11 posted a perl solution, here's the awk I was working on:

Code:
 awk -F , '
    BEGIN { max = 0; }
    {
        nxt[$1+0] = $2+0;
        if( $1+0 > max )
            max = $1;
        next;
    }
    END {
        for( i = 1; i <= max; i++ )
        {
            if( nxt[i] != "" )
            {
                n = i;
                while( nxt[n] != "" )
                    n = nxt[n];
                printf( "%d,%d\n", i, n );
            }
        }
    }
'  input-file

---------- Post updated at 15:34 ---------- Previous update was at 15:29 ----------

A bit cleaner, but doesn't preserve original order:

Code:
 awk -F , '
    { nxt[$1+0] = $2+0; }
    END {
        for( i in nxt )
        {
                n = i;
                while( n in nxt )
                    n = nxt[n];
                printf( "%d,%d\n", i, n );
        }
    }
' input-file


Last edited by agama; 12-17-2011 at 04:34 PM.. Reason: typo
This User Gave Thanks to agama For This Post:
# 7  
Old 12-18-2011
Also preserves order:
Code:
awk -F , 'FNR==NR {nxt[$1+0]=$2+0;next}
{for(n=$1;n in nxt;n=nxt[n]); print $1","n}' infile infile

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

Help in below scenario

Hi, my file has the data like below: 11,231,ABCVAV 22,AAHJHAj22,hdsjkhdls 22,dhskjhdkshd 22,gdgkdkadh 11,232,dgsjgdjh 22,ghdskahdkja 22,shdkajshs 11,233,ddjs 22,dhjkahkd 22,hsajhaah 11,231,sjkjsjj 22,ahkh 22,hsakh From the above i need only the records which starts as 11,231... (5 Replies)
Discussion started by: pandeesh
5 Replies

2. Shell Programming and Scripting

simple but challenging ignore case

Folks - I found this code on the forums to extract a paragraph for a matching pattern but I don't know how to make it ignore case. grep "-ip" is not an option for me as I am on SUSE LINUX. Thanks for ur help. I run this script as below: grep_para.ksh sqlstate < logfile "The end result... (2 Replies)
Discussion started by: beowulfkid
2 Replies

3. Shell Programming and Scripting

Challenging Awk array problem

Hi, I rather have a very complicated awk problem here, at least to me. I have two files. File 1: 607 687 174 0 0 chr1 3000001 3000156 -194195276 - L1_Mur2 LINE L1 -4310 1567 1413 1 607 917 214 114 45 chr1 3000237 ... (19 Replies)
Discussion started by: polsum
19 Replies

4. UNIX for Advanced & Expert Users

Very Challenging :Copy files in Multiple Threads

Hello all, I asked this in the basic Unix forum got no answer since one week. So I believe this is an advanced level question hence posting it here. Any suggestions welcome. I have a directory of files of varying sizes. I want to copy all these files in n number of threads to... (2 Replies)
Discussion started by: samoo
2 Replies

5. Shell Programming and Scripting

Need help with this challenging code....

Hello All, I am new to this forum, and the reason I came here is to seek solution from the experts. I have written following wrapper script, it was running fine from past couple of months, until last week. When one of the function in the script which suppose to login through ssh to the... (2 Replies)
Discussion started by: tajdar
2 Replies

6. Shell Programming and Scripting

Very Challenging Problem. Please read fully.

Hi, This is the Third thread i'm putting here for the same problem. :( Actually, i'm trying a script like this.. but its taking a long time.. about 3 days to complete fully.. #!/bin/ksh if then exit 1 fi while read i do while read j do field7=`echo $j|cut -d "|"... (12 Replies)
Discussion started by: RRVARMA
12 Replies

7. Programming

A challenging problem involving symbolic links.

Hello, I'm working on an application that bridges together several applications involved in creating a video workflow for editing with digital cinema cameras. The main platform is MacOSX. Because of the nature of some of the utilities for working with this video footage I must spoof filenames... (2 Replies)
Discussion started by: ibloom
2 Replies

8. Shell Programming and Scripting

Challenging!! Help needed

Hi, I have a script xyz.ksh which accpets two parameters the format of first one is :X_TABLENAME_Y and second one is a digit. I can extract a table name from that parameter and store it in a variable var_tblnm, so if i pass a parameter X_TABLE1_Y the value in var_tblenm is "TABLE1" now i have... (1 Reply)
Discussion started by: hcdiss
1 Replies

9. UNIX for Dummies Questions & Answers

A Challenging situation for the MODERATORS

Well, I hope this way you will respond to my inquiries. I have 4 unix servers,with static ips (though i dont think this is an issue)....i can telnet and rlogin from one to the other....if i FTP from on et othe other and try to execute : cd /user return /user : no such file or... (1 Reply)
Discussion started by: BAM
1 Replies

10. UNIX for Advanced & Expert Users

Very Challenging Question! Need help bad!

I am in desperate need of an answer to this question. I have looked everywhere (even the man pages) and found very little. Solaris has the concept of "plumbing" a network interface. What does this mean? I would be really greatful to whoever could help me answer this question. I am so... (1 Reply)
Discussion started by: Sparticus007
1 Replies
Login or Register to Ask a Question