count the number of instances in 2 columns using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting count the number of instances in 2 columns using awk
# 1  
Old 09-28-2012
count the number of instances in 2 columns using awk

Input

Code:
A.1  Q.1
A.1  Q.2
A.1  Q.3
A.2  Q.4

Explanation:
Quote:
print all unique keys in a order

A.1
Q.1
Q.2
Q.3
A.2
Q.4

and number them like this

A.1 (s1) (t0)
Q.1 (s2) (t1)
Q.2 (s3) (t2)
Q.3 (s4) (t3)
A.2 (s5) (t4)
Q.4 (s6) (t5)

now output the connected pairs like A.1 is connected to Q.1 and Q.2 and Q.3, Therefore
s1 t1
s1 t2
s1 t3
Final Output
Code:
A.1  Q.1  s1  t1
A.1  Q.2  s1  t2
A.1  Q.3  s1  t3
A.2  Q.4  s5  t5

---------- Post updated 09-28-12 at 03:38 AM ---------- Previous update was 09-27-12 at 09:10 AM ----------

Hi Guys, I was able to do until here but still doesn't know how to connect these. Could you please help me. Thanx in advance.

Code:
tr ' ' '\n' <input |awk '/./'  |awk '!a[$1]++' | awk '{ print $0"\t""s"NR"\t""t"NR-1}'

temp output
Code:
A.1    s1    t0
Q.1    s2    t1
Q.2    s3    t2
Q.3    s4    t3
A.2    s5    t4
Q.4    s6    t5


Last edited by quincyjones; 09-28-2012 at 06:11 AM..
# 2  
Old 09-28-2012
Hi

Code:
$ awk '{a[$1]=a[$1] "\n" $2;next}END{for(i in a){print i, a[i];}}' file | awk '{print $0 "\t" "s"i++ "\t" "t"j++ }' i=1 j=0
A.1     s1      t0
Q.1     s2      t1
Q.2     s3      t2
Q.3     s4      t3
A.2     s5      t4
Q.4     s6      t5

Guru
# 3  
Old 09-28-2012
try this..

i know this is too large and confusing... need some refinement.. i m trying...Smilie
Code:
$tr '  ' '\n' < file  | awk '/./ { if($0 ~ /^A/ ){if(!x[$0]++){a++;print $0,"s"a}else{print $0,"s"a}}else if($0 ~ /^Q/ ){if(!x[$0]++){b++;print $0,"t"(b)}else{print $0,"t"(b)}}}' | xargs -n 4 | awk '{ print $1,$3,$2,$4}'

A.1 Q.1 s1 t1
A.1 Q.2 s1 t2
A.1 Q.3 s1 t3
A.2 Q.4 s2 t4

# 4  
Old 09-28-2012
@Guru: I was trying to get the "Final Output" not the "Temp Output". Sorry for the confusion.
# 5  
Old 09-28-2012
try this..
Code:
awk '{ if(!x[$1]++){if(b){a+=b}{a++};p="s"a}else{p="s"a}; if(!x[$2]++){b++;q="t"b}else{q="t"b}}{print $0,p,q}' file

hope this helps you..
# 6  
Old 09-28-2012
@Pamu: t should start with t0 not t1. therefor you got the last line in the output wrong.

I made an ugly script but it did the job. However I prefer an elegant code. Thanx

input
Code:
A.1  Q.1
A.1  Q.2
A.1  Q.3
A.2  Q.4

output
Code:
A.1    Q.1    s1    t1
A.1    Q.2    s1    t2
A.1    Q.3    s1    t3
A.2    Q.4    s5    t5

Code:
awk '{print $1"\t"$2}' $1 | sort >> filex;
awk '{print $2"\t"$1}' $1 | sort >> filey;
tr ' ' '\n' <filex |awk '/./'  |awk '!a[$1]++' | awk '{ print $0"\t""s"NR}' | sort  >> file1;
tr ' ' '\n' <filex |awk '/./'  |awk '!a[$1]++' | awk '{ print $0"\t""t"NR-1}' | sort  >> file2;
join file1 filex |awk '{print $1":"$3"\t"$2}' | sort >> file3 ;
join file2 filey |awk '{print $3":"$1"\t"$2}' | sort > file4  ; 
join file3 file4 | tr ':' '\t' |awk '{print $1"\t"$2"\t"$3"\t"$4}' >> final_output ;
rm file* ;

