Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Character count for each string in a 'find' output

Status
Not open for further replies.

czarj

Technical User
Apr 22, 2004
130
US
I need to generate the character count for each string (full path and filename excluding the ./) generated by the find command.

find . -name "*" -print
./long.sh
./.snapshot/sv_hourly.0
./.snapshot/sv_hourly.0/.ssh/known_hosts
./.snapshot/sv_hourly.0/WINDOWS

Any help would be appreciated.

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Hi,
You can make use of xargs and wc -c with find command. Store in array and remove two characters for './'.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
I am able to do it with perl functions, but I am currently having a problem writing the data to a file. Can anyone tell why the data doesn't write to the file? There are no error message and if I remove "FILE" from the print line the data will print to the screen just fine.

----------------------------------------------------------
# Set variables
my $dir = shift;
my $file = "/tmp/length.txt";
my $tchfl = `touch $file`;

# Create data file
system $tchfl;

# Pipe find output to an array
@dataarray = `find $dir -name "*"` ;

# Calculate length of each find string and ouput to file
open (FILE, '>>$file') or die "YIKES, error opening file $file: $!";
foreach (@dataarray) {
chomp;
$CharLength = length($_);
print FILE "$CharLength $_\n";
};
close (FILE) or die;
----------------------------------------------------------

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
czarj,

Try
Code:
open (FILE, [b]"[/b]>>$file[b]"[/b]) or die "YIKES, error opening file $file: $!";

Also is FILE reserved word in PERL? Use some other name for filehandle than 'FILE'.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
This works:

# Set command line argument variable
my $dir = shift;

# Pipe find output to an array
@dataarray = `find $dir -name "*"` ;

# Calculate length of each find string and ouput to file
open (FILE, '>>/tmp/length.txt') or die "YIKES, error opening file $file: $!";
foreach (@dataarray) {
chomp;
$CharLength = length($_);
print FILE "$CharLength $_\n";
};
close (FILE) or die;


--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
czarj,

This is because your $file is not interpolated/recognized inside single quote. If you want to use the variable instead of hardcore path use the double quotes as shown in my previous post.


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top