|
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-13T00:00:00+00:00</updated> |
| 8 | + <updated>2025-01-14T00:00:00+00:00</updated> |
9 | 9 | <id>https://learnbyexample.github.io/atom.xml</id>
|
10 | 10 | <entry xml:lang="en">
|
11 | 11 | <title>Coloring matched portions with GNU grep, sed and awk</title>
|
12 | 12 | <published>2025-01-13T00:00:00+00:00</published>
|
13 |
| - <updated>2025-01-13T00:00:00+00:00</updated> |
| 13 | + <updated>2025-01-14T00:00:00+00:00</updated> |
14 | 14 | <link rel="alternate" href="https://learnbyexample.github.io/coloring-matched-portions-grep-sed-awk/" type="text/html"/>
|
15 | 15 | <id>https://learnbyexample.github.io/coloring-matched-portions-grep-sed-awk/</id>
|
16 | 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>
|
|
30 | 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 | 31 | <p>Here are some examples to show how you can format text in the terminal using ANSI escape sequences:</p>
|
32 | 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> |
| 33 | +<p>Your choice of formatting goes between <code>\033[</code> and <code>m</code>. You can use <code>01</code> for <strong>bold</strong>, <code>03</code> for <em>italics</em> and <code>04</code> for <u>underline</u>. <code>31</code> is for the color <font color='red'>red</font>, <code>32</code> for <font color='green'>green</font> and <code>34</code> for <font color='blue'>blue</font>. 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>If you find a file that has accidentally saved such escape sequences, you can use <code>cat -v</code> to identify them.</p> |
| 35 | +<pre style="background-color:#f5f5f5;color:#1f1f1f;"><code><span>$ echo &#39;one (two) three&#39; | grep --color=always &#39;(two)&#39; | cat -v |
| 36 | +</span><span>one ^[[01;31m^[[K(two)^[[m^[[K three |
| 37 | +</span></code></pre> |
34 | 38 | <p>See also:</p>
|
35 | 39 | <ul>
|
36 | 40 | <li><a href="https://stackoverflow.com/q/4842424/4082052">List of ANSI color escape sequences</a></li>
|
|
39 | 43 | <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 | 44 | <br>
|
41 | 45 | <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> |
| 46 | +<p>With <code>GNU sed</code>, you'll need to use <code>\o</code> to specify an octal escape sequence. Here's an example: </p> |
43 | 47 | <p align="center"><img src="/images/sed_color.png" alt="Example for displaying matched portions in color with GNU sed" /></p>
|
| 48 | +<p>Here's an example for processing lines bounded by distinct markers:</p> |
| 49 | +<pre style="background-color:#f5f5f5;color:#1f1f1f;"><code><span>$ cat blocks.txt |
| 50 | +</span><span>mango |
| 51 | +</span><span>icecream |
| 52 | +</span><span>--start 1-- |
| 53 | +</span><span>dragon 1234 |
| 54 | +</span><span>unicorn 6789 |
| 55 | +</span><span>**end 1** |
| 56 | +</span><span>have a nice day |
| 57 | +</span><span>--start 2-- |
| 58 | +</span><span>a b c |
| 59 | +</span><span>apple banana cherry |
| 60 | +</span><span>**end 2** |
| 61 | +</span><span>par,far,mar,tar |
| 62 | +</span></code></pre> |
| 63 | +<p align="center"><img src="/images/sed_multiline_color.png" alt="Highlighting portion of interest in multiline processing with GNU sed" /></p> |
44 | 64 | <br>
|
45 | 65 | <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> |
| 66 | +<p>With <code>GNU awk</code>, you can embed the ANSI escape sequences in a string similar to the <code>printf</code> example seen earlier.</p> |
47 | 67 | <p align="center"><img src="/images/awk_color.png" alt="Example for displaying matched portions in color with GNU awk" /></p>
|
48 | 68 | <p>Here's a field processing example:</p>
|
49 | 69 | <pre style="background-color:#f5f5f5;color:#1f1f1f;"><code><span>$ cat marks.txt
|
|
62 | 82 | </span><span>CSE 80
|
63 | 83 | </span></code></pre>
|
64 | 84 | <p align="center"><img src="/images/awk_color_field_processing.png" alt="Color field processing results with GNU awk" /></p>
|
| 85 | +<br> |
| 86 | +<h2 id="linux-cli-ebooks">Linux CLI ebooks<a class="zola-anchor" href="#linux-cli-ebooks" aria-label="Anchor link for: linux-cli-ebooks">🔗</a></h2> |
| 87 | +<p>Check out <a href="https://learnbyexample.github.io/books/">my ebooks</a> if you are interested in learning more about Linux CLI basics, coreutils, text processing tools like <code>GNU grep</code>, <code>GNU sed</code>, <code>GNU awk</code> and <code>perl</code>.</p> |
65 | 88 | </content>
|
66 | 89 | </entry>
|
67 | 90 | <entry xml:lang="en">
|
|
0 commit comments