More a logic than a programming question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting More a logic than a programming question
# 1  
Old 02-02-2014
More a logic than a programming question

hello,

I'm a littlebit lost...

i have a table of single names and combination of two names and i want a table of ALL single names with ALL combinations (also the indirect ones).

- - - in reality the names are longer, not only a,b,.. and the table is tab-delimited, and the result table should also be tab-delimited - - -

input (is NOT sorted, only here for demonstration)
Code:
a
b
b c
c
c d
d
d c
e
f
f e
g
g c
h

output
Code:
 
a a
b b,c,d,g
c b,c,d,g
d b,c,d,g
e e,f
f e,f
g b,c,d,g
h h

Result: remove all redundancies, show all connections and sorted second column.

probably someone can give me some hints and code snippets (i know awk a little bit and can finish the program...)

thanks a lot,

dietmar

Last edited by dietmar13; 02-02-2014 at 07:37 AM.. Reason: addition
# 2  
Old 02-02-2014
What you have tried so far ?
# 3  
Old 02-02-2014
@Akshay

dear Akshay,

to be honest - not too much. as said - i am a littlebit lost...

i don't want that other people think for me, but that people with similar problems share their thoughts and solutions...

at the moment i also think about some solutions, but the indirect combinations pose difficulties...
# 4  
Old 02-02-2014
Hi dietmar13...

Have you any experience or understanding of coding at all?

If not do some research on here for help on the UNIX shell scripting environent and tutorials.

Answers to Frequently Asked Questions - The UNIX and Linux Forums

There is no way the guys on here can do a teaching class, you are on your own there.

They will always attempt solutions if they see an attempt at a solution from the OP however.
And some of them are exceptionally good at it...
# 5  
Old 02-02-2014
@wisecracker

dear wisecracker,

as said, i have awk experience but i have problems in programming logic for my problem.

one very rude solution would probably be (and i will try to program it now):
loop twice over all combinations, concatenating for all names all lines if one of the names is already present in the concatenated names list.
finally sort and make unique all concatenated names...

---------- Post updated at 10:55 AM ---------- Previous update was at 07:42 AM ----------

my first solution - don't blame me if you get eye- or brain-cancer from this code - you wanted to show my solution Smilie

in test.txt is the example from my first post (tab-delimited).

Quote:
awk 'BEGIN {FS=OFS="\t"} FNR==NR { name[$1]++;next } {for (i in name) {if (match($1,i) || match($2,i)) res[i]=res[i] "#" i "#" $1 "#" $2 "#"}} END {for (j=1;j<6;j++) {for (i in res) { while(( getline < "test.txt" ) > 0 ) {split($0,t,OFS); if ( match(res[i],"#" t[1] "#") || (t[2] != "" && match(res[i],"#" t[2] "#")) ) res[i]=res[i] "#" t[1] "#" t[2]; t[1]=t[2]=""}; close("test.txt")}}; for (i in res) {split(res[i],out,"#"); for (k in out) if(out[k]!="") kk=kk out[k] ","; print i, kk; kk=""}}' test.txt test.txt > test_out.txt
Code:
 
h h,h,h,h,h,h,h,
a a,a,a,a,a,a,a,
b b,b,c,c,c,d,d,d,d,c,c,g,g,g,c,b,g,c,b,b,b,c,b,c,b,b,c,c,b,b,c,c,d,d,g,c,b,b,c,c,b,c,c,c,d,c,d,d,c,d,d,d,c,d,c,g,d,c,g,g,c,g,c,
c c,c,d,c,d,c,d,c,d,g,c,g,g,g,c,b,g,c,b,b,b,c,c,c,b,b,c,c,c,c,c,c,c,d,d,c,d,d,d,c,g,g,c,c,b,b,c,c,b,c,c,c,d,c,d,d,c,d,d,d,c,d,c,g,d,c,g,g,c,g,c,b,b,c,
d d,d,c,b,c,c,c,c,c,d,d,d,d,d,c,g,d,c,g,g,g,c,d,b,c,g,d,c,b,d,d,b,c,c,d,d,c,g,c,b,g,c,b,b,c,b,c,c,b,c,c,c,d,c,d,d,c,d,d,d,c,d,c,g,g,c,
e e,f,f,e,e,f,f,f,e,e,f,f,e,e,e,e,f,e,f,e,e,f,f,e,e,
f e,f,f,e,e,f,f,f,e,e,f,f,e,f,f,f,f,e,f,e,e,f,f,e,e,
g b,c,c,c,d,d,d,c,g,d,c,g,g,g,c,b,g,c,b,b,b,c,g,c,g,b,c,c,g,g,c,c,d,d,g,c,b,b,c,c,b,c,c,c,d,c,d,d,c,d,d,d,c,d,c,g,d,c,g,g,c,g,c,

