Sponsored Content
Top Forums Shell Programming and Scripting Help me to perform count & group by operation in shell scripting? Post 302728875 by rdrtx1 on Thursday 8th of November 2012 03:21:28 PM
Old 11-08-2012
try also:
Code:
awk '{a[$1]++;}END{for (i in a) print i, a[i]}' file

---------- Post updated at 02:21 PM ---------- Previous update was at 02:10 PM ----------

or for a shell example:
Code:
#!/bin/sh
 
while read var
do
  ar[$var]=$var
  (( arr[$var]++ ))
done < file
 
for v in ${ar[@]}
do
  echo $v ${arr[$v]}
done

This User Gave Thanks to rdrtx1 For This Post:
 

10 More Discussions You Might Find Interesting

1. Solaris

How to perform addition of two numbers in shell scripting in Solaris-10

Hi, I have a sh script which contains the following line TOTAL=$((e4-s4)) -> Where e4 and s4 are input got from the user. At the time of execution of this line the following error occurs test.sh: syntax error at line 8: `TOTAL=$' unexpected How to solve this issue?. Can any... (9 Replies)
Discussion started by: krevathi1912
9 Replies

2. UNIX for Dummies Questions & Answers

what does the operation '>&' mean in a shell script

hi, all, i saw a syntax like this usr/local/mpiexec -np 8 /home/XXXX/program_exe >& ./temp/out the output will be put into ./temp/out. I want to know the meaning of the operation symbol '>&'. Thanks (3 Replies)
Discussion started by: cy163
3 Replies

3. Shell Programming and Scripting

How to perform arithmetic operation on date

Hi all, I would appreciate if anyone knows how to perform adding to date. As for normal date, i can easily plus with any number. But when it comes to month end say for example 28 Jun, i need to perform a plus with number 3, it will not return 1 Jul. Thanks in advance for your help. (4 Replies)
Discussion started by: agathaeleanor
4 Replies

4. Shell Programming and Scripting

Enter third column & Perform Operation

I am trying to enter a third column in this file, but the third column should that I call "Math" perform a some math calculations based on the value found in column #2. Here is the input file: Here is the desired output: Output GERk0203078$ Levir Math Cotete_1... (5 Replies)
Discussion started by: Ernst
5 Replies

5. Shell Programming and Scripting

[Solved] Perform an operation to all directories

Sorry, about this thread - I solved my own problem! Thanks for taking a look. edit by bakunin: no problem, but it would have been a nice touch to actually tell us what the solution was. This would have been slightlich more educating than just knowing that you found it. I changed your title to... (0 Replies)
Discussion started by: Blue Solo
0 Replies

6. Shell Programming and Scripting

How to get the file size and count of a table using shell scripting?

Hi there, im a beginner to the shell scripting.i trying to extract a table from a db(IMD) and i have to get the count of that table and size of the file. can you help me out how to write the shall scriping for the above query. (2 Replies)
Discussion started by: pawanmamidi
2 Replies

7. Homework & Coursework Questions

Using dbms_pipe with C++ to perform daabase operation

I am getting two result: string and int in c++ code. That I want to store into database. The request which generates result is very frequent. So each time performing db operation to store the result is costly for me. So how this can be achived using dbms_sql? I dont have any experience and how... (1 Reply)
Discussion started by: karimkhan
1 Replies

8. Shell Programming and Scripting

How To Perform Mathematical Operation Within If in awk?

Hi All, I am using an awk script as below: awk -F'|' 'BEGIN{OFS="|";} { if ($1==$3 && $3==$7 && $7==$13 && $2==$6 && $6==$11 && $15-$14+1==$11) print $0"|""TRUE"; else print $0"|""FALSE"; }' tempfile.txt In above script, all conditions are being checked except the one which is... (4 Replies)
Discussion started by: angshuman
4 Replies

9. Shell Programming and Scripting

awk script to find data in three file and perform replace operation

Have three files. Any other approach with regards to file concatenation or splitting, etc is appreciated If column55(billngtype) of file1 contains YMNC or YPBC then pick the value of column13(documentnumber). Now find this documentnumber in column1(Billdoc) of file2 and grep the corresponding... (4 Replies)
Discussion started by: as7951
4 Replies

10. UNIX for Beginners Questions & Answers

Copy last few lines of a file, perform math operation and iterate further

Hi, I am trying to generate a data of following order: 4 0 1 642 643 4 642 643 1283 1284 4 1283 1284 1924 1925 4 1924 1925 2565 2566 4 2565 2566 3206 3207 4 3206 3207 3847 3848 4 3847 3848 4488 4489 4 4488 4489 5129 5130 ---------------------- 4 1 2 643 644 4 643 644 1284... (6 Replies)
Discussion started by: SaPa
6 Replies
ARRAY_FILTER(3) 							 1							   ARRAY_FILTER(3)

array_filter - Filters elements of an array using a callback function

SYNOPSIS
array array_filter (array $array, [callable $callback], [int $flag]) DESCRIPTION
Iterates over each value in the $array passing them to the $callback function. If the $callback function returns true, the current value from $array is returned into the result array. Array keys are preserved. PARAMETERS
o $array - The array to iterate over o $callback - The callback function to use If no $callback is supplied, all entries of $array equal to FALSE (see converting to boolean) will be removed. o $flag - Flag determining what arguments are sent to $callback: o ARRAY_FILTER_USE_KEY - pass key as the only argument to $callback instead of the value o ARRAY_FILTER_USE_BOTH - pass both value and key as arguments to $callback instead of the value RETURN VALUES
Returns the filtered array. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.6.0 | | | | | | | Added optional $flag parameter and constants | | | ARRAY_FILTER_USE_KEY and ARRAY_FILTER_USE_BOTH | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 array_filter(3) example <?php function odd($var) { // returns whether the input integer is odd return($var & 1); } function even($var) { // returns whether the input integer is even return(!($var & 1)); } $array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5); $array2 = array(6, 7, 8, 9, 10, 11, 12); echo "Odd : "; print_r(array_filter($array1, "odd")); echo "Even: "; print_r(array_filter($array2, "even")); ?> The above example will output: Odd : Array ( [a] => 1 [c] => 3 [e] => 5 ) Even: Array ( [0] => 6 [2] => 8 [4] => 10 [6] => 12 ) Example #2 array_filter(3) without $callback <?php $entry = array( 0 => 'foo', 1 => false, 2 => -1, 3 => null, 4 => '' ); print_r(array_filter($entry)); ?> The above example will output: Array ( [0] => foo [2] => -1 ) Example #3 array_filter(3) with $flag <?php $arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]; var_dump(array_filter($arr, function($k) { return $k == 'b'; }, ARRAY_FILTER_USE_KEY)); var_dump(array_filter($arr, function($v, $k) { return $k == 'b' || $v == 4; }, ARRAY_FILTER_USE_BOTH)); ?> The above example will output: array(1) { ["b"]=> int(2) } array(2) { ["b"]=> int(2) ["d"]=> int(4) } NOTES
Caution If the array is changed from the callback function (e.g. element added, deleted or unset) the behavior of this function is unde- fined. SEE ALSO
array_map(3), array_reduce(3), array_walk(3). PHP Documentation Group ARRAY_FILTER(3)
All times are GMT -4. The time now is 04:34 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy