awk to count occurrence of strings and loop for multiple columns


 
Thread Tools Search this Thread
Top Forums Programming awk to count occurrence of strings and loop for multiple columns
# 1  
Old 09-26-2014
awk to count occurrence of strings and loop for multiple columns

Hi all,

If i would like to process a file input as below:

Code:
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 to print out col1 as concatenate form. the expected output would be:

Code:
col2 count col1 col2 count col1 col3 count col1 ...col100 count col1
A 1 1 C 2 1,6
D 2 3,6 E 1 3
T 1 5 T 1 5

Appreciate if you could help me resolve this problem...urgent!!

Thanks

Last edited by Don Cragun; 09-26-2014 at 04:49 PM.. Reason: Add CODE tags.
# 2  
Old 09-26-2014
What OS are you using? (Show output from uname -a.)

What is the value of LINE_MAX on your system? (Show output from getconf LINE_MAX.)

The obvious way to do things like this is with awk, but the standards only define the behavior of awk on text files. On many systems, a text file cannot have any lines longer than 2048 bytes and if you have 1100 fields in your input files, you have crossed that threshold.

How many lines are in your input file? What is the total size of your input file?

The header for your desired output file:
Code:
col2 count col1 col2 count col1 col3 count col1 ...col100 count col1

shows the 1st three output fields being duplicated but if I understand what you're trying to do, shouldn't the header be?:
Code:
col2 count col1 col3 count col1 ...col100 count col1

# 3  
Old 09-27-2014
Apologize for my typo error

Hi Don,

Thanks for your response. I'm currently using redhat. The line max is about 100k lines, while the number of fields are 100.

Yup, my desired output shall be what you have corrected. I manage to do the count for column by column, but wonder how could i perform a for loop one shot for 100 columns.Smilie

Smilie


Quote:
Originally Posted by Don Cragun
What OS are you using? (Show output from uname -a.)

What is the value of LINE_MAX on your system? (Show output from getconf LINE_MAX.)

The obvious way to do things like this is with awk, but the standards only define the behavior of awk on text files. On many systems, a text file cannot have any lines longer than 2048 bytes and if you have 1100 fields in your input files, you have crossed that threshold.

How many lines are in your input file? What is the total size of your input file?

The header for your desired output file:
Code:
col2 count col1 col2 count col1 col3 count col1 ...col100 count col1

shows the 1st three output fields being duplicated but if I understand what you're trying to do, shouldn't the header be?:
Code:
col2 count col1 col3 count col1 ...col100 count col1

# 4  
Old 09-27-2014
Quote:
Originally Posted by iling14
Hi Don,

Thanks for your response. I'm currently using redhat. The line max is about 100k lines, while the number of fields are 100.

Yup, my desired output shall be what you have corrected. I manage to do the count for column by column, but wonder how could i perform a for loop one shot for 100 columns.Smilie

Smilie
You didn't answer most of my questions. But, now that I know that you're using a Linux system, I know that your version of awk will handle pretty much unlimited line lengths.

I'm still trying to get a feel for what the input and output data is going to look like. I'm assuming that the 100k number you gave is the number of lines in your input file (not the number of bytes in the longest line in your input file). Assuming that the numbers in the 1st column of your input are unique and that there are 6 distinct values in the other columns (A, G, C, T, D, and E) that means that we are converting 100k input rows with a maximum line length of about 210 bytes each into six output data lines (plus one header line) with a maximum line length approaching (3 * 99 spaces between fields +1 * 99 single letters for the col2 through col100 data + 99 * (6 digits + 1 comma) * 100k col1 values + 99 * 5 digits for the count values) 69.3 million bytes and an average line length approaching 11.5 million bytes.

Am I in the right ballpark here, or are my assumptions off? If my assumptions are off, where am I guessing wrong? Are there values other than A, G, C, T, D, and E that will appear in col2 through col100 in the input file?

Once you have created this file, do you have something that is going to be able to use this data?
# 5  
Old 09-27-2014
Hi Don,

I presume that in linux OS, awk has not limitations of the file size? And i've no clue about bytes calculations Smilie

Your assumptions to my problems are pretty accurate except that the values in each columns are not fixed, in other words, it might have other letters or combinations of 2 letters.

