Skip to content

Commit 76b43a1

Browse files
committedSep 29, 2021
edited cut, paste, seq and shuf chapters
1 parent 2270509 commit 76b43a1

File tree

7 files changed

+42
-35
lines changed

7 files changed

+42
-35
lines changed
 

Diff for: ‎cut.html

+8-8
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@
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=cut><a class=header href=#cut>cut</a></h1><p><code>cut</code> is a handy tool for many field processing use cases. The features are limited compared to say <code>awk</code> or <code>perl</code>, but the reduced scope also leads to faster processing.<h2 id=individual-field-selections><a class=header href=#individual-field-selections>Individual field selections</a></h2><p>You can use the <code>-f</code> option to select one or more fields separated by the comma character. By default, <code>cut</code> splits the input on tab character.<pre><code class=language-bash># second field
41+
</script><div id=content class=content><main><div class=sidetoc><nav class=pagetoc></nav></div><h1 id=cut><a class=header href=#cut>cut</a></h1><p><code>cut</code> is a handy tool for many field processing use cases. The features are limited compared to <code>awk</code> and <code>perl</code> commands, but the reduced scope also leads to faster processing.<h2 id=individual-field-selections><a class=header href=#individual-field-selections>Individual field selections</a></h2><p>By default, <code>cut</code> splits the input content into fields based on the tab character. You can use the <code>-f</code> option to select a desired field from each input line. To extract multiple fields, specify the selections separated by the comma character.<pre><code class=language-bash># second field
4242
$ printf 'apple\tbanana\tcherry\n' | cut -f2
4343
banana
4444

4545
# first and third field
4646
$ printf 'apple\tbanana\tcherry\n' | cut -f1,3
4747
apple cherry
48-
</code></pre><p><code>cut</code> will always display the selected fields in the same order as input. Field duplication will be ignored as well.<pre><code class=language-bash># same as: cut -f1,3
48+
</code></pre><p><code>cut</code> will always display the selected fields in ascending order. Field duplication will be ignored as well.<pre><code class=language-bash># same as: cut -f1,3
4949
$ printf 'apple\tbanana\tcherry\n' | cut -f3,1
5050
apple cherry
5151

5252
# same as: cut -f1,2
5353
$ printf 'apple\tbanana\tcherry\n' | cut -f1,1,2,1,2,1,1,2
5454
apple banana
55-
</code></pre><p>By default, <code>cut</code> uses the newline character as the line separator for both input and output.<pre><code class=language-bash>$ printf 'good\tfood\ntip\ttap' | cut -f2
55+
</code></pre><p>By default, <code>cut</code> uses the newline character as the line separator. <code>cut</code> will add a newline character to the output even if the last input line doesn't end with a newline.<pre><code class=language-bash>$ printf 'good\tfood\ntip\ttap' | cut -f2
5656
food
5757
tap
5858
</code></pre><h2 id=field-ranges><a class=header href=#field-ranges>Field ranges</a></h2><p>You can use the <code>-</code> character to specify field ranges. You can skip the starting or ending range, but not both.<pre><code class=language-bash># 2nd, 3rd and 4th fields
@@ -85,7 +85,7 @@
8585
-f3: command not found
8686
$ echo 'one;two;three;four' | cut -d';' -f3
8787
three
88-
</code></pre><h2 id=output-field-delimiter><a class=header href=#output-field-delimiter>Output field delimiter</a></h2><p>Use the <code>--output-delimiter</code> option to customize the output separator to any string of your choice. The string 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.<pre><code class=language-bash># same as: tr '\t' ','
88+
</code></pre><h2 id=output-field-delimiter><a class=header href=#output-field-delimiter>Output field delimiter</a></h2><p>Use the <code>--output-delimiter</code> option to customize the output separator to any string of your choice. The string 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.<pre><code class=language-bash># same as: tr '\t' ','
8989
$ printf 'apple\tbanana\tcherry\n' | cut --output-delimiter=, -f1-
9090
apple,banana,cherry
9191

