two-column data to matrix in AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting two-column data to matrix in AWK
# 8  
Old 04-06-2011
Had extra time to play.

One line of awk versus 52 lines of shell script:
Code:
#!/usr/bin/ksh
typeset -i mTot=0
typeset -i mInd
mPrev1="First"
rm -f F_Summ
while read mCol1 mCol2; do
  if [[ "${mCol1}" != "${mPrev1}" || "${mCol2}" != "${mPrev2}" ]]; then
    if [[ "${mPrev1}" != "First" ]]; then
      echo ${mPrev1} ${mPrev2} ${mTot} >> F_Summ
      mTot=0
    fi
  fi
  mPrev1=${mCol1}
  mPrev2=${mCol2}
  mTot=${mTot}+1
done < F_Inp_File
if [[ "${mPrev1}" != "First" ]]; then
  echo ${mPrev1} ${mPrev2} ${mTot} >> F_Summ
fi

sed 's/.* \(.*\) .*/\1/' B2 | sort | uniq > F_Uniq_col2
set -A mACol2 `cat F_Uniq_col2`
echo "  "${mACol2[*]}

mPrev1="First"
while read mCol1 mCol2 mTotal; do
  if [[ "${mCol1}" != "${mPrev1}" ]]; then
    while [[ ${mInd} -lt ${#mACol2[*]} ]]; do
      mAll=${mAll}" 0"
      mInd=${mInd}+1
    done
    if [[ "${mPrev1}" != "First" ]]; then
      echo ${mPrev1} ${mAll}
    fi
    mInd=0
    mAll=""
  fi
  while [[ "${mCol2}" != "${mACol2[$mInd]}" ]]; do
    mAll=${mAll}" 0"
    mInd=${mInd}+1
  done
  mAll=${mAll}" "${mTotal}
  mPrev1=${mCol1}
  mInd=${mInd}+1
done < F_Summ
if [[ "${mPrev1}" != "First" ]]; then
  while [[ ${mInd} -lt ${#mACol2[*]} ]]; do
    mAll=${mAll}" 0"
    mInd=${mInd}+1
  done
  echo ${mPrev1} ${mAll}
fi

This User Gave Thanks to Shell_Life For This Post:
# 9  
Old 04-07-2011
awk may be ok also

awk '{
_[$1"."$2]=1
v[$1]=1
h[$2]=1
}
END{
printf " "
for(a in h)
printf a" "
printf "\n"
for(i in v)
{
printf i
for(j in h)
{
tmp=sprintf("%s.%s",i,j)
if(exists _[tmp])
printf " "1
else
printf " "0
}
printf "\n"
}
}' yourfile
# 10  
Old 04-07-2011
Quote:
Originally Posted by Shell_Life
Had extra time to play.
One line of awk versus 52 lines of shell script:
you can put all your 52 shell statements into one line as well. If the awk statement is properly indented, its not one line anymore. Smilie

---------- Post updated at 03:56 AM ---------- Previous update was at 03:27 AM ----------

Ruby(1.9+)
Code:
#!/usr/bin/env ruby 
h={}; assoc=[]
File.open("file").each do |line|
    a,b = line.chomp.split
    assoc << b
    (h[a] ||= []) << b
end
assoc.uniq!
puts " "*20 + assoc.join("%20s" % " ")
h.each do |x,y|
    print "#{x} "
    assoc.each {|w| print "%20s " % (y.include?(w) ? "1 "  : "0 ") }
    puts
end

Code:
$ ruby matrix.rb
                    A                    B                    C
X                   1                    1                    0
Y                   1                    0                    1
Z                   0                    1                    1

# 11  
Old 04-07-2011
Quote:
Originally Posted by kurumi
you can put all your 52 shell statements into one line as well. If the awk statement is properly indented, its not one line anymore. Smilie
Kurumi,
You are right - if indented properly, the awk solution will be more than one line.

As in every case, there are always several different ways to solve the same problem.

When choosing a solution, one should keep in mind the following:

1) Easy to understand.
Can other programmers follow it?

2) Easy to maintain.
Can other programmers easily change it?

3) Expertise.
How many other programmers in the group know the language?
If the expert leaves, who will maintain it?

Cheers.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get row data printed in column using awk?

Hi team, I have below sample file. $ cat sample dn: MSISDN=400512345677,dc=msisdn,ou=NPSD,serv=CSPS,ou=servCommonData,dc=stc structuralObjectClass: NphData objectClass: NphData objectClass: MSISDN entryDS: 0 nodeId: 35 createTimestamp: 20170216121047Z modifyTimestamp: 20170216121047Z... (3 Replies)
Discussion started by: shanul karim
3 Replies

2. Shell Programming and Scripting

Transpose matrix based on second column using awk

Hi, Is it possible to transpose the matrix like this using awk ? Many thanks in advance Input abc Name_1 0 abc Name_2 1 abc Name_3 2 abc Name_4 0.4 def Name_1 0 def Name_2 9 def Name_3 78 def Name_4 1 Output abc def Name_1 0 ... (4 Replies)
Discussion started by: quincyjones
4 Replies

3. Shell Programming and Scripting

How order a data matrix using awk?

is it possible to order the following row clusters from ascending to descending. thanx in advance input 1 2 4 0 1 2 4 0 3 3 3 3 1 5 1 0 1 5 1 0 6 0 0 0 5 1 1 1... (4 Replies)
Discussion started by: quincyjones
4 Replies

4. Shell Programming and Scripting

column to matrix

Hello All, I need your help in the following problem. I have a matrix of 500 columns and 1000 rows and in each cell, it is having a value range from 0 to 9. I would like to convert each column in to a matrix, according to the value in each cell (ie) 0 to 9. For each column, I need a matrix... (5 Replies)
Discussion started by: Fredrick
5 Replies

5. Shell Programming and Scripting

3 column .csv --> correlation matrix; awk, perl?

Greetings, salutations. I have a 3 column csv file with ~13 million rows and I would like to generate a correlation matrix. Interestingly, you all previously provided a solution to the inverse of this problem. Thread title: "awk? adjacency matrix to adjacency list / correlation matrix to list"... (6 Replies)
Discussion started by: R3353
6 Replies

6. Shell Programming and Scripting

Printing another column using awk and input data

Hi, I have data of the following type, chr1 234 678 39 852 638 abcd 7895 chr1 526 326 33 887 965 kilj 5849 Now, I would like to have something like this chr1 234 678 39 852 638 abcd 7895 <a href="http://unix.com/thread=chr1:234-678">Link</a> chr1 526 326 33 887 965 kilj 5849 <a... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

7. Shell Programming and Scripting

convert data into matrix- awk

is it possible to count the number of keys based on state and cell and output it as a simple matrix. Ex: cell1-state1 has 2 keys cell3-state1 has 4 keys. Note: Insert 0 if no data available. input key states cell key1 state1 cell1 key1 state2 cell1 key1 ... (21 Replies)
Discussion started by: quincyjones
21 Replies

8. Ubuntu

How to convert full data matrix to linearised left data matrix?

Hi all, Is there a way to convert full data matrix to linearised left data matrix? e.g full data matrix Bh1 Bh2 Bh3 Bh4 Bh5 Bh6 Bh7 Bh1 0 0.241058 0.236129 0.244397 0.237479 0.240767 0.245245 Bh2 0.241058 0 0.240594 0.241931 0.241975 ... (8 Replies)
Discussion started by: evoll
8 Replies

9. Emergency UNIX and Linux Support

AWK - check column data

Hi, I have a data in a file . Infile: 1 e 1.2 1.6 5 f 2.3 3.6 3 g 1.2 2.6 6 i 2.3 3.6 8 o 1.2 3.6 output: 1 e 1.2 1.6 5 f 2.3 3.6 3 g 1.1 2.6 6 i 2.2 3.5 8 o 1.0 3.4 (17 Replies)
Discussion started by: vasanth.vadalur
17 Replies

10. Shell Programming and Scripting

extract data from a data matrix with filter criteria

Here is what old matrix look like, IDs X1 X2 Y1 Y2 10914061 -0.364613333 -0.362922333 0.001691 -0.450094667 10855062 0.845956333 0.860396667 0.014440333 1.483899333... (7 Replies)
Discussion started by: ssshen
7 Replies
Login or Register to Ask a Question