LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-28-2023, 06:21 AM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
Highlight matched pattern upon string


I would like to have a bash function that highlights a matched pattern upon a string.

Code:
cregex ()
 { 
  local ptrn="$1"
  local string="$2"
  
  local -r rst="$( tput sgr0 )"
  local -r blu="$( tput bold; tput setaf 39 )"
  
  echo "$string" | sed -E 's/$ptrn/${blu}${ptrn}${rst}/'
 }

But the function in not behaving as I would wish.

For instance

Code:
cregex "tput" "tput bold; tput setaf 39"
does not highlight the word "tput"
 
Old 03-28-2023, 06:42 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,966

Rep: Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332
grep --color tput <<<'erfa tput f r tput tput adf'
 
Old 03-28-2023, 06:58 AM   #3
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Would this allow a user to specify any pattern and any text, so that the matched text is highlighted? Would the use of "<<<" redirection suffer from code injection vulnerabilities?

Last edited by Faki; 03-28-2023 at 07:01 AM.
 
  


Reply

Tags
bash, sed



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed command to display only the matched pattern and not the whole line divya0102 Programming 8 06-12-2014 01:39 AM
[SOLVED] sed and how to remove all lines after matched pattern transmutated Programming 5 06-13-2012 07:54 AM
[SOLVED] replace matched pattern from 1st file into 2nd file using awk sopier Programming 6 12-13-2011 09:58 AM
How to grep string in tar.gz file with matched filenames? beeth2006 Linux - Newbie 3 09-29-2011 10:00 AM
Inputting with sed, based on matched pattern mike9287 Programming 5 07-20-2006 07:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration