Sponsored Content
Top Forums Shell Programming and Scripting Count number of unique values in each column of array Post 303011172 by Yoda on Sunday 14th of January 2018 02:10:21 PM
Old 01-14-2018
Here is an awk approach:-
Code:
awk '
        BEGIN {
                n = split ( "A B C D", T )
        }
        {
                for ( i = 1; i <= NF; i++ )
                        R[i FS $i] += 1
        }
        END {
                printf "VAL\t"
                for ( i = 1; i <= NF; i++ )
                        printf "COL%d\t", i
                printf "\n"

                for ( j = 1; j <= n; j++ )
                {
                        printf "%c\t", T[j]
                        for ( i = 1; i <= NF; i++ )
                                printf "%d\t", R[i FS T[j]]
                        printf "\n"
                }
        }
' file

This User Gave Thanks to Yoda For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Not able to read unique values in array

Hi Friends, I am having some trouble reading into an array. Basically, I am trying to grep for a pattern and extract it's value and store the same into an array. For eg., if my input is: <L:RECORD>name=faisel farooq,age=21, company=TCS,project=BT</L:RECORD> <L:RECORD>name=abc... (1 Reply)
Discussion started by: faiz1985
1 Replies

2. Shell Programming and Scripting

print unique values of a column and sum up the corresponding values in next column

Hi All, I have a file which is having 3 columns as (string string integer) a b 1 x y 2 p k 5 y y 4 ..... ..... Question: I want get the unique value of column 2 in a sorted way(on column 2) and the sum of the 3rd column of the corresponding rows. e.g the above file should return the... (6 Replies)
Discussion started by: amigarus
6 Replies

3. UNIX for Dummies Questions & Answers

count number of rows based on other column values

Could anybody help with this? I have input below ..... david,39 david,39 emelie,40 clarissa,22 bob,42 bob,42 tim,32 bob,39 david,38 emelie,47 what i want to do is count how many names there are with different ages, so output would be like this .... david,2 emelie,2 clarissa,1... (3 Replies)
Discussion started by: itsme999
3 Replies

4. Shell Programming and Scripting

How to count Unique Values from a file.

Hi I have the following info in a file - <Cell id="25D"/> <Cell id="26A"/> <Cell id="26B"/> <Cell id="26C"/> <Cell id="27A"/> <Cell id="27B"/> <Cell id="27C"/> <Cell id="28A"/> I would like to know how would you go about counting all... (4 Replies)
Discussion started by: Prega
4 Replies

5. UNIX for Dummies Questions & Answers

count number of distinct values in each column with awk

Hi ! input: A|B|C|D A|F|C|E A|B|I|C A|T|I|B As the title of the thread says, I would need to get: 1|3|2|4 I tried different variants of this command, but I don't manage to obtain what I need: gawk 'BEGIN{FS=OFS="|"}{for(i=1; i<=NF; i++) a++} END {for (b in a) print b}' input ... (2 Replies)
Discussion started by: beca123456
2 Replies

6. Shell Programming and Scripting

Count frequency of unique values in specific column

Hi, I have tab-deliminated data similar to the following: dot is-big 2 dot is-round 3 dot is-gray 4 cat is-big 3 hot in-summer 5 I want to count the frequency of each individual "unique" value in the 1st column. Thus, the desired output would be as follows: dot 3 cat 1 hot 1 is... (5 Replies)
Discussion started by: owwow14
5 Replies

7. Shell Programming and Scripting

Count occurrence of column one unique value having unique second column value

Hello Team, I need your help on the following: My input file a.txt is as below: 3330690|373846|108471 3330690|373846|108471 0640829|459725|100001 0640829|459725|100001 3330690|373847|108471 Here row 1 and row 2 of column 1 are identical but corresponding column 2 value are... (4 Replies)
Discussion started by: angshuman
4 Replies

8. Shell Programming and Scripting

Print count of unique values

Hello experts, I am converting a number into its binary output as : read n echo "obase=2;$n" | bc I wish to count the maximum continuous occurrences of the digit 1. Example : 1. The binary equivalent of 5 = 101. Hence the output must be 1. 2. The binary... (3 Replies)
Discussion started by: H squared
3 Replies

9. UNIX for Beginners Questions & Answers

Awk: count unique element of array

Hi, tab-separated input: blabla_1 A,B,C,C blabla_2 A,E,G blabla_3 R,Q,A,B,C,R,Q output: blabla_1 3 blabla_2 3 blabla_3 5 After splitting $2 in an array, I am trying to store the number of unique elements in a variable, but have some difficulties resetting the variable to 0 before... (6 Replies)
Discussion started by: beca123456
6 Replies

10. UNIX for Beginners Questions & Answers

Count unique column

Hello, I am trying to count unique rows in my file based on 4 columns (2-5) and to output its frequency in a sixth column. My file is tab delimited My input file looks like this: Colum1 Colum2 Colum3 Colum4 Coulmn5 1.1 100 100 a b 1.1 100 100 a c 1.2 200 205 a d 1.3 300 301 a y 1.3 300... (6 Replies)
Discussion started by: nans
6 Replies
PDOSTATEMENT.FETCHALL(3)						 1						  PDOSTATEMENT.FETCHALL(3)

PDOStatement::fetchAll - Returns an array containing all of the result set rows

