Sponsored Content
Full Discussion: Count char, sum and change
Top Forums Shell Programming and Scripting Count char, sum and change Post 302730547 by awil on Tuesday 13th of November 2012 07:43:03 AM
Old 11-13-2012
Thank you very very much, Pamu.

Great. ^^ I never know that shell script can do in everything. So good.

I tried to use your code, it's worked but I fixed a little bit because when I have 'A' > 5 in column and number of 'B' > 5 but more than number of 'A'. This may be incorrect.

So, I changed code in a little bit like this :
Code:
awk 'function decide_num(y) { 	if(X[y,"A"] > X[y,"B"]){p=1}else{p=0} 	if(p == 0)
{sub("AA","2",$y);sub("AB","1",$y);sub("BB","0",$y)}else{sub("BB","2",$y);sub("AB","1",$y);sub("AA","0",$y)}
	 	} FNR==NR{for(i=2;i<=NF;i++){for(j=1;j<=length($i);j++){X[i,substr($i,j,1)]++}};next}
{for(i=2;i<=NF;i++){decide_num(i)}}1' file file

Test input again :
Code:
Sam1 BB BB AA AA BB BB BB AA AA BB BB
Sam2 BB BB AA AA AB AB AB AA AA BB BB
Sam3 BB BB AA AA BB BB BB AA AA BB BB
Sam4 AB AB AB AB AB AB AA AB AA BB AB
Sam5 BB BB AA AA BB BB BB AA AA AB BB
Sam6 BB BB AA AA BB BB BB AA AA BB BB
Sam7 BB BB AA AA AB AB AB AA AA BB BB
Sam8 BB BB AA AA BB BB BB AA AA BB BB
Sam9 AB AB AB AB -- AB AA AB AA BB AB
Sam10 BB BB AA AA BB BB BB AA AA AB BB

Output showed :
Code:
Sam1 0 0 0 0 0 0 0 0 0 0 0
Sam2 0 0 0 0 1 1 1 0 0 0 0
Sam3 0 0 0 0 0 0 0 0 0 0 0
Sam4 1 1 1 1 1 1 2 1 0 0 1
Sam5 0 0 0 0 0 0 0 0 0 1 0
Sam6 0 0 0 0 0 0 0 0 0 0 0
Sam7 0 0 0 0 1 1 1 0 0 0 0
Sam8 0 0 0 0 0 0 0 0 0 0 0
Sam9 1 1 1 1 -- 1 2 1 0 0 1
Sam10 0 0 0 0 0 0 0 0 0 1 0

Thank you very very much. Pamu Smilie

Last edited by Scott; 11-13-2012 at 08:47 AM.. Reason: Please use code tags and format your code. Thanks.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do I count # of char. in a word?

I havent done shell scripting in quite some time. I want to know how to count the number of characters in a word, specifically a parameter. Example: myscript hello I want "myscript" to return the number of charcaters in the parameter "hello". Any ideas? (9 Replies)
Discussion started by: xadamz23
9 Replies

2. UNIX for Dummies Questions & Answers

how to count pariticular char in a location in a file

