aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2024-11-07 22:13:40 +0100
committerJohannes Stoelp <johannes.stoelp@gmail.com>2024-11-07 22:13:40 +0100
commitd0fa8ffb43684853b626595abd3d650e86ef4d63 (patch)
treeaad827697bf9318c826c1105d79feffbca4757df /src
parent1070b7b60308925b9c0f98075c354bb05b6f25ca (diff)
downloadnotes-d0fa8ffb43684853b626595abd3d650e86ef4d63.tar.gz
notes-d0fa8ffb43684853b626595abd3d650e86ef4d63.zip
cli: add cut and rev, update tac
Diffstat (limited to 'src')
-rw-r--r--src/SUMMARY.md2
-rw-r--r--src/cli/README.md2
-rw-r--r--src/cli/cut.md41
-rw-r--r--src/cli/rev.md18
-rw-r--r--src/cli/tac.md10
5 files changed, 68 insertions, 5 deletions
diff --git a/src/SUMMARY.md b/src/SUMMARY.md
index ea680ea..e0a4a3e 100644
--- a/src/SUMMARY.md
+++ b/src/SUMMARY.md
@@ -9,11 +9,13 @@
- [CLI foo](./cli/README.md)
- [awk](./cli/awk.md)
+ - [cut](./cli/cut.md)
- [sed](./cli/sed.md)
- [column](./cli/column.md)
- [sort](./cli/sort.md)
- [tr](./cli/tr.md)
- [tac](./cli/tac.md)
+ - [rev](./cli/rev.md)
- [paste](./cli/paste.md)
- [Tools](./tools/README.md)
diff --git a/src/cli/README.md b/src/cli/README.md
index 5eac7cb..c20edc3 100644
--- a/src/cli/README.md
+++ b/src/cli/README.md
@@ -1,9 +1,11 @@
# CLI foo
- [awk](./awk.md)
+- [cut](./cut.md)
- [sed](./sed.md)
- [column](./column.md)
- [sort](./sort.md)
- [tr](./tr.md)
- [tac](./tac.md)
+- [rev](./rev.md)
- [paste](./paste.md)
diff --git a/src/cli/cut.md b/src/cli/cut.md
new file mode 100644
index 0000000..aa7f7f0
--- /dev/null
+++ b/src/cli/cut.md
@@ -0,0 +1,41 @@
+# cut(1)
+
+```sh
+# Remove sections from each line of files(s).
+cut OPT FILE [FILE]
+ -d DELIM delimiter to tokenize
+ -f LIST field selector
+ -c LIST character selector
+```
+
+## Example: only selected characters
+
+```sh
+echo 'aa bb cc dd' | cut -c "1-4"
+# aa b
+
+# Inverted selection.
+echo 'aa bb cc dd' | cut --complement -c "1-4"
+# b cc dd
+```
+
+## Example: only selected fields
+Fields in `cut` are indexed starting from `1` rather than `0`.
+```sh
+# Fields 2 until 3.
+echo 'aa bb cc dd' | cut -d ' ' -f 2-3
+# bb cc
+
+# First field until the 2nd.
+echo 'aa bb cc dd' | cut -d ' ' -f -2
+# aa bb
+
+# Third field until the end.
+echo 'aa bb cc dd' | cut -d ' ' -f 3-
+# cc dd
+
+# If the number of tokens in a line is unkown but we want to remove the last 2
+# tokens we can use rev(1).
+echo 'aa bb cc dd' | rev | cut -d ' ' -f3- | rev
+# aa bb
+```
diff --git a/src/cli/rev.md b/src/cli/rev.md
new file mode 100644
index 0000000..c00d517
--- /dev/null
+++ b/src/cli/rev.md
@@ -0,0 +1,18 @@
+# rev(1)
+
+```sh
+# Reverse each line of file(s), character by character.
+rev FILE [FILE]
+
+echo -e '123\nabc' | rev
+# 321
+# cba
+```
+
+## Example: remove the last 2 tokens with unknown length
+```sh
+# If the number of tokens in a line is unkown but we want to remove the last 2
+# tokens we can use rev(1).
+echo 'aa bb cc dd' | rev | cut -d ' ' -f3- | rev
+# aa bb
+```
diff --git a/src/cli/tac.md b/src/cli/tac.md
index eba42ba..56ab92c 100644
--- a/src/cli/tac.md
+++ b/src/cli/tac.md
@@ -4,9 +4,9 @@
# Reverse output lines of file(s) and concatenate (reverse of cat).
tac FILE [FILE]
-echo -e 'aa\nbb\ncc\ndd' | tac
-# dd
-# cc
-# bb
-# aa
+echo -e 'a1\nb2\nc3\nd4' | tac
+# d4
+# c3
+# b2
+# a1
```