With GNU awk
:
awk -v FPAT=' [0-9]+#' '{ c[$1]++; }; END{ for(x in c) print x, c[x]; }' infile
123# 1
456# 1
Assuming there is always one pattern " [0-9]+#
" matched per line as shown in your given sample input;
to filtering out the whitespaces from the result and also during processing for a input like:
(1608926678.237962) vcan0 123#0000000158
(1608926678.251533) vcan0 456#0000000186
(1608926678.237962) vcan0 123#0000000158
(1608926678.251533) vcan0 456#0000000186
(1608926678.237962) vcan0 123#0000000158
(1608926678.251533) vcan0 456#0000000186
(1608926678.237962) vcan0 123#0000000158
awk -v FPAT='[ \t][0-9]+#' '{
filter=$1; sub(/[ \t]/, "", filter);
c[filter]++;
};
END{ for(x in c) print x, c[x]; }' infile
456# 3
123# 4
for a input having multiple matched pattern " [0-9]+#
" in each or every lines, you would do:
awk -v FPAT='[ \t][0-9]+#' '{
for (i=1; i<=NF; i++){
filter=$i; sub(/[ \t]/, "", filter); c[filter]++;
};
};
END{ for(x in c) print x, c[x]; }' infile
\d
nor the+
qualifier are supported by BRE grep - see for example Why does my regular expression work in X but not in Y? – steeldriver Dec 21 '20 at 19:40