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

  • Home
  • SEARCH
  • 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 8775865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:54:42+00:00 2026-06-13T18:54:42+00:00

I have written the following filter as a function in my ~/.bash_profile: hilite() {

  • 0

I have written the following filter as a function in my ~/.bash_profile:

hilite() {
 export REGEX_SED=$(echo $1 | sed "s/[|()]/\\\&/g")
 while read line
 do
  echo $line | egrep "$1" | sed "s/$REGEX_SED/\x1b[7m&\x1b[0m/g"
 done
 exit 0
}

to find lines of anything piped into it matching a regular expression, and highlight matches using ANSI escape codes on a VT100-compatible terminal.

For example, the following finds and highlights the strings bin, U or 1 which are whole words in the last 10 lines of /etc/passwd:

tail /etc/passwd | hilite "\b(bin|[U1])\b"

However, the script runs very slowly as each line forks an echo, egrep and sed.

In this case, it would be more efficient to do egrep on the entire input, and then run sed on its output.

How can I modify my function to do this? I would prefer to not create any temporary files if possible.

P.S. Is there another way to find and highlight lines in a similar way?

  • 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-13T18:54:43+00:00Added an answer on June 13, 2026 at 6:54 pm

    sed can do a bit of grepping itself: if you give it the -n flag (or #n instruction in a script) it won’t echo any output unless asked. So

    while read line
     do
      echo $line | egrep "$1" | sed "s/$REGEX_SED/\x1b[7m&\x1b[0m/g"
     done
    

    could be simplified to

    sed -n "s/$REGEX_SED/\x1b[7m&\x1b[0m/gp"
    

    EDIT:
    Here’s the whole function:

    hilite() { 
        REGEX_SED=$(echo $1 | sed "s/[|()]/\\\&/g");
        sed -n "s/$REGEX_SED/\x1b[7m&\x1b[0m/gp"
    }
    

    That’s all there is to it – no while loop, reading, grepping, etc.

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

Sidebar

Related Questions

I have written the following algorithm in order to evaluate a function in MatLab
I have written following PHP code: $input=menu=1&type=0&; print $input.<hr>.ereg_replace('/&/', ':::', $input); After running above
I have lots of functions of the following template: function (field, filter){ var q
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];
I am trying my hands on WPF MVVM. I have written following code in
i have written the following code class Program { static void Main(string[] args) {
I have written the following code, CrystalDecisions.CrystalReports.Engine.ReportDocument report = new CrystalDecisions.CrystalReports.Engine.ReportDocument(); report.Load(@C:\Users\XXX\Desktop\Backup1\Project\ReportsFolder\ReportSalesInvoice.rpt); Report works
I have written the following query, but in the WHERE clause I have used
I have written the following constraint for a column I've called 'grade': CONSTRAINT gradeRule
I have written the following Utility class to get an instance of any class

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.