I would need to use this kind of program in my other analysis works. (i'm working on quantitative genetics, dealing with DNA data).
# 6  
Old 09-27-2014
Not sure if this is entirely what you are looking for, but try:
Code:
awk '
  FNR==1{
    next
  }
  { 
    for(i=2; i<=NF; i++) {
      if(NR==FNR){
        A[$i,i]=(($i,i) in A?A[$i,i]",":x) $1
        C[$i,i]++
      } 
      else {
        $i=$i FS C[$i,i] FS A[$i,i]
      }
    }
  }
  NR>FNR
' file file

Note: The input file is specified twice


Output:
Code:
1 A 1 1 C 2 1,6 E 1 1 A 3 1,3,5
3 D 2 3,6 E 1 3 G 1 3 A 3 1,3,5
5 T 1 5 T 1 5 A 2 5,6 A 3 1,3,5
6 D 2 3,6 C 2 1,6 A 2 5,6 G 1 6


Last edited by Scrutinizer; 09-27-2014 at 09:55 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 09-28-2014
Quote:
Originally Posted by Scrutinizer
Not sure if this is entirely what you are looking for, but try:
Code:
awk '
  FNR==1{
    next
  }
  { 
    for(i=2; i<=NF; i++) {
      if(NR==FNR){
        A[$i,i]=(($i,i) in A?A[$i,i]",":x) $1
        C[$i,i]++
      } 
      else {
        $i=$i FS C[$i,i] FS A[$i,i]
      }
    }
  }
  NR>FNR
' file file

Note: The input file is specified twice


Output:
Code:
1 A 1 1 C 2 1,6 E 1 1 A 3 1,3,5
3 D 2 3,6 E 1 3 G 1 3 A 3 1,3,5
5 T 1 5 T 1 5 A 2 5,6 A 3 1,3,5
6 D 2 3,6 C 2 1,6 A 2 5,6 G 1 6


Thanks a lot scutinizer!

The program works fine, but i jus realize tht the number will appear many times because the output will have same NR as input. (not like uniq -c) anymore.

Btw, we dont have to specify print using this program?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Count multiple columns and print original file

Hello, I have two tab files with headers File1: with 4 columns header1 header2 header3 header4 44 a bb 1 57 c ab 4 64 d d 5 File2: with 26 columns header1.. header5 header6 header7 ... header 22...header26 id1 44 a bb id2 57 ... (6 Replies)
Discussion started by: nans
6 Replies

2. UNIX for Dummies Questions & Answers

Count occurrence of string (based on type) in a column using awk

Hello, I have a table that looks like what is shown below: AA BB CC XY PQ RS AA BB CC XY RS I would like the total counts depending on the set they belong to: if search pattern is in {AA, BB, CC} --> count them as Type1 | wc -l (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Awk: count occurrence of each character for every field

Hi, let's say an input looks like: A|C|C|D A|C|I|E A|B|I|C A|T|I|B as the title of the thread explains, I am trying to get something like: 1|A=4 2|C=2|B=1|T=1 3|I=3|C=1 4|D=1|E=1|C=1|B=1 i.e. a count of every character in each field (first column of output) independently, sorted... (4 Replies)
Discussion started by: beca123456
4 Replies

4. Shell Programming and Scripting

Count occurrence of string in a column using awk

Hi, I want to count the occurrences of strings in a column and display as in example below: Input: get1 345 789 098 get2 567 982 090 fet4 777 610 632 get1 800 544 230 get1 600 788 451 get2 892 321 243 get1 673 111 235 fet3 789 220 278 fet4 768 222 341 output: 4 get1 345 789... (7 Replies)
Discussion started by: aydj
7 Replies

5. UNIX for Dummies Questions & Answers

Split a column into multiple columns at certain character count

Hey everyone, I have an issue with a client that is passing me a list of values in one column, and occasionally the combination of all the values results in more than an 255 character string. My DB has a 255 character limit, so I am looking to take the column (comma delimited file), and if it... (1 Reply)
Discussion started by: perekl
1 Replies

6. Shell Programming and Scripting

How to count the number of occurrence of words from multiple files?

File 1 aaa bbb ccc File 2 aaa xxx zzz bbb File 3 aaa bbb xxx Output: (4 Replies)
Discussion started by: Misa-Misa
4 Replies

7. Shell Programming and Scripting

Count no of occurrence of the strings based on column value

Can anyone help me to count number of occurrence of the strings based on column value. Say i have 300 files with 1000 record length from which i need to count the number of occurrence string which is existing from 213 to 219. Some may be unique and some may be repeated. (8 Replies)
Discussion started by: zooby
8 Replies

8. Shell Programming and Scripting

Copying of multiple columns of one table to another by mapping with particular strings.

Hi, I would like to copy some columns from a particular file by mapping with the string names. i am using the .csv file format. my one file consist of 100 of columns but i want only particular 4 columns such as ( First_name, Middle_name,Last_name & Stlc). but they are listed in many files... (15 Replies)
Discussion started by: dsh007
15 Replies

9. Shell Programming and Scripting

Count occurance of multiple strings using grep command

How to grep multiple string occurance in input file using single grep command? I have below input file with many IDP, RRBE messages. Out put should have count of each messages. I have used below command but it is not working grep -cH "(sent IDP Request)(Recv RRBCSM)" *.txt ... (5 Replies)
Discussion started by: sushmab82
5 Replies

10. Linux

To find multiple strings count in a file

I need to find the line count of multiple strings in a particular file. The strings are as follows: bmgcc bmgccftp bsmsftp bulkftp cctuneftp crbtftp crmpos cso gujhr I am doing manual grep for each of the string to find the line count. The command i am using right now is: grep mark... (3 Replies)
Discussion started by: salaathi
3 Replies
Login or Register to Ask a Question