Sponsored Content
Top Forums Shell Programming and Scripting Count occurrences in first column Post 302944530 by sheemam on Wednesday 20th of May 2015 01:52:04 AM
Old 05-20-2015
This one should work.

Code:
awk 'BEGIN{h[" "]=0;max=0}{ind=-1;for(k in h)if(k==$1){ind=h[k];break}if(ind==-1){ind=++max;h[$1]=ind}print $1,ind,$2,$3}' infile

 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

count occurrences and substitute with counter

Hi Unix-Experts, I have a textfile with several occurrences of some string XXX. I'd like to count all the occurrences and number them in reverse order. E.g. input: XXX bla XXX foo XXX output: 3 bla 2 foo 1 I tried to achieve this with sed, but failed. Any suggestions? Thanks in... (4 Replies)
Discussion started by: ptob
4 Replies

2. UNIX for Dummies Questions & Answers

Search and Count Occurrences of Pattern in a File

I need to search and count the occurrences of a pattern in a file. The catch here is it's a pattern and not a word ( not necessarily delimited by spaces). For eg. if ABCD is the pattern I need to search and count, it can come in all flavors like (ABCD, ABCD), XYZ.ABCD=100, XYZ.ABCD>=500,... (6 Replies)
Discussion started by: tektips
6 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

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

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

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

7. Shell Programming and Scripting

How to count occurrences in a specific column

Hi, I need help to count the number of occurrences in $3 of file1.txt. I only know how to count by checking one by one and the code is like this: awk '$3 ~ /aku hanya poyo/ {++c} END {print c}' FS="\t" file1.txt But this is not wise to do as i have hundreds of different occurrences in that... (10 Replies)
Discussion started by: redse171
10 Replies

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

9. 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
DS(3)                                                      DACS Library Functions Manual                                                     DS(3)