@@ -111,24 +111,24 @@
111111
$ printf 'apple ball cat\n1 2 3 4 5' | cut --complement -d' ' -f1,3
112112
ball
113113
2 4 5
114-
</code></pre><h2 id=suppress-lines-without-delimiters><a class=header href=#suppress-lines-without-delimiters>Suppress lines without delimiters</a></h2><p>By default, lines not containing the specified input delimiter will be part of the output. You can use the <code>-s</code> option to suppress such lines.<pre><code class=language-bash>$ printf '1,2,3,4\nhello\na,b,c\n'
114+
</code></pre><h2 id=suppress-lines-without-delimiters><a class=header href=#suppress-lines-without-delimiters>Suppress lines without delimiters</a></h2><p>By default, lines not containing the input delimiter will still be part of the output. You can use the <code>-s</code> option to suppress such lines.<pre><code class=language-bash>$ cat mixed_fields.csv
115115
1,2,3,4
116116
hello
117117
a,b,c
118118

119119
# second line doesn't have the comma separator
120120
# by default, such lines will be part of the output
121-
$ printf '1,2,3,4\nhello\na,b,c\n' | cut -d, -f2
121+
$ cut -d, -f2 mixed_fields.csv
122122
2
123123
hello
124124
b
125125

126126
# use -s option to suppress such lines
127-
$ printf '1,2,3,4\nhello\na,b,c\n' | cut -sd, -f2
127+
$ cut -sd, -f2 mixed_fields.csv
128128
2
129129
b
130130

131-
$ printf '1,2,3,4\nhello\na,b,c\n' | cut --complement -sd, -f2
131+
$ cut --complement -sd, -f2 mixed_fields.csv
132132
1,3,4
133133
a,c
134134
</code></pre><p>If a line contains the specified delimiter but doesn't have the field number requested, you'll get a blank line. The <code>-s</code> option has no effect on such lines.<pre><code class=language-bash>$ printf 'apple ball cat\n1 2 3 4 5' | cut -d' ' -f4

Diff for: ‎head-tail.html

+5-3
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,13 @@
7777
13) Much ado about nothing
7878
14) He he he
7979
15) Adios amigo
80-
</code></pre><p>If there are less than 10 lines in the input, only those lines will be displayed.<pre><code class=language-bash># same as: seq 3 | tail
81-
$ seq 3 | head
80+
</code></pre><p>If there are less than 10 lines in the input, only those lines will be displayed.<pre><code class=language-bash># seq command will be discussed in detail later, generates 1 to 4 here
81+
# same as: seq 4 | tail
82+
$ seq 4 | head
8283
1
8384
2
8485
3
86+
4
8587
</code></pre><p>You can use the <code>-nN</code> option to customize the number of lines (<code>N</code>) needed.<pre><code class=language-bash># first three lines
8688
# space between -n and N is optional
8789
$ head -n3 sample.txt
@@ -160,6 +162,6 @@
160162

161163
$ printf 'car\0jeep\0bus\0' | tail -z -n2 | cat -v
162164
jeep^@bus^@
163-
</code></pre></main><nav class=nav-wrapper aria-label="Page navigation"><a rel=prev href=cat-tac.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=tr.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=cat-tac.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=tr.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>
165+
</code></pre><h2 id=further-reading><a class=header href=#further-reading>Further Reading</a></h2><ul><li><a href=https://en.wikipedia.org/wiki/Tail_(Unix)#File_monitoring>wikipedia: File monitoring with tail -f and -F options</a><li><a href=https://unix.stackexchange.com/q/18760/109046>unix.stackexchange: How does the tail -f option work?</a><li><a href=https://mywiki.wooledge.org/BashFAQ/009>How to deal with output buffering?</a></ul></main><nav class=nav-wrapper aria-label="Page navigation"><a rel=prev href=cat-tac.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=tr.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=cat-tac.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=tr.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>
164166
window.playground_copyable = true;
165167
</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>

Diff for: ‎paste.html

