how to format a list of files from ls?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to format a list of files from ls?
# 1  
Old 10-20-2009
how to format a list of files from ls?

Hi,

I have am looking for advice on how to format a directory listing... Basically, I have a list of speech voices at /System/Library/speech/voices

This directory looks like:
Agnes.SpeechVoice
Albert.SpeechVoice
Alex.SpeechVoice
.. etc

I would like to print these out by splitting by "." and just displaying the first part and order them by number like:

1. Agnes
2. Albert
3. Alex
.. etc.

How is the best way to do this?

-patrick
# 2  
Old 10-20-2009
bash
Code:
# i=0;for file in *SpeechVoice; do i=$(( i+1 ));echo "$i ${file%%.*}"; done
1 Agnes
2 Albert
3 Alex

# 3  
Old 10-20-2009
Code:
ls | sort | cut -d. -f1

# 4  
Old 10-20-2009
you can do that with a simple command..
Code:
for dir_name in `ls *.SpeechVoice` ; do
basename  $dir_name .SpeechVoice
done


Last edited by vidyadhar85; 10-21-2009 at 12:55 AM..
# 5  
Old 10-21-2009
that's not quite there...yet at least. There's no option for wildcard expansion for basename the last time i checked. (if there is, then pardon me for my ignorance)
Code:
# basename  Agnes.SpeechVoice  .SpeechVoice
Agnes
# basename  *SpeechVoice  .SpeechVoice
basename: extra operand `.SpeechVoice'
Try `basename --help' for more information.
# basename  *.SpeechVoice  .SpeechVoice
basename: extra operand `.SpeechVoice'
Try `basename --help' for more information.
# basename  *.SpeechVoice
Agnes.SpeechVoice



---------- Post updated at 11:34 PM ---------- Previous update was at 10:43 PM ----------

Quote:
Originally Posted by vidyadhar85
you can do that with a simple command..
Code:
for dir_name in `ls *.SpeechVoice` ; do
basename  $dir_name .SpeechVoice
done

another way without the ls
Code:
for name in *.SpeechVoice
do
 .....
done


Last edited by ghostdog74; 10-21-2009 at 01:33 AM..
# 6  
Old 10-21-2009
Quote:
Originally Posted by vidyadhar85
you can do that with a simple command..
Code:
for dir_name in `ls *.SpeechVoice` ; do


Not only is ls unnecessary, but it will break the script if any filenames contain spaces. Use wildcard expansion directly.
Quote:
Code:
basename  $dir_name .SpeechVoice
done


And there's no need for the external command, basename.

Code:
for dir_name in *.SpeechVoice
do
  printf "%s\n" "${dir_name%.SpeechVoice}"
done

In bash or ksh93, it can be done much faster using an array:

Code:
x=( *.SpeechVoice )
printf "%s\n"  "${x[@]%.*}"


Last edited by cfajohnson; 10-23-2009 at 12:50 AM..
# 7  
Old 10-23-2009
Code:
awk -F"." '{printf("%s %s\n",NR,$1)}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Not sure how to describe it but how to format this list so that it print the 1st field once only

Hi, Not sure how to describe the problem. But basically, I have this file listing here app01_app.test.com.ph|PROGRAM=SQL Developer|HOST=AKL0TS100|USER=Admtest01|HOST=10.111.12.23| app02_app.test.com.ph|PROGRAM=D:\interface\apps\bin32\batch.exe|HOST=AKL0TS100|USER=Admtest09|HOST=10.111.12.35|... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

How to find list of missing files based on the file format?

Hi All, In the file names we have dates. Based on the file format given by the user, if any file is not existed for a particular date with in a given interval we should consider that file is missing. I have the below files in the directory /bin/daily/voda_files. ... (9 Replies)
Discussion started by: nalu
9 Replies

3. Shell Programming and Scripting

Long list file display different time format.

Hi Gurus, I have some weird issue. when using ls -l the result shows different time format: -rw-r--r-- 1 abc gourp1 3032605576 Jun 14 2013 abc -rw-rw-r-- 1 abc gourp1 1689948832 Aug 10 06:22 abc one display 2013 which is year; another one displays 06:22 which is time. ... (4 Replies)
Discussion started by: ken6503
4 Replies

4. UNIX for Dummies Questions & Answers

Rename all Files in a UNIX Directory from one date format to another date format

Hi Unix Gurus, I would like to rename several files in a Unix Directory . The filenames can have more than 1 underscore ( _ ) and the last underscore is always followed by a date in the format mmddyyyy. The Extension of the files can be .txt or .pdf or .xls etc and is case insensitive ie... (1 Reply)
Discussion started by: pchegoor
1 Replies

5. Shell Programming and Scripting

Need script for transferring bulk files from one format to text format

"Help Me" Need script for transferring bulk files from one format to text format in a unix server. Please suggest (2 Replies)
Discussion started by: Kranthi Kumar
2 Replies

6. Shell Programming and Scripting

search for content in files. Name of files is in another file. Format as report.

Hi I have multiple files in a folder and one file which contains a list of files (one on each line). I was to search for a string only within these files and not the whole folder. I need the output to be in the form File1<tab>string instance 2<tab> string instance 2<tab>string instance 3... (6 Replies)
Discussion started by: pkabali
6 Replies

7. Shell Programming and Scripting

Need your help to get the output of the list in desired format

Hello Guys, I am working on a script and using the below code to fetch the list of all repositories CHDIR='/mnt/scm/subversion/' repolist() { cd ${CHDIR} Repo=`ls|cut -d " " -f1` echo $Repo } Output of the above code is BSB CIB COB DCI DIB DSB ESB-P ESB-TOOLS FareVerify GCACHE GWY... (15 Replies)
Discussion started by: rohit22hamirpur
15 Replies

8. Shell Programming and Scripting

changing the format of the list

I have a file such that List : a, b, c, d I want to list this as List a b c d however the trick is the number of arguments in the list is varies. There might be 0-7 variables . Can you help me? Please use code tags when posting data and code samples! (5 Replies)
Discussion started by: ozum
5 Replies

9. Shell Programming and Scripting

List files and display last modify time in a particular format

hi everyone, can someone suggest how i can list the contents of a directory and display their corresponding last modify time in the format yyyymmddhhmm? thanks in advance! (16 Replies)
Discussion started by: Deanne
16 Replies

10. Shell Programming and Scripting

Help with find command and list in a long format each found file

The purpose of those comands are to find the newest file in a directory acvrdind to system date, and it has to be recursively found in each directory. The problem is that i want to list in a long format every found file, but the commands i use produce unexpected results ,so the output lists in a... (5 Replies)
Discussion started by: alexcol
5 Replies
Login or Register to Ask a Question