Sponsored Content
Top Forums UNIX for Beginners Questions & Answers awk or sed script to count number of occurrences and creating an average Post 303039222 by kshitij on Thursday 26th of September 2019 10:16:47 AM
Old 09-26-2019
awk or sed script to count number of occurrences and creating an average

Hi Friends ,

I am having one problem as stated file .
Having an input CSV file as shown in the code

Code:
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_2_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_4_/Q,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,0,0
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_5_/Q,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_3_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0

I need to find the number of times "1" occurs in each line and it should go as the last column in each line
Similarly I need to find the number of times "0" occurs in each line and it should go as the last column in each line
I need to create the average of [ number of times "1" occurs / number of "1" or "0" occurs] in each line
I need to create the average of [ number of times "0" occurs / number of "1" or "0" occurs] in each line

Output file should be something like this
Code:
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_2_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0,{Number of 1 occurence}, {Number of 0 occurence} , {Number of 1 / Number of 1 and 0 } , {Number of 0 / Number of 1 and Number of 0}
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_4_/Q,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,0,0,{Number of 1 occurrence}, {Number of 0 occurrence} , {Number of 1 / Number of 1 and 0 } , {Number of 0 / Number of 1 and Number of 0}
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_5_/Q,0,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,{Number of 1 occurrence}, {Number of 0 occurrence} , {Number of 1 / Number of 1 and 0 } , {Number of 0 / Number of 1 and Number of 0}
U_TOP_LOGIC/U_HPB2/U_HBRIDGE2/i_core/i_paddr_reg_3_/Q,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,0,0,{Number of 1 occurrence}, {Number of 0 occurrence} , {Number of 1 / Number of 1 and 0 } , {Number of 0 / Number of 1 and Number of 0}

Could you help me out ?

Thanks
Kshitij Smilie
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to count number of occurrences of a "|" from a variable?

I have a variable, var="some1|some2|some3" I want to know how many "|" are in $var. When I say echo $var | grep -c '|' I am getting only 1 :confused: :confused: :confused: ? (4 Replies)
Discussion started by: jingi1234
4 Replies

2. 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

3. Shell Programming and Scripting

Count the number of occurrences of the word

I am a newbie in UNIX shell script and seeking help on this UNIX function. Please give me a hand. Thanks. I have a large file. Named as 'MyFile'. It was tab-delmited. I am told to write a shell function that counts the number of occurrences of the ord “mysring” in the file 'MyFile'. (1 Reply)
Discussion started by: duke0001
1 Replies

4. Shell Programming and Scripting

Count the number of occurrences of a pattern between each occurrence of a different pattern

I need to count the number of occurrences of a pattern, say 'key', between each occurrence of a different pattern, say 'lu'. Here's a portion of the text I'm trying to parse: lu S1234L_149_m1_vg.6, part-att 1, vdp-att 1 p-reserver IID 0xdb registrations: key 4156 4353 0000 0000 ... (3 Replies)
Discussion started by: slipstream
3 Replies

5. 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

6. 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

7. Shell Programming and Scripting

Count occurrences in awk

Hello, I have an output from GDB with many entries that looks like this 0x00007ffff7dece94 39 in dl-fini.c 0x00007ffff7dece97 39 in dl-fini.c 0x00007ffff7ab356c 50 in exit.c 0x00007ffff7aed9db in _IO_cleanup () at genops.c:1022 115 in dl-fini.c 0x00007ffff7decf7b in _dl_sort_fini (l=0x0,... (6 Replies)
Discussion started by: ikke008
6 Replies

8. Shell Programming and Scripting

Script to count word occurrences, but exclude some?

I am trying to count the occurrences of ALL words in a file. However, I want to exclude certain words: short words (i.e. <3 chars), and words contained in an blacklist file. There is also a desire to count words that are capitalized (e.g. proper names). I am not 100% sure where the line on... (5 Replies)
Discussion started by: Cronk
5 Replies

9. Shell Programming and Scripting

awk Group By and count string occurrences

Hi Gurus, I'm scratching my head over and over and couldn't find the the right way to compose this AWK properly - PLEASE HELP :confused: Input: c,d,e,CLICK a,b,c,CLICK a,b,c,CONV c,d,e,CLICK a,b,c,CLICK a,b,c,CLICK a,b,c,CONV b,c,d,CLICK c,d,e,CLICK c,d,e,CLICK b,c,d,CONV... (6 Replies)
Discussion started by: Royi
6 Replies

10. Shell Programming and Scripting

Count the number of string occurrences to display 0 entries in output

Hello Friends, Can somebody assist an issue I am having? I have a separate file with a list of account ids XXX200B02Y01 XXX200B03Y01 XXX200B05Y01 XXX200B07Y01 XXX200B08Y01 I call the file, and run an egrep against a directory and logfiles AccountID=$(cat... (2 Replies)
Discussion started by: liketheshell
2 Replies
ost::Number(3)						     Library Functions Manual						    ost::Number(3)

NAME
ost::Number - A number manipulation class. SYNOPSIS
#include <numbers.h> Inherited by ost::DateNumber, and ost::ZNumber. Public Member Functions Number (char *buffer, unsigned size) Create an instance of a number. void setValue (long value) const char * getBuffer () const long getValue () const long operator() () operator long () operator char * () long operator= (const long value) long operator+= (const long value) long operator-= (const long value) long operator-- () long operator++ () int operator== (const Number &num) int operator!= (const Number &num) int operator< (const Number &num) int operator<= (const Number &num) int operator> (const Number &num) int operator>= (const Number &num) Protected Attributes char * buffer unsigned size Friends long operator+ (const Number &num, const long val) long operator+ (const long val, const Number &num) long operator- (const Number &num, long val) long operator- (const long val, const Number &num) Detailed Description A number manipulation class. This is used to extract, convert, and manage simple numbers that are represented in C ascii strings in a very quick and optimal way. Author: David Sugar dyfet@ostel.com number manipulation. Constructor &; Destructor Documentation ost::Number::Number (char *buffer, unsignedsize) Create an instance of a number. Parameters: buffer or NULL if created internally. size use - values for zero filled. Member Function Documentation const char* ost::Number::getBuffer () const [inline] long ost::Number::getValue () const ost::Number::operator char * () [inline] ost::Number::operator long () [inline] int ost::Number::operator!= (const Number &num) long ost::Number::operator() () [inline] long ost::Number::operator++ () long ost::Number::operator+= (const longvalue) long ost::Number::operator-- () long ost::Number::operator-= (const longvalue) int ost::Number::operator< (const Number &num) int ost::Number::operator<= (const Number &num) long ost::Number::operator= (const longvalue) Reimplemented in ost::ZNumber. int ost::Number::operator== (const Number &num) int ost::Number::operator> (const Number &num) int ost::Number::operator>= (const Number &num) void ost::Number::setValue (longvalue) Reimplemented in ost::ZNumber. Friends And Related Function Documentation long operator+ (const Number &num, const longval) [friend] long operator+ (const longval, const Number &num) [friend] long operator- (const Number &num, longval) [friend] long operator- (const longval, const Number &num) [friend] Member Data Documentation char* ost::Number::buffer [protected] unsigned ost::Number::size [protected] Author Generated automatically by Doxygen for GNU CommonC++ from the source code. GNU CommonC++ Sat Jun 23 2012 ost::Number(3)
All times are GMT -4. The time now is 07:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy