5

I've used the below command to count the number of words in a file:

tr ' ' '\n' < Filename | grep -c "WORD"

This returns the word list with counter. Now I want to count the number of words of a particular length. For example, given a file with these contents:

employee paresh emp employee jayesh hitesh

When I run the shell script with argument 6, it should display/count words with 6 characters (paresh, jayesh, and hitesh, with count 3). How can I do that?

2 Answers 2

8

If you grep for the regular expression ^.{6}$ it will return lines with exactly six characters:

$ tr ' ' '\n' < Filename | grep '^.\{6\}$'
paresh
jayesh
hitesh

$ tr ' ' '\n' < Filename | grep -c '^.\{6\}$'
3
5
  • Thanx for the support, it is running fine !! but can you please let me know about this : .\{6\} , that you have used with grep command. Jan 3, 2011 at 7:39
  • what about \ sign before { and } ?? please Jan 3, 2011 at 10:24
  • @Paresh Otherwise grep will look for { and } symbols, but not 6 repetitions of preceeding pattern.
    – alex
    Jan 3, 2011 at 11:50
  • Argh. I accidentally deleted a comment trying to edit it; sorry whoever's that was. @Paresh As that comment pointed out, it's to escape the braces so they're treated as part of the regular expression (X{Y} means "find the X character Y times"); without the escapes it searches for actual braces Jan 4, 2011 at 15:28
  • thanx a lot for such helpful hints and answer. Thanx once again Jan 5, 2011 at 6:04
3

Also,

awk -v n=6 '{for (i=1; i<=NF; i++) if (length($i) == n) print $i}' file

or, with tr

tr ' ' '\n' file | awk -v n=6 'length($0)==n'
4
  • Or just use NF, you don't need to iterate to find the number of fields.
    – Chris Down
    Dec 11, 2012 at 16:24
  • Sorry @ChrisDown, I don't understand your comment. Use NF where? Dec 11, 2012 at 16:56
  • Oh, apologies, I misunderstood the intent of the question (misread as 6 fields rather than six characters in a field).
    – Chris Down
    Dec 11, 2012 at 17:02
  • awk '{ print length($NF) }' filename | sort | uniq -c was useful to count word sizes in last column of a file. thanks
    – roblogic
    Nov 10, 2017 at 0:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.