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


 
Thread Tools Search this Thread
# 1  
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. Smilie
# 2  
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

# 3  
Quote:
Originally Posted by IndigoFox
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:
Mod Comment 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  
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...

 

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Highlight 'comm' command output
muppets
Given the output below (simplified) extracted from the comparison of two curl -I commands saved in two different files, I am looking for the best approach to highlight the following scenarios in a script: this header exists only in file1.txt but this one does not this one exists in both cases...... Shell Programming and Scripting
1
Shell Programming and Scripting
How to modify the script to include cat function
evelibertine
I have the following script that greps lines containing "AT" from data files data1.hsq through data1000.hsq, then cuts their second column and puts in data files called perm1 through perm1000. I want to modify the script so that instead of putting the data in separate data files perm1 through...... UNIX for Dummies Questions & Answers
2
UNIX for Dummies Questions & Answers
cat in the command line doesn't match cat in the script
shira
Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole...... Shell Programming and Scripting
21
Shell Programming and Scripting
cat/delete per line any word "192.168.1.12"
carnegiex
Hi All Can u help me.. My problem is delete word per line sample: cat /tmp/file.txt monitor 192.168.1.11 Copying files in current directory 1 monitor 192.168.1.1 Copying files in current directory 2 monitor 192.168.1.12 Copying files in current directory 3 monitor 192.168.1.14...... Shell Programming and Scripting
1
Shell Programming and Scripting
how to move word by word on command line
pbsrinivas
Hey All, On commad promt of a shell.. How can we move our cursor word by word. Like Ctrl+A takes to the starting of the command... Any shortcut like that..? Thanks pbsrinivas... UNIX for Dummies Questions & Answers
1
UNIX for Dummies Questions & Answers

Featured Tech Videos