# 7  
Old 09-28-2012
Code:
awk 'FNR==NR{for(i=1;i<=NF;i++) if(!($i in s)){s[$i]=++y;t[$i]=z++};next}
{print $1,$2,"s" s[$1],"t" t[$2]}' file file


Last edited by elixir_sinari; 09-28-2012 at 06:55 AM..
This User Gave Thanks to elixir_sinari For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count The Number Of Delimiters using awk or better

What to know the way to count the number of delimiters in each record by ignoring the escape delimiters. Sample Data: 12345678|ABN\|XYZ MED CHEM PTY. LTD.|C||100.00|22|AB"C\|Corp|"XYZ|CDEF"| I'm using awk -F'|' '{ print NF-1 }' command to find the number of delimiters. this command... (8 Replies)
Discussion started by: BrahmaNaiduA
8 Replies

2. Programming

awk to count occurrence of strings and loop for multiple columns

Hi all, If i would like to process a file input as below: col1 col2 col3 ...col100 1 A C E A ... 3 D E G A 5 T T A A 6 D C A G how can i perform a for loop to count the occurences of letters in each column? (just like uniq -c ) in every column. on top of that, i would also like... (8 Replies)
Discussion started by: iling14
8 Replies

3. Shell Programming and Scripting

How to count a number using awk?

hi, does any one know how to compare a file1 with many files and have to count a number (i.e. 1) like the following ..Thanx in advance input file1 file2 file3 filen 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 output instances in file1 only - 2 instances in... (3 Replies)
Discussion started by: quincyjones
3 Replies

4. Shell Programming and Scripting

Count number of occurences using awk

Hi Guys, I have 2 files like below file1 xx yy file2 b yy b2 xx c1 yy xx yy Now I want an idea which can count occurences of text from file1 and file2 so outbout would be kind of (9 Replies)
Discussion started by: prashant2507198
9 Replies

5. Shell Programming and Scripting

Help with Unix and Awk to count number of occurrences

Hi, I have a file (movies.sh), this file contains list of movies such as I want to redirect the movies from movies.sh to file_to_process to allow me process the file with out losing anything. I have tried Movies.sh >> file_to_process But I want to add the row number to the data... (2 Replies)
Discussion started by: INHF
2 Replies

6. Shell Programming and Scripting

count and number instances of a character in sed or awk

I currently use LaTeX together with a sed script to set cloze test papers for my students. I currently pepend and equals sign to the front of the words I want to leave out in the finished test, =perpendicular, for example. I am able to number the blanks using a variable in LaTeX. I would like to... (8 Replies)
Discussion started by: maouinin
8 Replies

7. Shell Programming and Scripting

Awk - Count instances of a number in col1 and put results in a col2 (new) of diff file

I have 2 files as follows: filename1: : 6742 /welcome/mundial98_ahf1_404.htm 1020 6743 /welcome/mundial98_ahf1_404.htm 2224 6744 /welcome/mundial_ef1_404.htm 21678 6745 /welcome/mundial_if_404.htm 4236 6746 /welcome/mundial_lf1_404.htm 21678 filename2: 6746 894694763 1... (2 Replies)
Discussion started by: jontjioe
2 Replies

8. UNIX for Dummies Questions & Answers

count number of fields not using SED or AWK

hi forums i need help with a little problem i am having. i need to count the number of fields that are in a saved variable so i can use that number to make a different function work properly. is there a way of doing this without using SED/AWK? anything would be greatly appreciated (4 Replies)
Discussion started by: strasner
4 Replies

9. Shell Programming and Scripting

simple awk question to count columns

hi all here is a very simple question.. i want to count the number of columns using awk..my file looks like this: 1,2 1,2 1,2 1,2 1,2 1,2 1,2 1,2 i want to count number of columns and i so far i have: awk 'BEGIN {IFS=","} END {print NF}' data > data1 i am getting 1... (3 Replies)
Discussion started by: npatwardhan
3 Replies

10. Shell Programming and Scripting

count number of nonempty columns in row

Hi, Suppose i have a inputfile in csv format. How to use awk to count 'the number of nonempty columns in each row' minus one, and add the value as a new column in the end For cosmetic reason, it's even better to include a descriptive label for the last column in the first row. for... (2 Replies)
Discussion started by: grossgermany
2 Replies
Login or Register to Ask a Question