|
5 | 5 | <link href="https://learnbyexample.github.io/atom.xml" rel="self" type="application/atom+xml"/>
|
6 | 6 | <link href="https://learnbyexample.github.io"/>
|
7 | 7 | <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> |
9 | 9 | <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"><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> |
| 17 | +<span id="continue-reading"></span><br> |
| 18 | +<h2 id="gnu-grep">GNU grep<a class="zola-anchor" href="#gnu-grep" aria-label="Anchor link for: gnu-grep">🔗</a></h2> |
| 19 | +<p>Consider this sample input file:</p> |
| 20 | +<pre style="background-color:#f5f5f5;color:#1f1f1f;"><code><span>$ cat fruits.txt |
| 21 | +</span><span>banana mango cherry pineapple |
| 22 | +</span><span>grape fig apple dragonfruit papaya |
| 23 | +</span><span>watermelon cashew tomato orange |
| 24 | +</span><span>almond lime grapefruit walnut |
| 25 | +</span></code></pre> |
| 26 | +<p>The output for <code>grep --color -wE '[ago]\w+' fruits.txt</code> is shown below:</p> |
| 27 | +<p align="center"><img src="/images/grep_color.png" alt="Example for displaying matched portions in color with GNU grep" /></p> |
| 28 | +<p><img src="/images/info.svg" alt="info" /> 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> |
| 29 | +<br> |
| 30 | +<h2 id="formatting-with-ansi-escape-sequences">Formatting with ANSI escape sequences<a class="zola-anchor" href="#formatting-with-ansi-escape-sequences" aria-label="Anchor link for: formatting-with-ansi-escape-sequences">🔗</a></h2> |
| 31 | +<p>Here are some examples to show how you can format text in the terminal using ANSI escape sequences:</p> |
| 32 | +<p align="center"><img src="/images/ANSI_escape_sequences_formatting.png" alt="Examples for formatting with ANSI escape sequences" /></p> |
| 33 | +<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> |
| 34 | +<p>See also:</p> |
| 35 | +<ul> |
| 36 | +<li><a href="https://stackoverflow.com/q/4842424/4082052">List of ANSI color escape sequences</a></li> |
| 37 | +<li><a href="https://unix.stackexchange.com/q/148/109046">Colorizing your terminal and shell environment</a></li> |
| 38 | +</ul> |
| 39 | +<p><img src="/images/info.svg" alt="info" /> 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> |
| 40 | +<br> |
| 41 | +<h2 id="gnu-sed">GNU sed<a class="zola-anchor" href="#gnu-sed" aria-label="Anchor link for: gnu-sed">🔗</a></h2> |
| 42 | +<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> |
| 43 | +<p align="center"><img src="/images/sed_color.png" alt="Example for displaying matched portions in color with GNU sed" /></p> |
| 44 | +<br> |
| 45 | +<h2 id="gnu-awk">GNU awk<a class="zola-anchor" href="#gnu-awk" aria-label="Anchor link for: gnu-awk">🔗</a></h2> |
| 46 | +<p>With <code>GNU awk</code>, you can directly embed the ANSI escape sequences in a string. Here's an example:</p> |
| 47 | +<p align="center"><img src="/images/awk_color.png" alt="Example for displaying matched portions in color with GNU awk" /></p> |
| 48 | +<p>Here's a field processing example:</p> |
| 49 | +<pre style="background-color:#f5f5f5;color:#1f1f1f;"><code><span>$ cat marks.txt |
| 50 | +</span><span>Dept Name Marks |
| 51 | +</span><span>ECE Raj 53 |
| 52 | +</span><span>ECE Joel 62 |
| 53 | +</span><span>EEE Moi 68 |
| 54 | +</span><span>CSE Surya 81 |
| 55 | +</span><span>EEE Tia 59 |
| 56 | +</span><span>ECE Om 92 |
| 57 | +</span><span>CSE Amy 67 |
| 58 | +</span><span> |
| 59 | +</span><span>$ cat filter.txt |
| 60 | +</span><span>ECE 70 |
| 61 | +</span><span>EEE 65 |
| 62 | +</span><span>CSE 80 |
| 63 | +</span></code></pre> |
| 64 | +<p align="center"><img src="/images/awk_color_field_processing.png" alt="Color field processing results with GNU awk" /></p> |
| 65 | +</content> |
| 66 | + </entry> |
10 | 67 | <entry xml:lang="en">
|
11 | 68 | <title>100 Page Python Intro book announcement</title>
|
12 | 69 | <published>2024-12-19T00:00:00+00:00</published>
|
|
0 commit comments