Skip to content

Commit 1dce776

Browse files
updated coloring with CLI tools post
1 parent c71f12e commit 1dce776

File tree

5 files changed

+47
-9
lines changed

5 files changed

+47
-9
lines changed

atom.xml

+28-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
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-13T00:00:00+00:00</updated>
8+
<updated>2025-01-14T00:00:00+00:00</updated>
99
<id>https://learnbyexample.github.io/atom.xml</id>
1010
<entry xml:lang="en">
1111
<title>Coloring matched portions with GNU grep, sed and awk</title>
1212
<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>
1414
<link rel="alternate" href="https://learnbyexample.github.io/coloring-matched-portions-grep-sed-awk/" type="text/html"/>
1515
<id>https://learnbyexample.github.io/coloring-matched-portions-grep-sed-awk/</id>
1616
<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;
@@ -30,7 +30,11 @@
3030
&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;
3131
&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;
3232
&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;
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;. You can use &lt;code&gt;01&lt;&#x2F;code&gt; for &lt;strong&gt;bold&lt;&#x2F;strong&gt;, &lt;code&gt;03&lt;&#x2F;code&gt; for &lt;em&gt;italics&lt;&#x2F;em&gt; and &lt;code&gt;04&lt;&#x2F;code&gt; for &lt;u&gt;underline&lt;&#x2F;u&gt;. &lt;code&gt;31&lt;&#x2F;code&gt; is for the color &lt;font color=&#x27;red&#x27;&gt;red&lt;&#x2F;font&gt;, &lt;code&gt;32&lt;&#x2F;code&gt; for &lt;font color=&#x27;green&#x27;&gt;green&lt;&#x2F;font&gt; and &lt;code&gt;34&lt;&#x2F;code&gt; for &lt;font color=&#x27;blue&#x27;&gt;blue&lt;&#x2F;font&gt;. 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;If you find a file that has accidentally saved such escape sequences, you can use &lt;code&gt;cat -v&lt;&#x2F;code&gt; to identify them.&lt;&#x2F;p&gt;
35+
&lt;pre style=&quot;background-color:#f5f5f5;color:#1f1f1f;&quot;&gt;&lt;code&gt;&lt;span&gt;$ echo &amp;#39;one (two) three&amp;#39; | grep --color=always &amp;#39;(two)&amp;#39; | cat -v
36+
&lt;&#x2F;span&gt;&lt;span&gt;one ^[[01;31m^[[K(two)^[[m^[[K three
37+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3438
&lt;p&gt;See also:&lt;&#x2F;p&gt;
3539
&lt;ul&gt;
3640
&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;
@@ -39,11 +43,27 @@
3943
&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;
4044
&lt;br&gt;
4145
&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;
46+
&lt;p&gt;With &lt;code&gt;GNU sed&lt;&#x2F;code&gt;, you&#x27;ll need to use &lt;code&gt;\o&lt;&#x2F;code&gt; to specify an octal escape sequence. Here&#x27;s an example: &lt;&#x2F;p&gt;
4347
&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;
48+
&lt;p&gt;Here&#x27;s an example for processing lines bounded by distinct markers:&lt;&#x2F;p&gt;
49+
&lt;pre style=&quot;background-color:#f5f5f5;color:#1f1f1f;&quot;&gt;&lt;code&gt;&lt;span&gt;$ cat blocks.txt
50+
&lt;&#x2F;span&gt;&lt;span&gt;mango
51+
&lt;&#x2F;span&gt;&lt;span&gt;icecream
52+
&lt;&#x2F;span&gt;&lt;span&gt;--start 1--
53+
&lt;&#x2F;span&gt;&lt;span&gt;dragon 1234
54+
&lt;&#x2F;span&gt;&lt;span&gt;unicorn 6789
55+
&lt;&#x2F;span&gt;&lt;span&gt;**end 1**
56+
&lt;&#x2F;span&gt;&lt;span&gt;have a nice day
57+
&lt;&#x2F;span&gt;&lt;span&gt;--start 2--
58+
&lt;&#x2F;span&gt;&lt;span&gt;a b c
59+
&lt;&#x2F;span&gt;&lt;span&gt;apple banana cherry
60+
&lt;&#x2F;span&gt;&lt;span&gt;**end 2**
61+
&lt;&#x2F;span&gt;&lt;span&gt;par,far,mar,tar
62+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
63+
&lt;p align=&quot;center&quot;&gt;&lt;img src=&quot;&#x2F;images&#x2F;sed_multiline_color.png&quot; alt=&quot;Highlighting portion of interest in multiline processing with GNU sed&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
4464
&lt;br&gt;
4565
&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;
66+
&lt;p&gt;With &lt;code&gt;GNU awk&lt;&#x2F;code&gt;, you can embed the ANSI escape sequences in a string similar to the &lt;code&gt;printf&lt;&#x2F;code&gt; example seen earlier.&lt;&#x2F;p&gt;
4767
&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;
4868
&lt;p&gt;Here&#x27;s a field processing example:&lt;&#x2F;p&gt;
4969
&lt;pre style=&quot;background-color:#f5f5f5;color:#1f1f1f;&quot;&gt;&lt;code&gt;&lt;span&gt;$ cat marks.txt
@@ -62,6 +82,9 @@
6282
&lt;&#x2F;span&gt;&lt;span&gt;CSE 80
6383
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
6484
&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;
85+
&lt;br&gt;
86+
&lt;h2 id=&quot;linux-cli-ebooks&quot;&gt;Linux CLI ebooks&lt;a class=&quot;zola-anchor&quot; href=&quot;#linux-cli-ebooks&quot; aria-label=&quot;Anchor link for: linux-cli-ebooks&quot;&gt;🔗&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
87+
&lt;p&gt;Check out &lt;a href=&quot;https:&#x2F;&#x2F;learnbyexample.github.io&#x2F;books&#x2F;&quot;&gt;my ebooks&lt;&#x2F;a&gt; if you are interested in learning more about Linux CLI basics, coreutils, text processing tools like &lt;code&gt;GNU grep&lt;&#x2F;code&gt;, &lt;code&gt;GNU sed&lt;&#x2F;code&gt;, &lt;code&gt;GNU awk&lt;&#x2F;code&gt; and &lt;code&gt;perl&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
6588
</content>
6689
</entry>
6790
<entry xml:lang="en">

0 commit comments

Comments
 (0)