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 7644067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:35:52+00:00 2026-05-31T09:35:52+00:00

I have the following code where I have collected all the file sizes greater

  • 0

I have the following code where I have collected all the file sizes greater than 40k from my system. I have stored all of this info into a text file. I need to process the file to read the number of times each entry is found in the text file and delete all of those entries.
I have the following code but it does not seem to to working properly.

 #! /bin/sh
  rm -rf /home/b/Desktop/CalcfileSizeGreater40.txt
  filename="/home/b/Desktop/fileSizeGreater40.txt"   
  cat $filename | while read line
    do
  number_of_times=`cat $filename | grep $line | wc -l`
  echo $line:$number_of_times
  echo $line : $number_of_times >> /home/b/Desktop/CalcfileSizeGreater40.txt
  sed '/$line/d' $filename  >tmp
  mv tmp $filename
  done

When I look at the CalcfileSizeGreater40.txt i can see

 131072 : 4
 65553 : 9
 65553 : 9
 65553 : 9
 65553 : 9
 65553 : 9
 65553 : 9
131072 : 4
 65553 : 9
 65553 : 9
 65553 : 9

any ideas as to where I am going wrong ?

  • 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-05-31T09:35:54+00:00Added an answer on May 31, 2026 at 9:35 am

    You can simplify this line:

    number_of_times=`cat $filename | grep $line | wc -l`
    

    to:

    number_of_times=$(grep -c "$line" "$filename")
    

    The use of $(...) in place of back-quotes is extra beneficial when you need to nest command execution. You can count occurrences with grep, and you never needed to use cat. It is a good idea to get into the habit of enclosing file names in variables in double quotes just in case the file names end up with spaces in them.

    Editing the file that you are using cat on is not a good idea. Because of the way you are operating, the initial cat will echo every line of the original file in turn, completely ignoring any changes you make to a (different) file of the same name with the editing commands. This is why some of your names showed up a lot in the output.

    However, what you are basically trying to do is count the number of occurrences of each line in the file. This is conventionally done with:

    sort "$filename" |
    uniq -c
    

    The sort groups all identical sets of lines together in the file, and uniq -c counts the number of occurrences of each distinct line. It does, however, output the count before the line, so that has to be reversed — we can use sed for that. So, your script could be just:

    sizefile="/home/b/Desktop/CalcfileSizeGreater40.txt"
    rm -f "$sizefile"
    filename="/home/b/Desktop/fileSizeGreater40.txt"
    sort "$filename" |
    uniq -c |
    sed 's/^[     ]*\([0-9][0-9]*\)[     ]\(.*\)/\2 : \1/' > "$sizefile"
    

    I’d be cautious about using rm -fr on your CalcfileSizeGreater40.txt; rm -f is sufficient for a file, and you probably don’t want to remove stuff if it isn’t a file but is a directory.

    One pleasant side effect of this is that the code is a lot more efficient than the original as it makes one pass through the file (unless it is so big that sort has to split it up to handle it).


    I am finding the sed code a little difficult to follow.

    I should have explained that the [ ] bits are meant to represent a blank and a tab. On my machine, it appears that uniq only generates spaces, so you could simplify that to:

    sed 's/^ *\([0-9][0-9]*\) \(.*\)/\2 : \1/'
    

    The regex looks for the start of a line, any number of blanks, and then a number (which it remembers as \1 because of the \(...\) enclosing it), followed by a space and then ‘everything else’, which is also remembered (as ‘\2’). The replacement then prints the ‘everything else’ followed by a space, colon, space and the count.

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

Sidebar

Related Questions

Would someone please take a look at the following code fragments (all from one
I have the following code that returns rows from a database when a form
ich have a xml file with the following structure: <layer1 name=this is layer1> <messages>
I have the following code: Tag.find_all_by_company_id(4).each.collect{|tag| tag.name }.join(,) (Essentially I'm trying to build a
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section
I have following code in my application: // to set tip - photo in
I have following code in my Application. Comments in my code will specify My
I have following code in my application. [self.navigationController pushViewController:x animated:YES]; It will push a
I have following code class User attr_accessor :name end u = User.new u.name =

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.