Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

How to modify the cat command to highlight a chosen word?

UNIX for Dummies Questions & Answers


Tags
cat, highlight, linux, modification, virtual machine

 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-04-2016
IndigoFox IndigoFox is offline
Registered User
 
Join Date: Jun 2016
Last Activity: 4 June 2016, 10:26 AM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
How to modify the cat command to highlight a chosen word?

Hi,

I'm fairly new to programming and was just wondering... Is it possible to modify the cat command (using a linux virtual machine) so that it displays text of a file and highlights a specifically chosen word? I've been looking for how to do it on the internet and can't find any helpful suggestions...

Thanks. Linux
Sponsored Links
    #2  
Old Unix and Linux 06-04-2016
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 18 October 2017, 10:48 AM EDT
Location: Aachen, Germany
Posts: 11,474
Thanks: 310
Thanked 3,557 Times in 3,274 Posts
I'm afraid, no. cat does exactly this:
Quote:
cat - concatenate files and print on the standard output
You CAN make it show some special chars, too, but that's it.

BTW, "highlighting" is quite fuzzy an action. What is "highlight"? Print in bold? in red? underlined? Do it when outputting to screen? Printing? Sending it with mail, or putting it in an HTML file? All those MAY have different mechanisms to highlight.

You could do some text modifications with e.g. sed, e.g. when outputting to a VT102 (ECMA-48) compatible terminal:


Code:
sed "s/AM0./$BOLD&$OFF/" file
000000000001AM00 1500895700000000000199
000000000001AM01 035000000000013000399820810000000P

Sponsored Links
    #3  
Old Unix and Linux 06-04-2016
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 18 October 2017, 8:52 AM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,610
Thanks: 106
Thanked 1,593 Times in 1,174 Posts
Quote:
Originally Posted by IndigoFox View Post
Is it possible to modify the cat command (using a linux virtual machine) so that it displays text of a file and highlights a specifically chosen word?
Despite RudiC has already come up with some rather elaborate workarounds: the short answer is you can't. The reason is (as RudiC also mentioned) that cat is intended for a different purpose.

Your questions amounts to "can i modify a hammer so that it drills holes". Yes, maybe, somehow, but why not use a drilling machine instead, which will be better suited for that from the start?

You can use sed as mentioned to decorate certain words or phrases with highlighting code, but that is quirky at best and certainly not very portable (chances are it won't work in the same way on another system). If you just want to search for something and want to have it easier to see the found pieces use the grep facility. In your Linuy system the grep has a --color option which should do what you want. If memory serves right this is even on by default. Otherwise cosult the man page of it.

Moderator's Comments:
How to modify the cat command to highlight a chosen word? And, by the way: please do NOT post in the "Answers to Frequently Asked Questions" forum if you are not doing exactly that - write an essay about some frequently asked question. I have moved your thread to a more fitting forum. Thanks for your consideration.


I hope this helps.

bakunin
    #4  
Old Unix and Linux 06-04-2016
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 18 October 2017, 9:58 AM EDT
Posts: 1,645
Thanks: 46
Thanked 626 Times in 587 Posts
If your system has support for it, using `it' as an example:


Code:
grep --color=auto -E 'it|' indigofox.txt


Code:
I'm fairly new to programming and was just wondering...
Is it possible to modify the cat command (using a linux virtual machine)
so that it displays text of a file and highlights a specifically chosen word?
I've been looking for how to do it on the internet and
can't find any helpful suggestions...

Sponsored Links
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to modify the script to include cat function evelibertine UNIX for Dummies Questions & Answers 2 10-24-2012 12:19 PM
get chosen value from bash menu olearydc Shell Programming and Scripting 9 08-19-2012 02:17 PM
cat in the command line doesn't match cat in the script shira Shell Programming and Scripting 21 01-28-2009 10:15 AM
cat/delete per line any word "192.168.1.12" carnegiex Shell Programming and Scripting 1 08-18-2008 01:27 AM
how to move word by word on command line pbsrinivas UNIX for Dummies Questions & Answers 1 11-23-2007 05:17 AM



All times are GMT -4. The time now is 10:50 AM.