|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 -cIt 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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
2As 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.. |
| Sponsored Links | ||
|
![]() |
| Tags |
| exec |
| Thread Tools | Search this Thread |
| 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 |
|
|