Skip to content

Commit 322ee7c

Browse files
added more examples for tac command
1 parent c899d0c commit 322ee7c

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

cat-tac.html

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@
205205
3
206206
2
207207
1
208+
</code></pre><blockquote><p><img src=./images/warning.svg alt=warning> If the last line of input doesn't end with a newline, the output will also not have that newline character.</blockquote><pre><code class=language-bash>$ printf 'apple\nbanana\ncherry' | tac
209+
cherrybanana
210+
apple
208211
</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 <a href=https://github.com/learnbyexample/learn_gnused>GNU sed</a> and <a href=https://github.com/learnbyexample/learn_gnuawk>GNU awk</a> for more examples.<pre><code class=language-bash>$ cat log.txt
209212
--> warning 1
210213
a,b,c,d
@@ -220,14 +223,55 @@
220223
$ tac log.txt | sed '/warning/q' | tac
221224
--> warning 3
222225
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><code class=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><code class=language-bash>$ printf 'car\njeep\nbus\n' | tac
224227
bus
225228
jeep
226229
car
227-
230+
# use NUL as the line separator
228231
# you can also use -s '' instead of -s $'\0'
229-
$ printf 'car\0jeep\0bus\0' | tac -s $'\0' | cat -e
232+
$ printf 'car\0jeep\0bus\0' | tac -s $'\0' | cat -v
230233
bus^@jeep^@car^@
231-
</code></pre><blockquote><p><img src=./images/info.svg alt=info> Use the <code>rev</code> command if you want each input line to be reversed character wise.</blockquote></main><nav class=nav-wrapper aria-label="Page navigation"><a rel=prev href=preface.html class="mobile-nav-chapters previous"title="Previous chapter"aria-label="Previous chapter"aria-keyshortcuts=Left> <i class="fa fa-angle-left"></i> </a><a rel=next href=head-tail.html class="mobile-nav-chapters next"title="Next chapter"aria-label="Next chapter"aria-keyshortcuts=Right> <i class="fa fa-angle-right"></i> </a><div style="clear: both"></div></nav></div></div><nav class=nav-wide-wrapper aria-label="Page navigation"><a rel=prev href=preface.html class="nav-chapters previous"title="Previous chapter"aria-label="Previous chapter"aria-keyshortcuts=Left> <i class="fa fa-angle-left"></i> </a><a rel=next href=head-tail.html class="nav-chapters next"title="Next chapter"aria-label="Next chapter"aria-keyshortcuts=Right> <i class="fa fa-angle-right"></i> </a></nav></div><script>
234+
235+
# as seen before, last entry should also have the separator
236+
# otherwise it won't be present in the output
237+
$ printf 'apple banana cherry' | tac -s ' ' | cat -e
238+
cherrybanana apple $
239+
$ printf 'apple banana cherry ' | tac -s ' ' | cat -e
240+
cherry banana apple $
241+
</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><code class=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><code class=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
272+
$ tac -b -rs '^.*warning' log.txt | awk '/warning/ && ++c==2{exit} 1'
273+
--> warning 3
274+
4,3,1
275+
</code></pre><blockquote><p><img src=./images/info.svg alt=info> See <a href=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><img src=./images/info.svg alt=info> Use the <code>rev</code> command if you want each input line to be reversed character wise.</blockquote></main><nav class=nav-wrapper aria-label="Page navigation"><a rel=prev href=preface.html class="mobile-nav-chapters previous"title="Previous chapter"aria-label="Previous chapter"aria-keyshortcuts=Left> <i class="fa fa-angle-left"></i> </a><a rel=next href=head-tail.html class="mobile-nav-chapters next"title="Next chapter"aria-label="Next chapter"aria-keyshortcuts=Right> <i class="fa fa-angle-right"></i> </a><div style="clear: both"></div></nav></div></div><nav class=nav-wide-wrapper aria-label="Page navigation"><a rel=prev href=preface.html class="nav-chapters previous"title="Previous chapter"aria-label="Previous chapter"aria-keyshortcuts=Left> <i class="fa fa-angle-left"></i> </a><a rel=next href=head-tail.html class="nav-chapters next"title="Next chapter"aria-label="Next chapter"aria-keyshortcuts=Right> <i class="fa fa-angle-right"></i> </a></nav></div><script>
232276
window.playground_copyable = true;
233277
</script><script src=elasticlunr.min.js charset=utf-8></script><script src=mark.min.js charset=utf-8></script><script src=searcher.js charset=utf-8></script><script src=clipboard.min.js charset=utf-8></script><script src=highlight.js charset=utf-8></script><script src=book.js charset=utf-8></script><script src=sidebar.js></script>

nl.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@
5757

5858
3 cherry
5959
</code></pre><h2 id=number-formatting><a class=header href=#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><code class=language-bash># right justified with space fillers
60-
$ nl -n 'rn' greeting.txt
60+
$ nl -n'rn' greeting.txt
6161
1 Hi there
6262
2 Have a nice day
6363

6464
# right justified with leading zeros
65-
$ nl -n 'rz' greeting.txt
65+
$ nl -n'rz' greeting.txt
6666
000001 Hi there
6767
000002 Have a nice day
6868

6969
# left justified with space fillers
70-
$ nl -n 'ln' greeting.txt
70+
$ nl -n'ln' greeting.txt
7171
1 Hi there
7272
2 Have a nice day
7373
</code></pre><h2 id=customize-width><a class=header href=#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><code class=language-bash>$ nl -w2 greeting.txt

searchindex.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

searchindex.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)