The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
What the command to find out the record length of a fixed length file? tranq01 UNIX for Dummies Questions & Answers 9 12-04-2008 04:04 PM
Change All File Names in a Directory andou UNIX for Dummies Questions & Answers 8 01-22-2008 09:06 PM
Replace characters in all file names in a particular directory madhunk Shell Programming and Scripting 4 02-16-2006 07:10 PM
directory names in a flat file surjyap Shell Programming and Scripting 2 10-06-2005 08:51 AM
File and Directory Names become hidden dbinsol1 UNIX for Advanced & Expert Users 10 05-29-2002 10:58 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-02-2008
koti_rama koti_rama is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 50
find the length of file names in a directory?

Hi,
how can find length of file names in a directory.

Examp:

I have a directory with name "d1".
directory: d1

files:
aaa.log
bbb.log
abcd.log
abcdef.log

I wold like out put like:
file name legnth
aaa.log 3
bbb.log 3
abcd.log 4
abcdef.log 5

Thanks
  #2 (permalink)  
Old 06-02-2008
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
using bash:
Code:
for file in *
do
    ff=`echo $file | cut -d '.' -f1`
    echo -n "$ff "
    echo ${#ff}
done
  #3 (permalink)  
Old 06-02-2008
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
  
 

Join Date: Apr 2002
Location: Italy
Posts: 370
Code:
ls | awk '$(NF+1)=length'

Last edited by robotronic; 06-02-2008 at 11:40 AM..
  #4 (permalink)  
Old 06-02-2008
linuxbynight linuxbynight is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 6
Script to get file name length discarding dot extension

If I understand your question, the following should work or you can modify it to do what you need.


for i in `ls -1`
do
val=`echo $i | cut -d '.' -f1 | wc | tr '\t' ' ' | tr -s ' ' | sed 's/^ //' | cut -d' ' -f 3`
val=`/usr/bin/expr $val - 1`
echo $i $val
done
  #5 (permalink)  
Old 06-02-2008
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
  
 

Join Date: Apr 2002
Location: Italy
Posts: 370
If you want to exclude the file extension from the character count, try this instead:

Code:
ls | awk -F'.' '{ print($0, NF>1?length-length($NF)-1:length) }'
  #6 (permalink)  
Old 06-04-2008
linuxbynight linuxbynight is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 6
Robo - your method

is more elegant. In my case, I just never took the time to learn awk. Thanks for the alternative.
Closed Thread

Bookmarks

Tags
awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:57 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0