1

I'm given a test file and am supposed to be able to count the words in a specific column of a file. The catch is that there are some lines that contain only numbers.

I know that the wc will give the word count of something - but if I remember correctly it does not distinguish between numbers and actual words (so a file with 0184674673 HELLO would give a word count of two) - is there some non-convoluted way (short of going through each line in the column, checking if the word has any numbers, and incrementing a counter if not) of solving this with a command or no?

If you wanted to see a snippet of the file I've been given, here:

disobediently RESINY GRAPHICS lownesses prickers intractabiliti es 85790227 villainously MINIS blinkering applicants TORPIDITIES subtexts apportioned carded electrocardiograph SINTERED FOOTSORE ENTHRALMENTS Sherpa FLAN OVERRULES NEWSREADER 15318116 PUTRIDNESS EXECUTIONAL vanguard LUCENTLY CONGRUENTLY 14117187 pretending misshapes cowslip 18714723 JUDGES INTERNATIONALIZING DISCUSSES 85192973 quorate shirking SECURES scrofula exclude NUCLIDE shipwrecks prologuing indelicacy NUTRITIONS decompresses manlike aggregately NEGOTIANT chewy Egypt bloodsports STOREYS worthier BELLOWING HAEMATIN UNCONTROLLED SUFFERER CLOPPING DUALLY 5363130 DISCOMMODING ENTRANCED brilliantness changeableness driest uncouth abjectnesses grumpiness ache 94854804 JETSAMS barbarousness REPOSSESSIONS INCLINATION Jardine AUTHORISED parading ties Hillyer USHER COMPLIANCES disdainful 98908803 CANDIDACY Rostov titrates DICTIONARIES optimists luted WART RAPINE 94683675 cannibal hostilely KALI ADMIRATIONS 95714958 AMPUTATED 65196125 VIEWFINDER uprated narrowing disavowing ALPINES Stahl HEELLESS feminises LUCKINESSES patriarchate anticommunism

1

If you are not restricted to wc, you can filter out the numbers with a tool like sed and then count the words using wc.

Given your text (in testfile) will give:

$ sed -e s/[0-9]*//g testfile

disobediently RESINY GRAPHICS lownesses prickers intractabiliti es villainously MINIS blinkering applicants TORPIDITIES subtexts apportioned carded electrocardiograph SINTERED FOOTSORE ENTHRALMENTS Sherpa FLAN OVERRULES NEWSREADER PUTRIDNESS EXECUTIONAL vanguard LUCENTLY CONGRUENTLY pretending misshapes cowslip JUDGES INTERNATIONALIZING DISCUSSES quorate shirking SECURES scrofula exclude NUCLIDE shipwrecks prologuing indelicacy NUTRITIONS decompresses manlike aggregately NEGOTIANT chewy Egypt bloodsports STOREYS worthier BELLOWING HAEMATIN UNCONTROLLED SUFFERER CLOPPING DUALLY DISCOMMODING ENTRANCED brilliantness changeableness driest uncouth abjectnesses grumpiness ache JETSAMS barbarousness REPOSSESSIONS INCLINATION Jardine AUTHORISED parading ties Hillyer USHER COMPLIANCES disdainful CANDIDACY Rostov titrates DICTIONARIES optimists luted WART RAPINE cannibal hostilely KALI ADMIRATIONS AMPUTATED VIEWFINDER uprated narrowing disavowing ALPINES Stahl HEELLESS feminises LUCKINESSES patriarchate anticommunism

The regex pattern I used has the drawback that the whitespaces after the numbers are not removed, but this seems not to matter for counting with wc.

You can count the words directly with wc after the filtering with the pipe.

$ sed -e s/[0-9]*//g testfile | wc
  2     104    1035
2
  • I am not restricted to wc! I honestly had not thought of this, but it makes things a lot easier than my method, I'll try it out! – secondubly Apr 11 '16 at 23:27
  • Edit - I used your command (I had to cut the specific column I was reading from) but it worked like a charm! I went ahead and marked your answer as the correct one. – secondubly Apr 11 '16 at 23:34

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.