SYNOPSIS
public array PDOStatement::fetchAll ([int $fetch_style], [mixed $fetch_argument], [array $ctor_args = array()]) DESCRIPTION
PARAMETERS
o $fetch_style - Controls the contents of the returned array as documented in PDOStatement.fetch(3). Defaults to value of PDO::ATTR_DEFAULT_FETCH_MODE (which defaults to PDO::FETCH_BOTH) To return an array consisting of all values of a single column from the result set, specify PDO::FETCH_COLUMN. You can specify which column you want with the $column-index parameter. To fetch only the unique values of a single column from the result set, bitwise-OR PDO::FETCH_COLUMN with PDO::FETCH_UNIQUE. To return an associative array grouped by the values of a specified column, bitwise-OR PDO::FETCH_COLUMN with PDO::FETCH_GROUP. o $fetch_argument - This argument has a different meaning depending on the value of the $fetch_style parameter: o PDO::FETCH_COLUMN: Returns the indicated 0-indexed column. o PDO::FETCH_CLASS: Returns instances of the specified class, mapping the columns of each row to named properties in the class. o PDO::FETCH_FUNC: Returns the results of calling the specified function, using each row's columns as parameters in the call. o $ctor_args - Arguments of custom class constructor when the $fetch_style parameter is PDO::FETCH_CLASS. RETURN VALUES
PDOStatement.fetchAll(3) returns an array containing all of the remaining rows in the result set. The array represents each row as either an array of column values or an object with properties corresponding to each column name. An empty array is returned if there are zero results to fetch, or FALSE on failure. Using this method to fetch large result sets will result in a heavy demand on system and possibly network resources. Rather than retriev- ing all of the data and manipulating it in PHP, consider using the database server to manipulate the result sets. For example, use the WHERE and ORDER BY clauses in SQL to restrict results before retrieving and processing them with PHP. EXAMPLES
Example #1 Fetch all remaining rows in a result set <?php $sth = $dbh->prepare("SELECT name, colour FROM fruit"); $sth->execute(); /* Fetch all of the remaining rows in the result set */ print("Fetch all of the remaining rows in the result set: "); $result = $sth->fetchAll(); print_r($result); ?> The above example will output something similar to: Fetch all of the remaining rows in the result set: Array ( [0] => Array ( [name] => pear [0] => pear [colour] => green [1] => green ) [1] => Array ( [name] => watermelon [0] => watermelon [colour] => pink [1] => pink ) ) Example #2 Fetching all values of a single column from a result set The following example demonstrates how to return all of the values of a single column from a result set, even though the SQL state- ment itself may return multiple columns per row. <?php $sth = $dbh->prepare("SELECT name, colour FROM fruit"); $sth->execute(); /* Fetch all of the values of the first column */ $result = $sth->fetchAll(PDO::FETCH_COLUMN, 0); var_dump($result); ?> The above example will output something similar to: Array(3) ( [0] => string(5) => apple [1] => string(4) => pear [2] => string(10) => watermelon ) Example #3 Grouping all values by a single column The following example demonstrates how to return an associative array grouped by the values of the specified column in the result set. The array contains three keys: values apple and pear are returned as arrays that contain two different colours, while water- melon is returned as an array that contains only one colour. <?php $insert = $dbh->prepare("INSERT INTO fruit(name, colour) VALUES (?, ?)"); $insert->execute(array('apple', 'green')); $insert->execute(array('pear', 'yellow')); $sth = $dbh->prepare("SELECT name, colour FROM fruit"); $sth->execute(); /* Group values by the first column */ var_dump($sth->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP)); ?> The above example will output something similar to: array(3) { ["apple"]=> array(2) { [0]=> string(5) "green" [1]=> string(3) "red" } ["pear"]=> array(2) { [0]=> string(5) "green" [1]=> string(6) "yellow" } ["watermelon"]=> array(1) { [0]=> string(5) "green" } } Example #4 Instantiating a class for each result The following example demonstrates the behaviour of the PDO::FETCH_CLASS fetch style. <?php class fruit { public $name; public $colour; } $sth = $dbh->prepare("SELECT name, colour FROM fruit"); $sth->execute(); $result = $sth->fetchAll(PDO::FETCH_CLASS, "fruit"); var_dump($result); ?> The above example will output something similar to: array(3) { [0]=> object(fruit)#1(2) { ["name"]=> string(5) "apple" ["colour"]=> string(5) "green" } [1]=> object(fruit)#2(2) { ["name"]=> string(4) "pear" ["colour"]=> string(6) "yellow" } [2]=> object(fruit)#3(2) { ["name"]=> string(10) "watermelon" ["colour"]=> string(4) "pink" } } Example #5 Calling a function for each result The following example demonstrates the behaviour of the PDO::FETCH_FUNC fetch style. <?php function fruit($name, $colour) { return "{$name}: {$colour}"; } $sth = $dbh->prepare("SELECT name, colour FROM fruit"); $sth->execute(); $result = $sth->fetchAll(PDO::FETCH_FUNC, "fruit"); var_dump($result); ?> The above example will output something similar to: array(3) { [0]=> string(12) "apple: green" [1]=> string(12) "pear: yellow" [2]=> string(16) "watermelon: pink" } SEE ALSO
PDO.query(3), PDOStatement.fetch(3), PDOStatement.fetchColumn(3), PDO.prepare(3), PDOStatement.setFetchMode(3). PHP Documentation Group PDOSTATEMENT.FETCHALL(3)
All times are GMT -4. The time now is 09:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy