Many to many -- mapping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Many to many -- mapping
# 1  
Old 08-29-2018
Many to many -- mapping

INPUT

Code:
13333--TEXT1
14444--TEXT2
13333--TEXT3
12233--TEXT5
14444--TEXT5
12233--TEXT1
12222--TEXT5
13333--TEXT09

what I'm looking for is something using awk arrays with below given output.

Code:
14444--TEXT2,TEXT5
13333--TEXT1,TEXT3,TEXT09
12233--TEXT5,TEXT1
12222--TEXT5

# 2  
Old 08-29-2018
Any attempts on your own?
# 3  
Old 08-29-2018
Here the END part is trying to do a scanning on the array of input being built in the action panel of gawk

Code:
gawk '/pmd/ { 
split($1,IP," "); 
x[IP[2]]++; 
y[n++]=IP[2]"--"$2;} END {
for(i in y){
{
{print y[i]}
}
}
}' file

other approach was

Code:
gawk '/pmd/ {
split($1,IP," "); 
x[IP[2]]++; y[IP[2]"--"$2]++;} END {
for(i in x){
 for(j in y){
if(i ~ y){
print i}
}
}
}' file

# 4  
Old 08-29-2018
Code:
awk -F='--'  '
  {
    a[$1]=($1 in a)?a[$1] "," $2:$2
  }
  END {
     for(i in a)
       print i,a[i]
  }' OFS='--' myFile

# 5  
Old 08-29-2018
Is this correct explanation of that specific line ?

Create array named 'a' indexed with first field with conditional value of : if first field exist in array a , set value to that array member, comma and second field, otherwise set the value to second field.
This will effectivley, append second field if array is defined with index of currently processing line, otherwise, create array member with value of second field.
Code:
...
    a[$1]=($1 in a)?a[$1] "," $2:$2
...


Thanks and regards
Peasant.
# 6  
Old 08-29-2018
it's somewhat close. Let me rewrite this short-circuit
Code:
 a[$1]=($1 in a)?a[$1] "," $2:$2

as this:
Code:
if ($1 in a)
   a[$1]=a[$1] "," $2
else
   a[$1]=$2

Hopefully the long-hand will make it easier to understand...
# 7  
Old 08-30-2018
@vgersh99

Thanks for the idea. it works for me as a standard input is concerned...

the problem is that I'm combining the result into this format in AWK's action panel( or action part) and then doing the many to many mapping ( requirement of this thread ) in the END part.

so my code goes like below:

Code:
gawk 'BEGIN {n=0;}
/pmd/ {
split($1,IP," "); 
x[IP[2]]++; 
y[n++]=IP[2]"--"$2;
} END {for(i in x){ 
for(j in y){
if(k ~ i){print i,j}
}
}
}' input-file.




the trick here is that the part before the END section is producing output like

x is the array containing 13333, 12222, 144444, 155555 -- just for refernce


in the array of
Code:
"y"

with
Code:
"n"

as index starting with 0. now when I try to scan the
Code:
13333--TEXT1
12222--TEXT2
12222--TEXT34
13333--TEXT9
14444--TEXT12
15555--TEXT23
14444--TEXT234
13333--TEXT08
13333--TEXT34

what would be the best way to scan the values of
Code:
x

in
Code:
y

from above given values format and then do a many to many mapping as you have done previously.

------ Post updated at 02:41 PM ------

below is how your code is working for me -- giving required ouput

but I need something in a single awk execution..

Code:
gawk '/pmd/ {split($1,IP," "); 
x[IP[2]]++; 
y[n++]=IP[2]"--"$2;} 
END {
for(j in y){ 
print y[j]
}
}'  inputfile |awk -F'--'  '
  {
    a[$1]=($1 in a)?a[$1] "," $2:$2
  }
  END {
     for(i in a)
       print i,a[i]
  }' OFS='--'

------ Post updated at 06:19 PM ------

i eventuall ended up in managing the requirement-- instead of going into the END part, I simply co-joined the parts in action part.

