Skip to content

Commit 5a7d9d2

Browse files
edited pr, fold and fmt chapters
1 parent 76b43a1 commit 5a7d9d2

File tree

5 files changed

+31
-14
lines changed

5 files changed

+31
-14
lines changed

fold-fmt.html

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@
3838
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
3939
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
4040
});
41-
</script><div id=content class=content><main><div class=sidetoc><nav class=pagetoc></nav></div><h1 id=fold-and-fmt><a class=header href=#fold-and-fmt>fold and fmt</a></h1><p>These two commands are useful to split and join lines to meet a specific line length requirement. <code>fmt</code> is smarter and usually the tool you want, but <code>fold</code> can be handy for some cases.<h2 id=fold><a class=header href=#fold>fold</a></h2><p>By default, <code>fold</code> will wrap lines that are greater than <code>80</code> bytes long, which can be customized using the <code>-w</code> option. You might wonder if there are tasks where wrapping without context could be useful. One use case I can think of is the <a href=https://en.wikipedia.org/wiki/FASTA_format>FASTA format</a>.<pre><code class=language-bash>$ cat greeting.txt
41+
</script><div id=content class=content><main><div class=sidetoc><nav class=pagetoc></nav></div><h1 id=fold-and-fmt><a class=header href=#fold-and-fmt>fold and fmt</a></h1><p>These two commands are useful to split and join lines to meet a specific line length requirement. <code>fmt</code> is smarter and usually the tool you want, but <code>fold</code> can be handy for some cases.<h2 id=fold><a class=header href=#fold>fold</a></h2><p>By default, <code>fold</code> will wrap lines that are greater than <code>80</code> bytes long, which can be customized using the <code>-w</code> option. The newline character isn't part of this line length calculation. You might wonder if there are tasks where wrapping without context could be useful. One use case I can think of is the <a href=https://en.wikipedia.org/wiki/FASTA_format>FASTA format</a>.<pre><code class=language-bash>$ cat greeting.txt
4242
Hi there
4343
Have a nice day
4444

45-
# second line is greater than 10 bytes
45+
# splits second line since it is greater than 10 bytes
4646
$ fold -w10 greeting.txt
4747
Hi there
4848
Have a nic
4949
e day
50-
</code></pre><p>The <code>-s</code> option will look for spaces to break before the maximum width allowed.<pre><code class=language-bash>$ fold -s -w10 greeting.txt
50+
</code></pre><p>The <code>-s</code> option looks for the presence of spaces to determine the line splitting. This check is performed within the limits of the wrap length.<pre><code class=language-bash>$ fold -s -w10 greeting.txt
5151
Hi there
5252
Have a
5353
nice day
@@ -86,8 +86,24 @@
8686
</code></pre><p>Unlike <code>fold</code>, words are not split even if they exceed the maximum line width. Another difference is that <code>fmt</code> will add a final newline character if it isn't present in the input.<pre><code class=language-bash>$ printf 'hi there' | fmt -w4
8787
hi
8888
there
89-
</code></pre><p>The <code>fmt</code> command also allows you to join lines together that are shorter than the specified width. As mentioned before, paragraphs are taken into consideration, so empty lines will prevent merging. The <code>-s</code> option will disable line merging.<pre><code class=language-bash># first 4 characters of sample.txt are ignored here,
90-
# since line numbering will hinder lines being merged
89+
</code></pre><p>The <code>fmt</code> command also allows you to join lines together that are shorter than the specified width. As mentioned before, paragraphs are taken into consideration, so empty lines will prevent merging. The <code>-s</code> option will disable line merging.<pre><code class=language-bash>$ cat sample.txt
90+
1) Hello World
91+
2)
92+
3) Hi there
93+
4) How are you
94+
5)
95+
6) Just do-it
96+
7) Believe it
97+
8)
98+
9) banana
99+
10) papaya
100+
11) mango
101+
12)
102+
13) Much ado about nothing
103+
14) He he he
104+
15) Adios amigo
105+
106+
# 'cut' here helps to ignore first 4 characters of sample.txt
91107
$ cut -c5- sample.txt | fmt -w30
92108
Hello World
93109

paste.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
1 3.14 2
110110
3 42 4
111111
5 1000 6
112-
</code></pre><p>If you don't want to manually type the number of <code>-</code> required, you can use the below <code>printf</code> trick<pre><code class=language-bash># the string before %.s is repeated based on the number of arguments
112+
</code></pre><p>If you don't want to manually type the number of <code>-</code> required, you can use this <code>printf</code> trick:<pre><code class=language-bash># the string before %.s is repeated based on the number of arguments
113113
$ printf 'x %.s' a b c
114114
x x x
115115
$ printf -- '- %.s' {1..5}

pr.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
3939
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
4040
});
41-
</script><div id=content class=content><main><div class=sidetoc><nav class=pagetoc></nav></div><h1 id=pr><a class=header href=#pr>pr</a></h1><blockquote><p>Paginate or columnate FILE(s) for printing.</blockquote><p>As stated in the above quote from the manual, the <code>pr</code> command is mainly used for two tasks. This book will discuss only the columnate features and some miscellaneous tasks.<p>Here's a pagination example if you are interested in exploring further. The <code>pr</code> command will add blank lines, a header and so on to make it suitable for printing.<pre><code class=language-bash>$ pr greeting.txt | head
41+
</script><div id=content class=content><main><div class=sidetoc><nav class=pagetoc></nav></div><h1 id=pr><a class=header href=#pr>pr</a></h1><blockquote><p>Paginate or columnate FILE(s) for printing.</blockquote><p>As stated in the above quote from the manual, the <code>pr</code> command is mainly used for those two tasks. This book will discuss only the columnate features and some miscellaneous tasks.<p>Here's a pagination example if you are interested in exploring further. The <code>pr</code> command will add blank lines, a header and so on to make it suitable for printing.<pre><code class=language-bash>$ pr greeting.txt | head
4242

