Skip to content

Commit c71f12e

Browse files
added post for coloring matched portions in the terminal
1 parent 688c5d1 commit c71f12e

File tree

21 files changed

+95
-13
lines changed

21 files changed

+95
-13
lines changed

100-page-python-intro-book-announcement/index.html

+1-1
Large diffs are not rendered by default.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ I'm addicted to reading fantasy/sci-fi books, so I have a [blog](https://learnby
66

77
## Posts
88

9+
* [Coloring matched portions with GNU grep, sed and awk](https://learnbyexample.github.io/coloring-matched-portions-grep-sed-awk/)
910
* [100 Page Python Intro book announcement](https://learnbyexample.github.io/100-page-python-intro-book-announcement/)
1011
* [Festive offers for books on Python, Linux, Regular Expressions, Vim and more!](https://learnbyexample.github.io/programming-deals-2024/)
1112
* [Interactive Python Exercises and Quiz](https://learnbyexample.github.io/interactive-python-exercises/)

atom.xml

+58-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,65 @@
55
<link href="https://learnbyexample.github.io/atom.xml" rel="self" type="application/atom+xml"/>
66
<link href="https://learnbyexample.github.io"/>
77
<generator uri="https://www.getzola.org/">Zola</generator>
8-
<updated>2025-01-09T00:00:00+00:00</updated>
8+
<updated>2025-01-13T00:00:00+00:00</updated>
99
<id>https://learnbyexample.github.io/atom.xml</id>
10+
<entry xml:lang="en">
11+
<title>Coloring matched portions with GNU grep, sed and awk</title>
12+
<published>2025-01-13T00:00:00+00:00</published>
13+
<updated>2025-01-13T00:00:00+00:00</updated>
14+
<link rel="alternate" href="https://learnbyexample.github.io/coloring-matched-portions-grep-sed-awk/" type="text/html"/>
15+
<id>https://learnbyexample.github.io/coloring-matched-portions-grep-sed-awk/</id>
16+
<content type="html">&lt;p&gt;You might already know how to use the &lt;code&gt;--color&lt;&#x2F;code&gt; option to highlight matched portions with &lt;code&gt;GNU grep&lt;&#x2F;code&gt;. In this post, you&#x27;ll see how to use ANSI escape sequences to format matched portions with &lt;code&gt;GNU sed&lt;&#x2F;code&gt; and &lt;code&gt;GNU awk&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
17+
&lt;span id=&quot;continue-reading&quot;&gt;&lt;&#x2F;span&gt;&lt;br&gt;
18+
&lt;h2 id=&quot;gnu-grep&quot;&gt;GNU grep&lt;a class=&quot;zola-anchor&quot; href=&quot;#gnu-grep&quot; aria-label=&quot;Anchor link for: gnu-grep&quot;&gt;🔗&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
19+
&lt;p&gt;Consider this sample input file:&lt;&#x2F;p&gt;
20+
&lt;pre style=&quot;background-color:#f5f5f5;color:#1f1f1f;&quot;&gt;&lt;code&gt;&lt;span&gt;$ cat fruits.txt
21+
&lt;&#x2F;span&gt;&lt;span&gt;banana mango cherry pineapple
22+
&lt;&#x2F;span&gt;&lt;span&gt;grape fig apple dragonfruit papaya
23+
&lt;&#x2F;span&gt;&lt;span&gt;watermelon cashew tomato orange
24+
&lt;&#x2F;span&gt;&lt;span&gt;almond lime grapefruit walnut
25+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
26+
&lt;p&gt;The output for &lt;code&gt;grep --color -wE &#x27;[ago]\w+&#x27; fruits.txt&lt;&#x2F;code&gt; is shown below:&lt;&#x2F;p&gt;
27+
&lt;p align=&quot;center&quot;&gt;&lt;img src=&quot;&#x2F;images&#x2F;grep_color.png&quot; alt=&quot;Example for displaying matched portions in color with GNU grep&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
28+
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;info.svg&quot; alt=&quot;info&quot; &#x2F;&gt; See &lt;a href=&quot;https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;learn_gnugrep_ripgrep&#x2F;frequently-used-options.html#colored-output&quot;&gt;this section&lt;&#x2F;a&gt; from my ebook on &lt;code&gt;GNU grep&lt;&#x2F;code&gt; for more details about this option. &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;BurntSushi&#x2F;ripgrep&quot;&gt;ripgrep&lt;&#x2F;a&gt; has a more featured support for color formatting, see &lt;a href=&quot;https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;learn_gnugrep_ripgrep&#x2F;ripgrep.html#colored-output&quot;&gt;this section&lt;&#x2F;a&gt; for an example.&lt;&#x2F;p&gt;
29+
&lt;br&gt;
30+
&lt;h2 id=&quot;formatting-with-ansi-escape-sequences&quot;&gt;Formatting with ANSI escape sequences&lt;a class=&quot;zola-anchor&quot; href=&quot;#formatting-with-ansi-escape-sequences&quot; aria-label=&quot;Anchor link for: formatting-with-ansi-escape-sequences&quot;&gt;🔗&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
31+
&lt;p&gt;Here are some examples to show how you can format text in the terminal using ANSI escape sequences:&lt;&#x2F;p&gt;
32+
&lt;p align=&quot;center&quot;&gt;&lt;img src=&quot;&#x2F;images&#x2F;ANSI_escape_sequences_formatting.png&quot; alt=&quot;Examples for formatting with ANSI escape sequences&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
33+
&lt;p&gt;Your choice of formatting goes between &lt;code&gt;\033[&lt;&#x2F;code&gt; and &lt;code&gt;m&lt;&#x2F;code&gt;. In the above example, &lt;code&gt;01&lt;&#x2F;code&gt; is for bold and &lt;code&gt;31&lt;&#x2F;code&gt; is for the color red. Multiple formats can be specified by separating the parameters with a semicolon. Using &lt;code&gt;0&lt;&#x2F;code&gt; turns off the format (otherwise, it will persist in the current terminal session until turned off).&lt;&#x2F;p&gt;
34+
&lt;p&gt;See also:&lt;&#x2F;p&gt;
35+
&lt;ul&gt;
36+
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;q&#x2F;4842424&#x2F;4082052&quot;&gt;List of ANSI color escape sequences&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
37+
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;unix.stackexchange.com&#x2F;q&#x2F;148&#x2F;109046&quot;&gt;Colorizing your terminal and shell environment&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
38+
&lt;&#x2F;ul&gt;
39+
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;info.svg&quot; alt=&quot;info&quot; &#x2F;&gt; Note that you can also use &lt;code&gt;\e&lt;&#x2F;code&gt; instead of &lt;code&gt;\033&lt;&#x2F;code&gt; in the above examples. However, that will not work with the &lt;code&gt;GNU awk&lt;&#x2F;code&gt; examples shown below.&lt;&#x2F;p&gt;
40+
&lt;br&gt;
41+
&lt;h2 id=&quot;gnu-sed&quot;&gt;GNU sed&lt;a class=&quot;zola-anchor&quot; href=&quot;#gnu-sed&quot; aria-label=&quot;Anchor link for: gnu-sed&quot;&gt;🔗&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
42+
&lt;p&gt;&lt;code&gt;GNU sed&lt;&#x2F;code&gt; doesn&#x27;t have a native support for the escape sequences discussed above. Instead, you can store the sequences in a shell variable using &lt;a href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;bash&#x2F;manual&#x2F;bash.html#ANSI_002dC-Quoting&quot;&gt;ANSI-C Quoting&lt;&#x2F;a&gt; and use them where required. Here&#x27;s an example: &lt;&#x2F;p&gt;
43+
&lt;p align=&quot;center&quot;&gt;&lt;img src=&quot;&#x2F;images&#x2F;sed_color.png&quot; alt=&quot;Example for displaying matched portions in color with GNU sed&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
44+
&lt;br&gt;
45+
&lt;h2 id=&quot;gnu-awk&quot;&gt;GNU awk&lt;a class=&quot;zola-anchor&quot; href=&quot;#gnu-awk&quot; aria-label=&quot;Anchor link for: gnu-awk&quot;&gt;🔗&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
46+
&lt;p&gt;With &lt;code&gt;GNU awk&lt;&#x2F;code&gt;, you can directly embed the ANSI escape sequences in a string. Here&#x27;s an example:&lt;&#x2F;p&gt;
47+
&lt;p align=&quot;center&quot;&gt;&lt;img src=&quot;&#x2F;images&#x2F;awk_color.png&quot; alt=&quot;Example for displaying matched portions in color with GNU awk&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
48+
&lt;p&gt;Here&#x27;s a field processing example:&lt;&#x2F;p&gt;
49+
&lt;pre style=&quot;background-color:#f5f5f5;color:#1f1f1f;&quot;&gt;&lt;code&gt;&lt;span&gt;$ cat marks.txt
50+
&lt;&#x2F;span&gt;&lt;span&gt;Dept Name Marks
51+
&lt;&#x2F;span&gt;&lt;span&gt;ECE Raj 53
52+
&lt;&#x2F;span&gt;&lt;span&gt;ECE Joel 62
53+
&lt;&#x2F;span&gt;&lt;span&gt;EEE Moi 68
54+
&lt;&#x2F;span&gt;&lt;span&gt;CSE Surya 81
55+
&lt;&#x2F;span&gt;&lt;span&gt;EEE Tia 59
56+
&lt;&#x2F;span&gt;&lt;span&gt;ECE Om 92
57+
&lt;&#x2F;span&gt;&lt;span&gt;CSE Amy 67
58+
&lt;&#x2F;span&gt;&lt;span&gt;
59+
&lt;&#x2F;span&gt;&lt;span&gt;$ cat filter.txt
60+
&lt;&#x2F;span&gt;&lt;span&gt;ECE 70
61+
&lt;&#x2F;span&gt;&lt;span&gt;EEE 65
62+
&lt;&#x2F;span&gt;&lt;span&gt;CSE 80
63+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
64+
&lt;p align=&quot;center&quot;&gt;&lt;img src=&quot;&#x2F;images&#x2F;awk_color_field_processing.png&quot; alt=&quot;Color field processing results with GNU awk&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
65+
</content>
66+
</entry>
1067
<entry xml:lang="en">
1168
<title>100 Page Python Intro book announcement</title>
1269
<published>2024-12-19T00:00:00+00:00</published>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!doctype html><html lang=en><head><meta content="IE=edge" http-equiv=X-UA-Compatible><meta content="text/html; charset=utf-8" http-equiv=content-type><meta content="width=device-width,initial-scale=1.0,maximum-scale=1" name=viewport><title>Coloring matched portions with GNU grep, sed and awk</title><link href=https://learnbyexample.github.io/atom.xml rel=alternate title=RSS type=application/atom+xml><script src=https://cdnjs.cloudflare.com/ajax/libs/slideout/1.0.1/slideout.min.js></script><link href=https://learnbyexample.github.io/site.css rel=stylesheet><meta content="Coloring matched portions with GNU grep, sed and awk" property=og:title><meta content=website property=og:type><meta content="Examples for highlighting text of interest with various Linux CLI tools." property=og:description><meta content=https://learnbyexample.github.io/coloring-matched-portions-grep-sed-awk/ property=og:url><meta content=https://learnbyexample.github.io/images/awk_color_field_processing.png property=og:image><meta content=752 property=og:image:width><meta content=467 property=og:image:height><meta content=summary_large_image property=twitter:card><meta content=@learn_byexample property=twitter:site><link href=https://learnbyexample.github.io/favicon.svg rel=icon><link rel="shortcut icon" href=https://learnbyexample.github.io/favicon.png><body><div class=container><div class=mobile-navbar id=mobile-navbar><div class=mobile-header-logo><a class=logo href=/>learnbyexample</a></div><div class="mobile-navbar-icon icon-out"><span></span><span></span><span></span></div></div><nav class="mobile-menu slideout-menu slideout-menu-left" id=mobile-menu><ul class=mobile-menu-list><li class=mobile-menu-item><a href=https://learnbyexample.github.io/books> Books </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/mini> Mini </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tips> Tips </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tags> Tags </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/about> About </a></ul></nav><header id=header><div class=logo><a href=https://learnbyexample.github.io>learnbyexample</a></div><nav class=menu><ul><li><a href=https://learnbyexample.github.io/books> Books </a><li><a href=https://learnbyexample.github.io/mini> Mini </a><li><a href=https://learnbyexample.github.io/tips> Tips </a><li><a href=https://learnbyexample.github.io/tags> Tags </a><li><a href=https://learnbyexample.github.io/about> About </a></ul></nav></header><main><div class=content id=mobile-panel><div class=post-toc id=post-toc><h2 class=post-toc-title>Contents</h2><div class="post-toc-content always-active"><nav id=TableOfContents><ul><li><a class=toc-link href=https://learnbyexample.github.io/coloring-matched-portions-grep-sed-awk/#gnu-grep>GNU grep</a><li><a class=toc-link href=https://learnbyexample.github.io/coloring-matched-portions-grep-sed-awk/#formatting-with-ansi-escape-sequences>Formatting with ANSI escape sequences</a><li><a class=toc-link href=https://learnbyexample.github.io/coloring-matched-portions-grep-sed-awk/#gnu-sed>GNU sed</a><li><a class=toc-link href=https://learnbyexample.github.io/coloring-matched-portions-grep-sed-awk/#gnu-awk>GNU awk</a></ul></nav></div></div><article class=post><header class=post__header><h1 class=post__title><a href=https://learnbyexample.github.io/coloring-matched-portions-grep-sed-awk/>Coloring matched portions with GNU grep, sed and awk</a></h1><div class=post__meta><span class=post__time>2025-01-13</span></div></header><div class=post-content><p>You might already know how to use the <code>--color</code> option to highlight matched portions with <code>GNU grep</code>. In this post, you'll see how to use ANSI escape sequences to format matched portions with <code>GNU sed</code> and <code>GNU awk</code>.</p><span id=continue-reading></span><br><h2 id=gnu-grep>GNU grep<a aria-label="Anchor link for: gnu-grep" class=zola-anchor href=#gnu-grep>🔗</a></h2><p>Consider this sample input file:<pre style=background-color:#f5f5f5;color:#1f1f1f;><code><span>$ cat fruits.txt
2+
</span><span>banana mango cherry pineapple
3+
</span><span>grape fig apple dragonfruit papaya
4+
</span><span>watermelon cashew tomato orange
5+
</span><span>almond lime grapefruit walnut
6+
</span></code></pre><p>The output for <code>grep --color -wE '[ago]\w+' fruits.txt</code> is shown below:<p align=center><img alt="Example for displaying matched portions in color with GNU grep" src=/images/grep_color.png><p><img alt=info src=/images/info.svg> See <a href=https://learnbyexample.github.io/learn_gnugrep_ripgrep/frequently-used-options.html#colored-output>this section</a> from my ebook on <code>GNU grep</code> for more details about this option. <a href=https://github.com/BurntSushi/ripgrep>ripgrep</a> has a more featured support for color formatting, see <a href=https://learnbyexample.github.io/learn_gnugrep_ripgrep/ripgrep.html#colored-output>this section</a> for an example.</p><br><h2 id=formatting-with-ansi-escape-sequences>Formatting with ANSI escape sequences<a aria-label="Anchor link for: formatting-with-ansi-escape-sequences" class=zola-anchor href=#formatting-with-ansi-escape-sequences>🔗</a></h2><p>Here are some examples to show how you can format text in the terminal using ANSI escape sequences:<p align=center><img alt="Examples for formatting with ANSI escape sequences" src=/images/ANSI_escape_sequences_formatting.png><p>Your choice of formatting goes between <code>\033[</code> and <code>m</code>. In the above example, <code>01</code> is for bold and <code>31</code> is for the color red. Multiple formats can be specified by separating the parameters with a semicolon. Using <code>0</code> turns off the format (otherwise, it will persist in the current terminal session until turned off).<p>See also:<ul><li><a href=https://stackoverflow.com/q/4842424/4082052>List of ANSI color escape sequences</a><li><a href=https://unix.stackexchange.com/q/148/109046>Colorizing your terminal and shell environment</a></ul><p><img alt=info src=/images/info.svg> Note that you can also use <code>\e</code> instead of <code>\033</code> in the above examples. However, that will not work with the <code>GNU awk</code> examples shown below.</p><br><h2 id=gnu-sed>GNU sed<a aria-label="Anchor link for: gnu-sed" class=zola-anchor href=#gnu-sed>🔗</a></h2><p><code>GNU sed</code> doesn't have a native support for the escape sequences discussed above. Instead, you can store the sequences in a shell variable using <a href=https://www.gnu.org/software/bash/manual/bash.html#ANSI_002dC-Quoting>ANSI-C Quoting</a> and use them where required. Here's an example:<p align=center><img alt="Example for displaying matched portions in color with GNU sed" src=/images/sed_color.png></p><br><h2 id=gnu-awk>GNU awk<a aria-label="Anchor link for: gnu-awk" class=zola-anchor href=#gnu-awk>🔗</a></h2><p>With <code>GNU awk</code>, you can directly embed the ANSI escape sequences in a string. Here's an example:<p align=center><img alt="Example for displaying matched portions in color with GNU awk" src=/images/awk_color.png><p>Here's a field processing example:<pre style=background-color:#f5f5f5;color:#1f1f1f;><code><span>$ cat marks.txt
7+
</span><span>Dept Name Marks
8+
</span><span>ECE Raj 53
9+
</span><span>ECE Joel 62
10+
</span><span>EEE Moi 68
11+
</span><span>CSE Surya 81
12+
</span><span>EEE Tia 59
13+
</span><span>ECE Om 92
14+
</span><span>CSE Amy 67
15+
</span><span>
16+
</span><span>$ cat filter.txt
17+
</span><span>ECE 70
18+
</span><span>EEE 65
19+
</span><span>CSE 80
20+
</span></code></pre><p align=center><img alt="Color field processing results with GNU awk" src=/images/awk_color_field_processing.png></div><div class=post-footer><div class=post-tags><a href=https://learnbyexample.github.io/tags/gnu-grep/>#gnu-grep</a><a href=https://learnbyexample.github.io/tags/gnu-sed/>#gnu-sed</a><a href=https://learnbyexample.github.io/tags/gnu-awk/>#gnu-awk</a><a href=https://learnbyexample.github.io/tags/linux/>#linux</a></div><hr color=#e6e6e6><div class=post-nav><p><a class=next href=https://learnbyexample.github.io/100-page-python-intro-book-announcement/>100 Page Python Intro book announcement →</a><br></div><hr color=#e6e6e6><p>📰 Use <a href=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <a href=https://twitter.com/learn_byexample>Twitter</a>, <a href=https://github.com/learnbyexample>GitHub</a> and <a href=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <a href=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hr color=#e6e6e6></div></article></div></main></div><script src=https://learnbyexample.github.io/even.js></script>
16.2 KB
Loading

images/awk_color.png

13.8 KB
Loading

images/awk_color_field_processing.png

17.2 KB
Loading

images/grep_color.png

10.4 KB
Loading

images/sed_color.png

13.4 KB
Loading

0 commit comments

Comments
 (0)