Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9252943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:05:19+00:00 2026-06-18T11:05:19+00:00

I am trying to make an executable that will take in any number of

  • 0

I am trying to make an executable that will take in any number of text files and give an output that is the distribution of words by number of occurrences. This is to be done in bash scripting, and what I have so far is:

#!/bin/bash
y=$(cat $* | wc -w)

cat $* | tr ' ' '//' |  tr '[:upper:]' '[:lower:]' | tr -d '[:punct:]' | 
grep -v '[^a-z]'| sort | uniq -c | sort -rn | head -$y

I get an error trying to set y and I can’t figure out how to get head to print out every word otherwise.

Is there a better way to print it out?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:05:22+00:00Added an answer on June 18, 2026 at 11:05 am

    Why run head at all? There’s no guarantee that there will be as many words as there are words in the files; indeed, it is practically guaranteed that there won’t be (since there’ll be some repeated words). And if you want to see all the data, then show all the data; don’t filter the output from sort -nr.

    The first tr only needs one slash, I think. Normally, you’d map blanks and punctuation to newlines (with a -s option to tr to squeeze adjacent newlines to one). The slashes from the first tr count as punctuation in the third tr, so it isn’t obvious what you’re up to there. I think I’d expect to see something like:

    cat "$@" |
    tr -cs '[:alpha:]' '\n' |      # Convert any non-alpha character to newline
    tr '[:upper:]' '[:lower:]' |   # Case-convert to lower case
    sort | uniq -c | sort -nr
    

    Note the use of "$@" rather than $*; there’s no difference when the file names you specify don’t contain blanks (newlines, tabs, etc); when they do, the "$@" form is correct and $* is not, so you may as well always use "$@". It is correct far more often than $* is.

    For some C source code I had lying around, the output from the script was:

     246 n
     217 i
     153 int
     141 list
     124 if
     118 t
     103 char
      99 a
      97 size
      90 buffer
      89 context
      82 d
      81 void
      79 include
      79 h
      78 s
      65 for
      62 j
      55 ptr
      54 r
      54 const
      53 static
      53 sem
      51 pthread
      49 z
      49 oldneedle
      49 err
      47 to
      47 return
      46 mutex
      44 printf
      43 error
      43 c
    

    Note that the word ‘h’ appears as often as the word ‘include’; there’s a reason for that! The word t appears a lot, but that’s because, for example, size_t is treated as two words by the filtering. Preserving underscores is possible; change the first tr to use '[:alpha:]_' (note the underscore). You eliminated digits, but you can keep those too if you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make an executable war file ( java -jar myWarFile.war ) that
I'm trying to create a make file which will compile all the .cpp files
So I am trying to write an .sh file that will be executable, this
I am trying to make a program that stores selected file's (specifically: executable's) contents
I'm trying to make an executable using py2exe. My .py uses Tkinter, matplotlib and
I get this error when trying to make the executable, the code compiles correctly
I'm trying make an entity with doctrine that has three associations with other entities
Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
Trying to make a simple number clicker control for BlackBerry 6/7, like this: At
I am trying to create an app that will allow me to stream video

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.