counting prefixes of files

Hello,

at the moment I'm on with programming some kind of version history script for network devices.

The configration files are uploaded in the form:
devicename-confg_date_time.

For keeping the last 10 configurations I want to split the devicename from the rest. This works well with

ls -1 | awk '{FS="_"; print $1}' 

but how do I count how often one devicename is returned?

Could someone please help me. I'm going crazy with this...

Love,
Sally

P.S.: I'm working on a BASH

Try if i understand you correctly...

ls -1 | awk -F\_ '{print $1,++arr[$1]}' 
1 Like

Just a minor (and quite unrelated) observation: you don't need to issue "ls -1" when you pipe into another process, because "ls" will format its output in one long column automatically if the output is not going to a terminal. Therefore "ls -1 | ..." and "ls | ..." will give the same result.

bakunin

ls | awk -F'_'   ' { arr[$1]++ } END {for(i in arr) { print i, arr } } '
1 Like

maybe

ls -1 | awk '{FS="_"; print $1}' | sort | uniq -c

Hello,

Thank you all, I got it now:

l34nmis:r2lanconfg-~/history> ls | awk -F\_ '/confg/ { ++arr[$1]; } END { for (name in arr) {print name, arr[name] } }'
deipn-mann-34-r-2-unitlab-confg 3
deipn-mann-34-r-1-rzlab-confg 2
deipn-mann-r-2-rzlab-confg 2
deipn-mann-34-sw-1-unitlab-confg 2
deipn-mann-34-sw-2-unitlab-confg 2
deipn-mann-34-r-1-unitlab-confg 2
l34nmis:r2lanconfg-~/history>

@jim mcnamara: I used the proposal from malcolmex and found exactly your solution. :slight_smile:

love,
Sally