+12-14
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
Pink
5656
Red
5757
White
58-
</code></pre><p>By default, <code>paste</code> adds a tab character between corresponding lines of input files. Use <code>-</code> as the filename for <code>stdin</code> data.<pre><code class=language-bash>$ paste colors_1.txt colors_2.txt
58+
</code></pre><p>By default, <code>paste</code> adds a tab character between corresponding lines of input files.<pre><code class=language-bash>$ paste colors_1.txt colors_2.txt
5959
Blue Black
6060
Brown Blue
6161
Orange Green
@@ -71,13 +71,11 @@
7171
5,10
7272

7373
# quote the delimiter if it is a shell metacharacter
74-
$ paste -d'|' <(seq 3) <(seq 4 6) <(seq 7 10)
75-
1|4|7
76-
2|5|8
77-
3|6|9
78-
||10
79-
</code></pre><p>Use empty string or <code>\0</code> if you don't want any delimiter between the columns.<pre><code class=language-bash># you can also use paste -d'\0'
80-
# note that the space between -d and empty string is necessary here
74+
$ paste -d'|' <(seq 3) <(seq 4 5) <(seq 6 8)
75+
1|4|6
76+
2|5|7
77+
3||8
78+
</code></pre><p>Use empty string if you don't want any delimiter between the columns. You can also use <code>\0</code> for this case, but that'd be confusing since it is typically used to mean the NUL character.<pre><code class=language-bash># note that the space between -d and empty string is necessary here
8179
$ paste -d '' <(seq 3) <(seq 6 8)
8280
16
8381
27
@@ -107,7 +105,11 @@
107105
Blue:Brown:Orange
108106
Purple:Red:Teal
109107
White::
110-
</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 (see <a href=https://stackoverflow.com/q/5349718/4082052>this stackoverflow thread</a> for more details).<pre><code class=language-bash># the string before %.s is repeated based on the number of arguments
108+
</code></pre><p>Here's an example with both <code>stdin</code> and file arguments:<pre><code class=language-bash>$ seq 6 | paste - nums.txt -
109+
1 3.14 2
110+
3 42 4
111+
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
111113
$ printf 'x %.s' a b c
112114
x x x
113115
$ printf -- '- %.s' {1..5}
@@ -116,11 +118,7 @@
116118
$ seq 10 | paste -d, $(printf -- '- %.s' {1..5})
117119
1,2,3,4,5
118120
6,7,8,9,10
119-
</code></pre><p>Here's an example with both <code>stdin</code> and file arguments:<pre><code class=language-bash>$ seq 6 | paste - nums.txt -
120-
1 3.14 2
121-
3 42 4
122-
5 1000 6
123-
</code></pre><h2 id=multicharacter-delimiters><a class=header href=#multicharacter-delimiters>Multicharacter delimiters</a></h2><p>The <code>-d</code> option accepts a list of characters (bytes to be precise) to be used one by one between the different columns. If the number of characters is less than the number of separators required, the characters are reused from the beginning and this cycle repeats until all the columns are done. If the number of characters is greater than the number of separators required, the extra characters are simply discarded.<pre><code class=language-bash># , is used between 1st and 2nd column
121+
</code></pre><blockquote><p><img src=./images/info.svg alt=info> See <a href=https://stackoverflow.com/q/5349718/4082052>this stackoverflow thread</a> for more details about the <code>printf</code> solution and other alternatives.</blockquote><h2 id=multicharacter-delimiters><a class=header href=#multicharacter-delimiters>Multicharacter delimiters</a></h2><p>The <code>-d</code> option accepts a list of characters (bytes to be precise) to be used one by one between the different columns. If the number of characters is less than the number of separators required, the characters are reused from the beginning and this cycle repeats until all the columns are done. If the number of characters is greater than the number of separators required, the extra characters are simply discarded.<pre><code class=language-bash># , is used between 1st and 2nd column
124122
# - is used between 2nd and 3rd column
125123
$ paste -d',-' <(seq 3) <(seq 4 6) <(seq 7 9)
126124
1,4-7

Diff for: ‎searchindex.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎searchindex.json

+1-1
Large diffs are not rendered by default.

Diff for: ‎seq.html

+7-5
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
-5
5656
-4
5757
-3
58-
</code></pre><p>When you want to specify all three numbers, the order is start, step and stop.<pre><code class=language-bash># start=1000, step=5 and stop=1010
58+
</code></pre><p>When you want to specify all the three numbers, the order is start, step and stop.<pre><code class=language-bash># start=1000, step=5 and stop=1010
5959
$ seq 1000 5 1010
6060
1000
6161
1005
@@ -92,7 +92,7 @@
9292
120.000
9393
120.752
9494
121.504
95-
</code></pre><h2 id=customizing-separator><a class=header href=#customizing-separator>Customizing separator</a></h2><p>You can use the <code>-s</code> option to change the separator between the numbers of a sequence. Multiple characters are allowed. 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 use escapes like <code>\t</code> instead of a literal tab character. A newline is always added at end.<pre><code class=language-bash>$ seq -s' ' 4
95+
</code></pre><h2 id=customizing-separator><a class=header href=#customizing-separator>Customizing separator</a></h2><p>You can use the <code>-s</code> option to change the separator between the numbers of a sequence. Multiple characters are allowed. 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 use escapes like <code>\t</code> instead of a literal tab character. A newline is always added at the end of the output.<pre><code class=language-bash>$ seq -s' ' 4
9696
1 2 3 4
9797

9898
$ seq -s: -2 0.75 3
@@ -122,7 +122,7 @@
122122
0001
123123
0002
124124
0003
125-
</code></pre><h2 id=printf-style-formatting><a class=header href=#printf-style-formatting>printf style formatting</a></h2><p>You can use the <code>-f</code> option for <code>printf</code> style floating-point number formatting.<pre><code class=language-bash>$ seq -f'%g' -s: 1 0.75 3
125+
</code></pre><h2 id=printf-style-formatting><a class=header href=#printf-style-formatting>printf style formatting</a></h2><p>You can use the <code>-f</code> option for <code>printf</code> style floating-point number formatting. See <a href=https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-printf>bash manual: printf</a> for more details on formatting options.<pre><code class=language-bash>$ seq -f'%g' -s: 1 0.75 3
126126
1:1.75:2.5
127127

128128
$ seq -f'%.4f' -s: 1 0.75 3
@@ -132,12 +132,14 @@
132132
1.200e+02
133133
1.208e+02
134134
1.215e+02
135-
</code></pre><h2 id=limitations><a class=header href=#limitations>Limitations</a></h2><p>As per <a href=https://www.gnu.org/software/coreutils/manual/html_node/seq-invocation.html#seq-invocation>the manual</a>:<blockquote><p>On most systems, <code>seq</code> can produce whole-number output for values up to at least <code>2^53</code>. Larger integers are approximated. The details differ depending on your floating-point implementation.</blockquote><pre><code class=language-bash>$ seq 100000000000000000000 3 100000000000000000010
135+
</code></pre><h2 id=limitations><a class=header href=#limitations>Limitations</a></h2><p>As per <a href=https://www.gnu.org/software/coreutils/manual/html_node/seq-invocation.html#seq-invocation>the manual</a>:<blockquote><p>On most systems, <code>seq</code> can produce whole-number output for values up to at least <code>2^53</code>. Larger integers are approximated. The details differ depending on your floating-point implementation.</blockquote><pre><code class=language-bash># example with approximate values
136+
$ seq 100000000000000000000 3 100000000000000000010
136137
100000000000000000000
137138
100000000000000000000
138139
100000000000000000008
139140
100000000000000000008
140-
</code></pre><blockquote><p>However, note that when limited to non-negative whole numbers, an increment of <code>1</code> and no format-specifying option, <code>seq</code> can print arbitrarily large numbers.</blockquote><pre><code class=language-bash>$ seq 100000000000000000000000000000 100000000000000000000000000005
141+
</code></pre><blockquote><p>However, note that when limited to non-negative whole numbers, an increment of <code>1</code> and no format-specifying option, <code>seq</code> can print arbitrarily large numbers.</blockquote><pre><code class=language-bash># no approximation for step value of 1
142+
$ seq 100000000000000000000000000000 100000000000000000000000000005
141143
100000000000000000000000000000
142144
100000000000000000000000000001
143145
100000000000000000000000000002

0 commit comments

Comments
 (0)
Please sign in to comment.