text file — search via regex, bold the results in order to physically print out

Posted on

Problem :

i can’t think off the top of my head an app which will allow me to do this.

i have a document with hundreds of lines, i want to search via a regular expression and either bold or italicise the hits. i have no problem with the regex or anything, just can’t think of an app that allows this.

i generally grep/sed/awk or notepad++ in windows, but this of course doesn’t allow me to bold the results. grep does, but i need to print as well…

Solution :

You’re on Windows, I’m not sure if LPT: can be made to understand old “ANSI.SYS” -style command sequences or similar “escape sequences” within text/documents to be printed.

Also: You didn’t tell if it is TEXT-FILE documents or WORD documents you work with.

Here is an example of how to use sed to make “0d” and “0a” (CR and LF) stand out in pure text in a Linux bash shell.
Note: od with the parameters shown prints byte values in hex and the ascii representation of the same.

$ cat firstfile
1. The quick brown fox jumped over the lazy dog.

$ od -t x1z -w24 firstfile | sed -re 's/(0a)/x1b[1m1x1b[m/g;s/(0d)/x1b[1m1x1b[m/g'
0000000 31 2e 20 54 68 65 20 71 75 69 63 6b 20 62 72 6f 77 6e 20 66 6f 78 20 6a  >1. The quick brown fox j<
0000030 75 6d 70 65 64 20 6f 76 65 72 20 74 68 65 20 6c 61 7a 79 20 64 6f 67 2e  >umped over the lazy dog.<
0000060 0a                                                                       >.<
0000061

note: The last “0a” should be bold, this doesn’t happen here, seems not be possible.

The x1b[1m and x1b[m things you see embedded are the sequences required to make the Ubuntu Terminal print bold and then turn it off. These come from the terminfo database and can be generated more generally by tput bold and tput sgr 0 as in:

$ tput bold | od -t x1z -w24 
0000000 1b 5b 31 6d                                                              >.[1m<
0000004

$ tput sgr 0 | od -t x1z -w24 
0000000 1b 28 42 1b 5b 30 6d                                                     >.(B.[0m<
0000007

Try

$ export GREP_COLORS='ms=01;33'
$ grep -C 1000 '<abc>' file.txt

See Linux and Unix grep command help and examples

If you’re willing to import your text into Microsoft Word for this purpose,
and if Word’s search capabilities are powerful enough for you, then you can do this with Word. 
(Word can search for glob-like wildcards, but not true regular expressions.) 
In the “Find and Replace” dialog, click on “More >>”, then click in the “Replace with” field,
then click on “Format” -> “Font” and set the font you want to apply to the strings that are found:

            Find and Replace with Font

Leave a Reply

Your email address will not be published. Required fields are marked *