4343

4444
2021-08-05 14:10 greeting.txt Page 1
@@ -88,7 +88,7 @@
8888
9,10,,
8989
</code></pre><h2 id=customizing-page-width><a class=header href=#customizing-page-width>Customizing page width</a></h2><p>As mentioned before, the default page width is <code>72</code>. This can cause lines to be truncated, unless <code>-s</code> or <code>-J</code> options are used. There's another issue you might run into, for example:<pre><code class=language-bash>$ seq 100 | pr -50ats,
9090
pr: page width too narrow
91-
</code></pre><p><code>(N-1)*length(separator) + N</code> is the minimum page width you need, where <code>N</code> is the number of columns required. So, for <code>50</code> columns and a separator of length <code>1</code>, you'll need a minimum width of <code>99</code>. This calculation doesn't make any assumption about the size of input lines, so you'll need <code>-J</code> to ensure input lines aren't truncated.<p>You can use the <code>-w</code> option to change the page width. This option will truncate lines, so use <code>-J</code> option as well unless you really need truncation. If truncation is active, maximum column width is <code>(PageWidth - (N-1)*length(separator)) / N</code> rounded down to an integer value. Here's some examples:<pre><code class=language-bash># minimum width needed is 3 for N=2 and length=1
91+
</code></pre><p><code>(N-1)*length(separator) + N</code> is the minimum page width you need, where <code>N</code> is the number of columns required. So, for <code>50</code> columns and a separator of length <code>1</code>, you'll need a minimum width of <code>99</code>. This calculation doesn't make any assumption about the size of input lines, so you may need <code>-J</code> to ensure input lines aren't truncated.<p>You can use the <code>-w</code> option to change the page width. The <code>-w</code> option overrides the effect of <code>-s</code> option on line truncation, so use <code>-J</code> option as well unless you really need truncation. If truncation is active, maximum column width is <code>(PageWidth - (N-1)*length(separator)) / N</code> rounded down to an integer value. Here's some examples:<pre><code class=language-bash># minimum width needed is 3 for N=2 and length=1
9292
# maximum column width: (6 - 1) / 2 = 2
9393
$ pr -w6 -2ts, greeting.txt
9494
Hi,Ha
@@ -121,7 +121,7 @@
121121
1 : 4
122122
2 : 5
123123
3 : 6
124-
</code></pre><p>You can prefix the output with line numbers using the <code>-n</code> option. By default, this option supports up to <code>5</code> digit numbers and uses the tab character to separate the line contents. You can optionally pass two arguments to this option — maximum number of digits and the separator character. If both arguments are used, the separator should be specified first. If you want to customize the starting line number, use the <code>-N</code> option as well.<pre><code class=language-bash># maximum of 1 digit for numbering
124+
</code></pre><p>You can prefix the output with line numbers using the <code>-n</code> option. By default, this option supports up to <code>5</code> digit numbers and uses the tab character to separate the numbering and line contents. You can optionally pass two arguments to this option — maximum number of digits and the separator character. If both arguments are used, the separator should be specified first. If you want to customize the starting line number, use the <code>-N</code> option as well.<pre><code class=language-bash># maximum of 1 digit for numbering
125125
# use : as the separator between line number and line contents
126126
$ pr -n:1 -mts, colors_1.txt colors_2.txt
127127
1:Blue,Black
@@ -133,14 +133,15 @@
133133
7:White,White
134134
</code></pre><p>The string passed to <code>-s</code> is treated literally. Depending on your shell you can use <a href=https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html>ANSI-C quoting</a> to allow escape sequences. Unlike columnate, the separator is added even if the data is missing for some of the files.<pre><code class=language-bash># greeting.txt has 2 lines
135135
# fruits.txt has 3 lines
136+
# same as: paste -d$'\n' greeting.txt fruits.txt
136137
$ pr -mts$'\n' greeting.txt fruits.txt
137138
Hi there
138139
banana
139140
Have a nice day
140141
papaya
141142

142143
mango
143-
</code></pre><h2 id=miscellaneous><a class=header href=#miscellaneous>Miscellaneous</a></h2><p>You can use the <code>-d</code> option to double space the input. That is, every newline character is doubled.<pre><code class=language-bash>$ pr -dt fruits.txt
144+
</code></pre><h2 id=miscellaneous><a class=header href=#miscellaneous>Miscellaneous</a></h2><p>You can use the <code>-d</code> option to double space the input contents. That is, every newline character is doubled.<pre><code class=language-bash>$ pr -dt fruits.txt
144145
banana
145146

146147
papaya
@@ -150,8 +151,8 @@
150151
</code></pre><p>The <code>-v</code> option will convert non-printing characters like carriage return, backspace, etc to their octal representations (<code>\NNN</code>).<pre><code class=language-bash>$ printf 'car\bt\r\nbike\0p\r\n' | pr -vt
151152
car\010t\015
152153
bike\000p\015
153-
</code></pre><p><code>pr -t</code> is a roundabout way of concatenating input files. But one advantage is that this will add a newline character at the end if not present in the input.<pre><code class=language-bash># cat will not add a newline character
154-
# so, use pr if newline is needed at the end
154+
</code></pre><p><code>pr -t</code> is a roundabout way of concatenating input files. But one advantage is that this will add a newline character at the end if not present in the input.<pre><code class=language-bash># 'cat' will not add a newline character
155+
# so, use 'pr' if newline is needed at the end
155156
$ printf 'a\nb\nc' | pr -t
156157
a
157158
b

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)