[SLL] grep and wc help needed

Jeremy C. Reed reed at reedmedia.net
Sat Feb 21 13:44:39 PST 2009


On Sat, 21 Feb 2009, Ralph Sims wrote:

> (or perl, or ...)
> 
> I have a text file that contains alpha characters as well as puncuation 
> parks. I tried grep [:alpha:] filename |wc but still get the punction 
> marks counted. I've also used [aZ-zZ] and still get the same result.  
> What I'm looking for is a way to count the letters and words in a file 
> without punctuation, spaces, etc.

grep is matching per line.

You can split the file up into words first, like with "fmt 1", or split up 
into letters, like with "fold -w 1", before grepping. Also strip out the 
punctuation and non-alpha first too, like with tr -d whatever,

You could then use "sort | uniq -c | sort -rn" to count the words (or 
letters) and not even use grep.

Hope this helps. If you need further details, show a few lines of input 
text and the expected output you want.

  Jeremy C. Reed

echo 'EhZ[h ^jjf0%%h[[Zc[Z_W$d[j%Xeeai%ZW[ced#]dk#f[d]k_d%' | \
  tr            '#-~'            '\-.-{'




More information about the linux-list mailing list