the clean-up and sorting will be easy...

---------- Post updated at 12:02 PM ---------- Previous update was at 10:55 AM ----------

i don't have to load the file three times, i could use always one array.

i have to concatenate all combinations and loop over this array (as often as necessary) and look if there is one of the two combinations present in the concatenation and than add the other of the two names.

Moderator's Comments:
Mod Comment Deleted rude comment. Gave infraction.
# 6  
Old 02-02-2014
Quote:
Originally Posted by dietmar13
dear wisecracker,

as said, i have awk experience but i have problems in programming logic for my problem.

one very rude solution would probably be (and i will try to program it now):
loop twice over all combinations, concatenating for all names all lines if one of the names is already present in the concatenated names list.
finally sort and make unique all concatenated names...

---------- Post updated at 10:55 AM ---------- Previous update was at 07:42 AM ----------

my first solution - don't blame me if you get eye- or brain-cancer from this code - you wanted to show my solution Smilie

in test.txt is the example from my first post (tab-delimited).


if i have to find the solution for myself then this forum is useless ... Smilie
Let me tell you frankly this forum has got prompt support, We won't tolerate this kind of behavior here, forum is from people and for people, there is no spoon feeding. if you are not getting reply it does not mean that forum is useless, member might be busy at their work, or trying to solve the issue but not replied or they might not understand your requirement there can be plenty of reasons for why you did not receive reply. if you are posting thread which does not reach people then perhaps your post might be useless.

Don't talk nonsense, there are many members who feels so proud about this forum and I am one of them.

Regards,
Akshay
# 7  
Old 02-02-2014
I have to second Akshay Hegde in that those kind of remarks are not really appreciated. Unfortunately I didn't see it when I started to work on the solution ...

Moderator's Comments:
Mod Comment dietmar13 is now set to "read only".. abusive behavior and insults of any kind are not permitted in this forums (period)
Login or Register to Ask a Question

Previous Thread | Next Thread

4 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Programming logic help

Hi, I was having a problem regarding this thread: https://www.unix.com/shell-programming-scripting/225221-setting-variable-using-variables-loop.html I have not been able to solve that issue. So i was thinking maybe my logic to solve the overall problem is wrong, so i am looking for a bit of... (2 Replies)
Discussion started by: brunlea
2 Replies

2. Shell Programming and Scripting

logic question

Hi, this is logic question, not sure anyone can answer I have 5 balls, and balance in front of me, this 5 balls look the same, but one of them is heavier, I need to use balance to find the heavier ball my approach is take any 2 balls, 2 balls in each side of balance, if the weight the same,... (2 Replies)
Discussion started by: ccp
2 Replies

3. Shell Programming and Scripting

need a logic for awk programming

Hello Friends, I have a txt file like below //*Init Start Reg(read,12'h42E,16'h0000); Nop(5628.5); //*Init End //*Main Start Reg(read,12'h42E,16'h0000); Nop(5628.5); //*Main End I want to calculate the values between //* Init Start & //* Init End And //*Main Start & //*Main... (5 Replies)
Discussion started by: user_prady
5 Replies

4. Shell Programming and Scripting

Parent/Child Scripting logic Question

I have Parent script that will execute a child script. The child script will take about 2 hours to run and I wanted to somehow build a logic for parent.sh to wait and perform ps -ef|grep cp|grep -v on the child.sh cp process and loop and check return code of child.sh $? as well. Once they are both... (3 Replies)
Discussion started by: jigarlakhani
3 Replies
Login or Register to Ask a Question