Code:
gawk '/pmd/ {
split($1,IP," ");
 a[IP[2]]=(IP[2] in a)?a[IP[2]] "<br>" $2:$2
  }
  END {
     for(i in a)
       print i,a[i]
  }' OFS='--'  input file

------ Post updated at 06:20 PM ------

thanks @vgersh99
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Creating unique mapping from multiple mapping

Hello, I do not know if this is the right title to use. I have a large dictionary database which has the following structure: where a b c d e are in English and p q r s t are in a target language., the two separated by the delimiter =. What I am looking for is a perl script which will take... (5 Replies)
Discussion started by: gimley
5 Replies

2. Shell Programming and Scripting

Need Help Mapping Arrays

I have the following arrays with different lengths that I want to map them with the same key. # Week numbers, 8 columns @headers = ("2011-34", "2011-35", "2011-36", "2011-37", "2011-38", "2011-39", "2011-40", "2011-41"); %data = ("2011-34", BCE, "2011-35", YZA, "2011-36",... (5 Replies)
Discussion started by: tqlam
5 Replies

3. UNIX for Dummies Questions & Answers

Re-Mapping Printers.

Hi we have a situation where some printers are on a server that sometimes has to be rebooted. If this happens the Unix boxes we have that are referencing the printers in the vfstab file fail to work even when the print server is brought back up. Does anyone know if it would be possible to put... (0 Replies)
Discussion started by: Hadleyshope
0 Replies

4. Red Hat

Key Mapping

Hi All i am a tad stuck i am trying to map alt gr key so that it acts as a Alt key i tried xmodmap but it went very wrong and it disabled both alt gr and alt key thanks Adam (0 Replies)
Discussion started by: ab52
0 Replies

5. Shell Programming and Scripting

Bit Mapping

Hi All, I am working on the Scenario where i need compare the integer value with other using bit mapping. input file, 1,4,5,4 1,2,4,6 2,3,4,4 like i have many fields. Script : i need to filter hte field one value "1" and field 3 value "4" and Filed 4 i need to do the Bitwise... (5 Replies)
Discussion started by: readycpbala
5 Replies

6. Shell Programming and Scripting

Pattern mapping

Dear Friends, Please help me on this I have file A.txt containing text lines as below grectec; 30 ,50, 60, base_123 ; top09 grectec; 30 ,55, 60, base_123 ; top09 grectec; 10 ,53, 60, base_123 ; top09 grectec; 50 ,57, 60, base_123 ; top09 ... ... another file B.txt containing test... (4 Replies)
Discussion started by: Danish Shakil
4 Replies

7. Shell Programming and Scripting

searching and mapping

Hi, I just started looking into various aspect of unix shell scripting. I am completely new to the world of UNIX. Could any one help me in solving the following requirement. I have a parameter file with some data e.g. sample.param ------------- Andrew=201 Bob=219 Shelly=239... (4 Replies)
Discussion started by: mamatabbsr
4 Replies

8. UNIX for Dummies Questions & Answers

Keyboard Mapping HP-UX

This is truly embarrassing but I have to know the answer anyway :p Can anyone share, how to assign keyboard mapping in hp-ux system? Every time I issue command which contain character "@", it'll go the next line. What I need is how to assign the character "@" to @ as it is supposed to be. ... (2 Replies)
Discussion started by: Olli.Lang
2 Replies

9. UNIX for Advanced & Expert Users

kernel mapping...

> how the sendmsg and recvmsg calls will know which kernel module to use (SCTP, RTP etc.) internally(kernel mapping: how kernel handle socket call) (1 Reply)
Discussion started by: prangin
1 Replies

10. UNIX for Dummies Questions & Answers

LUN mapping

Hi, We recently purchased a 3 tb RAID. In the setup, it created 2 slices, one 2 tb and one 1 tb slice. Right now only the 2 tb slice is visible when booting up. I was told that the second slice needs to be mapped to a lun and a port like the first slice is. Does anyone know how I would go... (1 Reply)
Discussion started by: user23
1 Replies
Login or Register to Ask a Question