You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: cat-tac.html
+48-4Lines changed: 48 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -205,6 +205,9 @@
205
205
3
206
206
2
207
207
1
208
+
</code></pre><blockquote><p><imgsrc=./images/warning.svgalt=warning> If the last line of input doesn't end with a newline, the output will also not have that newline character.</blockquote><pre><codeclass=language-bash>$ printf 'apple\nbanana\ncherry' | tac
209
+
cherrybanana
210
+
apple
208
211
</code></pre><p>Reversing input lines makes some of the text processing tasks easier. For example, if there multiple matches but you want only the last such match. See my ebooks on <ahref=https://github.com/learnbyexample/learn_gnused>GNU sed</a> and <ahref=https://github.com/learnbyexample/learn_gnuawk>GNU awk</a> for more examples.<pre><codeclass=language-bash>$ cat log.txt
209
212
--> warning 1
210
213
a,b,c,d
@@ -220,14 +223,55 @@
220
223
$ tac log.txt | sed '/warning/q' | tac
221
224
--> warning 3
222
225
4,3,1
223
-
</code></pre><p>The <code>log.txt</code> input file has multiple lines containing <code>warning</code>. The task is to fetch lines based on the last match. Tools like <code>grep</code> and <code>sed</code> have features to easily match the first occurrence, so applying <code>tac</code> on the input helps to reverse the condition from last match to first match. Another benefit is that the first <code>tac</code> will stop reading input contents after the match is found in the above examples.<p>By default, the newline character is used to split the input content into <em>lines</em>. You can use the <code>-s</code> option to specify a different string to be used as the separator. Here's an example:<pre><codeclass=language-bash>$ printf 'car\njeep\nbus\n' | tac
226
+
</code></pre><p>The <code>log.txt</code> input file has multiple lines containing <code>warning</code>. The task is to fetch lines based on the last match. Tools like <code>grep</code> and <code>sed</code> have features to easily match the first occurrence, so applying <code>tac</code> on the input helps to reverse the condition from last match to first match. Another benefit is that the first <code>tac</code> will stop reading input contents after the match is found in the above examples.<p>By default, the newline character is used to split the input content into <em>lines</em>. You can use the <code>-s</code> option to specify a different string to be used as the separator. Here's some examples:<pre><codeclass=language-bash>$ printf 'car\njeep\nbus\n' | tac
</code></pre><blockquote><p><imgsrc=./images/info.svgalt=info> Use the <code>rev</code> command if you want each input line to be reversed character wise.</blockquote></main><navclass=nav-wrapperaria-label="Page navigation"><arel=prevhref=preface.htmlclass="mobile-nav-chapters previous"title="Previous chapter"aria-label="Previous chapter"aria-keyshortcuts=Left><iclass="fa fa-angle-left"></i></a><arel=nexthref=head-tail.htmlclass="mobile-nav-chapters next"title="Next chapter"aria-label="Next chapter"aria-keyshortcuts=Right><iclass="fa fa-angle-right"></i></a><divstyle="clear: both"></div></nav></div></div><navclass=nav-wide-wrapperaria-label="Page navigation"><arel=prevhref=preface.htmlclass="nav-chapters previous"title="Previous chapter"aria-label="Previous chapter"aria-keyshortcuts=Left><iclass="fa fa-angle-left"></i></a><arel=nexthref=head-tail.htmlclass="nav-chapters next"title="Next chapter"aria-label="Next chapter"aria-keyshortcuts=Right><iclass="fa fa-angle-right"></i></a></nav></div><script>
234
+
235
+
# as seen before, last entry should also have the separator
</code></pre><p>When the custom separator occurs before the content of interest, use the <code>-b</code> option to print those separators before the content in the output as well.<pre><codeclass=language-bash>$ cat body_sep.txt
242
+
%=%=
243
+
apple
244
+
banana
245
+
%=%=
246
+
red
247
+
green
248
+
249
+
$ tac -b -s '%=%=' body_sep.txt
250
+
%=%=
251
+
red
252
+
green
253
+
%=%=
254
+
apple
255
+
banana
256
+
</code></pre><p>The separator will be treated as a regular expressions if you use the <code>-r</code> option as well.<pre><codeclass=language-bash>$ cat shopping.txt
257
+
apple 50
258
+
toys 5
259
+
Pizza 2
260
+
mango 25
261
+
Banana 10
262
+
263
+
# separator character is 'a' or 'm' at the start of a line
264
+
$ tac -b -rs '^[am]' shopping.txt
265
+
mango 25
266
+
Banana 10
267
+
apple 50
268
+
toys 5
269
+
Pizza 2
270
+
271
+
# alternate solution for this example: tac log.txt | sed '/warning/q' | tac
</code></pre><blockquote><p><imgsrc=./images/info.svgalt=info> See <ahref=https://learnbyexample.github.io/learn_gnugrep_ripgrep/breere-regular-expressions.html>Regular Expressions</a> chapter from my <strong>GNU grep</strong> ebook if you want to learn about regexp syntax and features.</blockquote><blockquote><p><imgsrc=./images/info.svgalt=info> Use the <code>rev</code> command if you want each input line to be reversed character wise.</blockquote></main><navclass=nav-wrapperaria-label="Page navigation"><arel=prevhref=preface.htmlclass="mobile-nav-chapters previous"title="Previous chapter"aria-label="Previous chapter"aria-keyshortcuts=Left><iclass="fa fa-angle-left"></i></a><arel=nexthref=head-tail.htmlclass="mobile-nav-chapters next"title="Next chapter"aria-label="Next chapter"aria-keyshortcuts=Right><iclass="fa fa-angle-right"></i></a><divstyle="clear: both"></div></nav></div></div><navclass=nav-wide-wrapperaria-label="Page navigation"><arel=prevhref=preface.htmlclass="nav-chapters previous"title="Previous chapter"aria-label="Previous chapter"aria-keyshortcuts=Left><iclass="fa fa-angle-left"></i></a><arel=nexthref=head-tail.htmlclass="nav-chapters next"title="Next chapter"aria-label="Next chapter"aria-keyshortcuts=Right><iclass="fa fa-angle-right"></i></a></nav></div><script>
Copy file name to clipboardExpand all lines: nl.html
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -57,17 +57,17 @@
57
57
58
58
3 cherry
59
59
</code></pre><h2id=number-formatting><aclass=headerhref=#number-formatting>Number formatting</a></h2><p>You can use the <code>-n</code> option to customize the number formatting. The available styles are:<ul><li><code>rn</code> right justified with space fillers (default)<li><code>rz</code> right justified with leading zeros<li><code>ln</code> left justified with space fillers</ul><pre><codeclass=language-bash># right justified with space fillers
60
-
$ nl -n'rn' greeting.txt
60
+
$ nl -n'rn' greeting.txt
61
61
1 Hi there
62
62
2 Have a nice day
63
63
64
64
# right justified with leading zeros
65
-
$ nl -n'rz' greeting.txt
65
+
$ nl -n'rz' greeting.txt
66
66
000001 Hi there
67
67
000002 Have a nice day
68
68
69
69
# left justified with space fillers
70
-
$ nl -n'ln' greeting.txt
70
+
$ nl -n'ln' greeting.txt
71
71
1 Hi there
72
72
2 Have a nice day
73
73
</code></pre><h2id=customize-width><aclass=headerhref=#customize-width>Customize width</a></h2><p>You can use the <code>-w</code> option to specify the width to be used for the numbers (default is <code>6</code>).<pre><codeclass=language-bash>$ nl -w2 greeting.txt
0 commit comments