counting prefixes of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting counting prefixes of files
# 1  
Old 05-31-2010
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

Code:
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

Moderator's Comments:
Mod Comment I added some CODE-tags. Please use them yourself when posting code (-fragments), file contents, output or similar data. Thank you.

Last edited by bakunin; 05-31-2010 at 05:48 AM..
# 2  
Old 05-31-2010
Try if i understand you correctly...

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

This User Gave Thanks to malcomex999 For This Post:
# 3  
Old 05-31-2010
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
# 4  
Old 05-31-2010
Code:
ls | awk -F'_'   ' { arr[$1]++ } END {for(i in arr) { print i, arr[i] } } '

This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 05-31-2010
maybe
Code:
ls -1 | awk '{FS="_"; print $1}' | sort | uniq -c

# 6  
Old 05-31-2010
Hello,

Thank you all, I got it now:

Code:
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. Smilie

love,
Sally
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove prefixes before dot

Shell : Bash shell I have a text file with entries like below srv.sr_num sr_number, atvx.ATTRIB_37 Product_Name, ktx.X_ATTRIB_52 Product_Type, mkx.created sr_created_date, nbv.sr_cat_type_cd sr_type, bkrx.sr_area sr_category, .. frx.order_id, des.stats_name , fpxg.current_id_name, ...... .... (3 Replies)
Discussion started by: John K
3 Replies

2. UNIX for Dummies Questions & Answers

Counting files without ls or wc

i need to write a shell script to "count the number of files in the current directory but without using either ls or wc command"..... please help! (1 Reply)
Discussion started by: lexicon
1 Replies

3. Shell Programming and Scripting

Start from the prefixes, delete script

My folder is/app2/istech/scratch, which contain all the below files. I need to delete the files which start from the prefixes. dup_events_*.* Event_*.* New_time_*.* New_Loc_*.* New_Uptime_*.* Detailed_Reason*.* cmt_dup_*.* Uptime_*.* Can anyone please let me know how to write a... (3 Replies)
Discussion started by: dsnaveen
3 Replies

4. Shell Programming and Scripting

Counting Files

In a script, how would I go about finding the number of files for the first parameter after my script name? For instance, my script name is myscript.sh and the folder I am checking is not the current working directory, lets say it's folder1. so I type myscript.sh folder1 This script below... (2 Replies)
Discussion started by: Confirmed104
2 Replies

5. UNIX for Dummies Questions & Answers

vi editor prefixes lines with # upon paste

I've been away from Unix and the vi editor for a while, and now I'm using vi (actually vim) in a Cygwin bash shell. When I copy-and-paste code examples (I'm playing with perl now) any time I paste code with lines beginning with the # character, vi inserts a # character at the beginning of every... (2 Replies)
Discussion started by: greenmangroup
2 Replies

6. UNIX for Dummies Questions & Answers

Counting rows in many files

I'm regularly counting the number of rows in a number of files. I need to know how many rows their are in all files together. Counting rows in one file I can handle, but how do I count rows in all at once? I'd be grateful for any answer. (7 Replies)
Discussion started by: DrZoidberg
7 Replies

7. Solaris

Counting up files

Hi, I have a load of if statements that look for files in a directory, I want to be able to count them up and the total files confirmed in an email? I ahve tried expr but i this does not work and it only reads in the first if and ignores the rest. Please see script, #!/bin/ksh ... (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

8. Shell Programming and Scripting

Help with counting files please

Hi all. If I have a unix directory with multiple files, lets say, I have some with .dat extensions, some with .txt extensions, etc etc. How in a script would I provide a count of all the different file types (so, the different extensions, I guess) in the directory?? So if I had: test.dat... (6 Replies)
Discussion started by: gerard1
6 Replies

9. UNIX for Dummies Questions & Answers

counting files

Which one line command can count and print to the screen the number of files containing "a" or "A" in their name (5 Replies)
Discussion started by: Edy
5 Replies
Login or Register to Ask a Question