Hi .. I am having file say at 53rd position it will be as 0's or 1's .. How can i count the total number of 1's and 0's in the files at the 53 rd location. Thanks, Arun (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

3. Shell Programming and Scripting

count of sum of row60 files same patter

i have 60 files that have same pattern tgt_abc1.dat tgt_abc2.dat i want to calculate sum of row count of these files and sum these up and populate that in third file. how can i do that?? example tgt_abc1.dat 2000 tgt_abc2.dat 4000 so want to populate in file xyz.dat 6000 (1 Reply)
Discussion started by: er_zeeshan05
1 Replies

4. Shell Programming and Scripting

Getting Sum, Count and Distinct Count of a file

Hi all this is a UNIX question. I have a large flat file with millions of records. col1|col2|col3 1|a|b 2|c|d 3|e|f 3|g|h footer**** I am supposed to calculate the sum of col1 1+2+3+3=9, count of col1 1,2,3,3=4, and distinct count of col1 1,2,3=c3 I would like it if you avoid... (4 Replies)
Discussion started by: singhabhijit
4 Replies

5. UNIX for Dummies Questions & Answers

how to count number of rows and sum of column using awk

Hi All, I have the following input which i want to process using AWK. Rows,NC,amount 1,1202,0.192387 2,1201,0.111111 3,1201,0.123456 i want the following output count of rows = 3 ,sum of amount = 0.426954 Many thanks (2 Replies)
Discussion started by: pistachio
2 Replies

6. Shell Programming and Scripting

awk count characters, sum, and divide by another column

Hi All, I am another biologist attempting to parse a large txt file containing several million lines like: tucosnp 56762 T Y 228 228 60 23 .CcCcc,,..c.c,cc,,.C... What I need to do is get the frequency of periods (.) plus commas (,) in column 9, and populate this number into another... (1 Reply)
Discussion started by: peromhc
1 Replies

7. Shell Programming and Scripting

sum divided by count

Dear friends, I'm stuck with the task below, I would be thankful for all your replies. INPUT : Date Price Volume 20110601 73052811.61 2845833 20110602 61489062.96 9909230 20110603 72790724.65 1108927 20110606 48299507.20 7435881 20110607 ... (5 Replies)
Discussion started by: hernand
5 Replies

8. Shell Programming and Scripting

awk and count sum ?

I have a input.txt file which have 3 fields separate by a comma place, os and timediff in seconds tampa,win7, 2575 tampa,win7, 157619 tampa,win7, 3352 dallas,vista,604799 greenbay,winxp, 14400 greenbay,win7 , 518400 san jose,winxp, 228121 san jose,winxp, 70853 san jose,winxp, 193514... (5 Replies)
Discussion started by: sabercats
5 Replies

9. Shell Programming and Scripting

How count number of char?

hello how can i cont number of char with loop coomand? i dont want to use wc or other special command the script should check all word's char. one by one also a counter can handle the number As noted in other threads started today. This is not the correct forum for homework assignments. ... (2 Replies)
Discussion started by: nimafire
2 Replies

10. Shell Programming and Scripting

Script Shell: Count The sum of numbers in a file

Hi all; Here is my file: V1.3=4 V1.4=5 V1.1=3 V1.2=6 V1.3=6 Please, can you help me to write a script shell that counts the sum of values in my file (4+5+3+6+6) ? Thank you so much for help. Kind regards. (3 Replies)
Discussion started by: chercheur111
3 Replies
sum(n)							     Cyclic Redundancy Checks							    sum(n)

__________________________________________________________________________________________________________________________________________________

NAME
sum - Calculate a sum(1) compatible checksum SYNOPSIS
package require Tcl 8.2 package require sum ?1.1.0? ::crc::sum ?-bsd | -sysv? ?-format fmt? ?-chunksize size? [ -filename file | -channel chan | string ] _________________________________________________________________ DESCRIPTION
This package provides a Tcl-only implementation of the sum(1) command which calculates a 16 bit checksum value from the input data. The BSD sum algorithm is used by default but the SysV algorithm is also available. COMMANDS
::crc::sum ?-bsd | -sysv? ?-format fmt? ?-chunksize size? [ -filename file | -channel chan | string ] The command takes string data or a file name or a channel and returns a checksum value calculated using the sum(1) algorithm. The result is formatted using the format(n) specifier provided or as an unsigned integer (%u) by default. OPTIONS
-sysv The SysV algorithm is fairly naive. The byte values are summed and any overflow is discarded. The lowest 16 bits are returned as the checksum. Input with the same content but different ordering will give the same result. -bsd This algorithm is similar to the SysV version but includes a bit rotation step which provides a dependency on the order of the data values. -filename name Return a checksum for the file contents instead of for parameter data. -channel chan Return a checksum for the contents of the specified channel. The channel must be open for reading and should be configured for binary translation. The channel will no be closed on completion. -chunksize size Set the block size used when reading data from either files or channels. This value defaults to 4096. -format string Return the checksum using an alternative format template. EXAMPLES
% crc::sum "Hello, World!" 37287 % crc::sum -format 0x%X "Hello, World!" 0x91A7 % crc::sum -file sum.tcl 13392 AUTHORS
Pat Thoyts BUGS, IDEAS, FEEDBACK This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category crc of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation. SEE ALSO
cksum(n), crc32(n), sum(1) KEYWORDS
checksum, cksum, crc, crc32, cyclic redundancy check, data integrity, security, sum CATEGORY
Hashes, checksums, and encryption COPYRIGHT
Copyright (c) 2002, Pat Thoyts <patthoyts@users.sourceforge.net> crc 1.1.0 sum(n)
All times are GMT -4. The time now is 11:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy