Getting the character count of the last line | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Getting the character count of the last line

UNIX for Dummies Questions & Answers


Tags
exec

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-03-2012
MIA651 MIA651 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 10 April 2014, 12:11 PM EDT
Location: United States
Posts: 28
Thanks: 20
Thanked 1 Time in 1 Post
Getting the character count of the last line

I need the character count of the last line of each file in a directory, and not the total.

Now I have been doing this but unfortunately, -exec doesn't support pipes:

Code:
find sent/ -type f -exec tail -1|wc -c {} \;

If I try this:

Code:
find sent/ -type f -exec tail -1 {} \; | wc -c

It will give me the total count of characters of ALL the last lines of ALL files. I would just like to have it listed for each file, if possible

Anyway around the usage of pipes?
Sponsored Links
    #2  
Old 12-03-2012
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 26 September 2014, 6:37 PM EDT
Location: Galactic Empire
Posts: 3,385
Thanks: 234
Thanked 1,208 Times in 1,134 Posts
Why don't you use a loop:-

Code:
for file in sent/*
do
   tail -1 $file | wc -c 
done

OR

Code:
for file in $( find sent/ -type f )
do
   tail -1 $file | wc -c 
done

OR you can write a script which will tail last line and get character count, then call it in find:-

Code:
# cat char_last.sh
file=$1
echo "$( tail -1 $file | wc -c ) \t $file"


Code:
find sent/ -type f -exec ./char_last.sh {} \;


Last edited by Yoda; 12-03-2012 at 04:31 PM..
The Following User Says Thank You to Yoda For This Useful Post:
MIA651 (12-03-2012)
Sponsored Links
    #3  
Old 12-03-2012
birei birei is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 24 June 2014, 5:36 AM EDT
Location: Bilbao, Spain
Posts: 628
Thanks: 8
Thanked 173 Times in 171 Posts
Hi MIA651,

One way using perl. It prints the file name, a colon and the number of characters in last line:

Code:
$ perl -lne 'if ( eof ) { printf qq|%s: %d\n|, $ARGV, length; }' *
3.txt: 12
AAAB.txt: 10
AAAC.txt: 26
file.data: 4

The Following User Says Thank You to birei For This Useful Post:
MIA651 (12-03-2012)
    #4  
Old 12-03-2012
in2nix4life's Avatar
in2nix4life in2nix4life is offline
Registered User
 
Join Date: Oct 2007
Last Activity: 1 October 2014, 2:40 PM EDT
Location: East Coast
Posts: 611
Thanks: 1
Thanked 176 Times in 162 Posts
Using awk in the find command:


Code:
find sent/ -type f -exec awk 'END{printf("%d\t%s\n",length($0),FILENAME)}' {} \;

Prints out number of characters and then the filename.
The Following User Says Thank You to in2nix4life For This Useful Post:
MIA651 (12-03-2012)
Sponsored Links
    #5  
Old 12-03-2012
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 2 October 2014, 9:01 AM EDT
Location: Amsterdam
Posts: 9,490
Thanks: 281
Thanked 2,399 Times in 2,151 Posts
Try:

Code:
find sent/ -type f -exec tail -1 {} \; | awk '{print length}'

To also count the linefeed at the end of the line, like wc -c or wc -m use awk '{print length+1}'



--
Use wc -c counts bytes, BTW. To count (multibyte) characters use wc -m

Last edited by Scrutinizer; 12-03-2012 at 05:27 PM..
The Following User Says Thank You to Scrutinizer For This Useful Post:
MIA651 (12-03-2012)
Sponsored Links
    #6  
Old 12-03-2012
MIA651 MIA651 is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 10 April 2014, 12:11 PM EDT
Location: United States
Posts: 28
Thanks: 20
Thanked 1 Time in 1 Post
Thank you very much all... From an earlier deleted post, I got the idea and it this is what I've done to have it also print the filename along with it, as you all have got it doing anyway.


Code:
find sent/ -type file | while read FILE;
do
 print $FILE
 tail -1 $FILE| wc -c
done

Sponsored Links
    #7  
Old 12-04-2012
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 2 October 2014, 9:01 AM EDT
Location: Amsterdam
Posts: 9,490
Thanks: 281
Thanked 2,399 Times in 2,151 Posts
This would break if there are file names with spaces and/or special characters. To avoid that you would need to use properly quoting:

Code:
print "$FILE"
tail -1 "$FILE"

Also, wc -c only counts bytes, so with multibyte characters you would end up with the wrong number. To avoid that use wc -m . For example:


Code:
$ echo 'e' | wc -c
       4
$ echo 'e' | wc -m
       3
$ printf "%s" 'e' | wc -m
       2

As you can see, they both count the linefeed at the end, you may or may not want that...



--
The semicolon after while read FILE is not necessary...

Last edited by Scrutinizer; 12-04-2012 at 04:57 AM..
The Following 2 Users Say Thank You to Scrutinizer For This Useful Post:
MIA651 (12-04-2012), Yoda (12-04-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Character count of each line bobbygsk Shell Programming and Scripting 1 11-21-2012 01:22 PM
Count character in one line ambious Shell Programming and Scripting 8 12-31-2011 11:33 AM
Count specific word or character per line janzper UNIX for Advanced & Expert Users 1 05-27-2011 02:42 AM
How to count the occurence of a character in a line sumit207 UNIX for Dummies Questions & Answers 5 02-09-2009 07:10 AM
How to count the occurence of a character in a line sumit207 UNIX for Advanced & Expert Users 1 11-26-2008 08:00 AM



All times are GMT -4. The time now is 10:29 AM.