NAME
ds - Dynamic strings and vectors SYNOPSIS
#include "dsslib.h" Ds *ds_init(Ds *ods); Ds *ds_init_size(Ds *ods, size_t nbytes); Ds *ds_alloc(void); Ds *ds_alloc_size(size_t nbytes); Ds *ds_reinit(Ds *ds); Ds *ds_reinit_size(Ds *ods, size_t nbytes); Ds *ds_reset(Ds *ds); Ds *ds_reset_buf(Ds *ds); Ds *ds_trim(Ds *ds); Ds *ds_grow(Ds *ds); void ds_free(Ds *ds); int ds_appendc(Ds *ds, int ch); int ds_copyb(Ds *ds, void *src, size_t len, unsigned int offset); int ds_insertb(Ds *ds, void *src, size_t len, unsigned int offset); int ds_append(Ds *ds, char *str); int ds_concat(Ds *ds, char *str); int ds_concatc(Ds *ds, int ch); int ds_concatn(Ds *ds, char *str, size_t len); Ds *ds_set(Ds *ds, char *str); Ds *ds_setn(Ds *ds, unsigned char *s, size_t slen); Ds *ds_range(Ds *ds, char *str, char *range_spec, Range_syntax *ors); int ds_getc(Ds *ds, FILE *fp, int *ch_ptr); Dsio *dsio_set(Ds *ds, FILE *fp, char *buf, unsigned long len, int have_length); int dsio_free(Ds *ds); int dsio_eof(Ds *ds); int dsio_nextc(Ds *ds, int *ch_ptr); int dsio_peekc(Ds *ds, int *ch_ptr); char *dsio_agets(Ds *ds); char *dsio_gets(Ds *ds); int dsio_load(Ds *ds); char *dsio_load_str(Ds *ds); char *ds_gets(Ds *ds, FILE *fp); char *ds_agets(Ds *ds, FILE *fp); char *ds_agets_buf(Ds *ds, char *buf); char *ds_prompt(Ds *ds, char *prompt, unsigned int flag); char *ds_readline(Ds *ds, char *prompt, char *cprompt); Ds *ds_load_file(Ds *ds, char *pathname); Ds *ds_agetf(Ds *ds, FILE *fp); Ds *ds_getf(FILE *fp); int ds_sprintf(Ds *ds, unsigned int offset, char *fmt, ...); int ds_asprintf(Ds *ds, char *fmt, ...); int ds_vasprintf(Ds *ds, char *fmt, va_list ap); char *ds_xprintf(char *fmt, ...); char *ds_vxprintf(char *fmt, va_list ap); FILE *ds_fopen_secure(const char *path, const char *mode, size_t size); int ds_fclose(FILE *fp); int ds_fclean(FILE *fp); Dsvec *dsvec_init(Dsvec *dsv, size_t size); Dsvec *dsvec_alloc(size_t size); Dsvec *dsvec_init_size(Dsvec *dsv, size_t size, int n); Dsvec *dsvec_alloc_size(size_t size, int n); void dsvec_free(Dsvec *dsv); Dsvec *dsvec_grow(Dsvec *dsv, unsigned int new_nelements); Dsvec *dsvec_subset(Dsvec *dsv, Dsvec *old_dsv, unsigned int start, int len); Dsvec *dsvec_copy(Dsvec *dsv, Dsvec *old_dsv); void *dsvec_ptr_index(Dsvec *dsv, unsigned int ind); int dsvec_delete_ptr_index(Dsvec *dsv, unsigned int ind); int dsvec_delete_ptr(Dsvec *dsv, void *ptr); int dsvec_replace_ptr(Dsvec *dsv, void *ptr, unsigned int ind); int dsvec_streq(Dsvec *dsv1, Dsvec *dsv2); int dsvec_strneq(Dsvec *dsv1, Dsvec *dsv2, size_t len); int dsvec_rotate(Dsvec *dsv, int npos); Dsvec *dsvec_strlist(Dsvec *dsv, char *str_spec); Dsvec *dsvec_slice(Dsvec *dsv, Dsvec *primary, unsigned int ind, unsigned int len); Dsvec *dsvec_range(Dsvec *dsv, Dsvec *primary, char *range_spec, Range_syntax *ors); int dsvec_replace(Dsvec *primary, unsigned int ind, unsigned int delete_len, Dsvec *secondary); int dsvec_append(Dsvec *primary, Dsvec *secondary); int dsvec_insert_ptr(Dsvec *dsv, unsigned int ind, void *ptr); int dsvec_add_ptr(Dsvec *dsv, void *ptr); int dsvec_insert(Dsvec *primary, unsigned int ind, Dsvec *secondary); int dsvec_delete(Dsvec *dsv, unsigned int ind, unsigned int ndelete); int dsvec_add_obj(Dsvec *dsv); void dsvec_sort(Dsvec *dsv, int (*compar)(const void *, const void *)); Dsvec *dsvec_load(Ds *ds, Dsvec *dsv); Dsvec *ds_mkargv_add(Dsvec *dsv, char *el); Dsvec *ds_mkargv_addv(Dsvec *dsv, int argc, char **argv); typedef struct Mkargv { int keepq; int keepws; char *ifs; char *startq; char *endq; } Mkargv; Dsvec *ds_mkargv(Dsvec *dsv, char *str, Mkargv *conf); char *ds_buf(Ds *ds); size_t ds_len(Ds *ds); size_t ds_size(Ds *ds); int ds_eq(Ds *ds1, Ds *ds2); (TYPE) dsvec_ptr(Ds *DSV, unsigned int IND, TYPE); (TYPE) dsvec_obj(Ds *DSV, unsigned int IND, TYPE); (TYPE) dsvec_new_obj(Ds *DSV, TYPE); (TYPE) dsvec_base_type(Ds *DSV, TYPE); void **dsvec_base(Dsvec *dsv); unsigned int dsvec_len(Dsvec *dsv); extern int ds_default_clear_flag; extern int ds_default_delnl_flag; extern size_t ds_default_len_limit; extern void *(*ds_default_malloc_func)(size_t size); extern void *(*ds_default_free_func)(void *ptr); DESCRIPTION
The DS library is a collection of functions for creating, manipulating, and formatting strings of bytes. The memory used by these strings is dynamically allocated and released as necessary. When used correctly, bugs caused by buffer overruns and other programming errors can be avoided. There are also functions for creating and manipulating dynamic vectors of pointers and objects. Byte Strings A Ds data structure represents a dynamic string. It must be initialized before it can be used. A Ds variable contains several fields that may be set after initialization but before it is first used. Default values for some of these fields are copied from global variables exported by the library. Initialization Manipulation Formatting Vectors DACS 1.4.27b 10/22/2012 DS(3)
All times are GMT -4. The time now is 05:37 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy