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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:49:51+00:00 2026-06-09T04:49:51+00:00

I am trying to write a shell script that will search for a regular

  • 0

I am trying to write a shell script that will search for a regular expression in each of the files in the current directory without using temp files.

Originally, I did this using a temp file to store echo * | sed 's/ /\n/g' and then looped through each line of this file, using cat on each and then grepping my expression and counting the lines of output. I was having some trouble with temp files being searched and was wondering if I could do everything using variables or some non-temp-files method (I don’t really want to create a separate directory for the temp files either).

The problem I was having with variables was that after I had set the value of the variable to the output of echo * | sed 's/ /\n/g', I didn’t know how to loop through each line so I could get the expression count from the files.

I just want the following to work (where I hardcode the expression):

% ls
% file1 file2 file3
% ./countMost.sh
% file2(28)
% ls
% file1 file2 file3

signifying that file2 has the most instances of the expression (28 of them).

  • 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-09T04:49:52+00:00Added an answer on June 9, 2026 at 4:49 am

    You can try something like this:

    grep -c regex files | sed -e 's/^\(.*\):\(.*\)$/\2 \1/' | sort -r -n | head -n 1
    

    Where regex is your regular expression (can use egrep as well) and the files are your list of files.

    Given 3 files:

    file1:
    qwe
    qwe
    qwe
    asd
    zxc
    
    file2:
    qwe
    asd
    zxc
    
    file3:
    asd
    qwe
    qwe
    qwe
    qwe
    

    and I run:

    grep -c 'qwe' file[1-3] | sed -e 's/^\(.*\):\(.*\)$/\2 \1/' | sort -r -n
    

    I get the output:

    4 file3
    3 file1
    1 file2
    

    Additionally, adding the | head -n 1 at the end only gives me:

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

Sidebar

Related Questions

Background: I'm trying to write a shell script using php that will automatically checkout
I'm trying to write a shell script that will make several targets into several
I'm trying to write a shell script that does a search and replace inside
I am trying to write a bourne-shell script that takes a directory as a
I'm trying to write a shell script that, when run, will set some environment
I'm trying to write a script using Android's shell to rename all files of
I'm trying to write a shell script that will execute a Hadoop MapReduce job
I'm trying to write a VBScript (.vbs) script that uses the WScript.Shell Run() method,
I am trying to write a wrapper shell script that caches information every time
I am trying to write a simple shell-script that prints out the first parameter

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.