mirror of
https://github.com/jbranchaud/til
synced 2026-07-02 23:58:25 +00:00
Add Reverse Each Line Of A File as a Unix TIL
This commit is contained in:
@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
|
||||||
|
|
||||||
_1780 TILs and counting..._
|
_1781 TILs and counting..._
|
||||||
|
|
||||||
See some of the other learning resources I work on:
|
See some of the other learning resources I work on:
|
||||||
|
|
||||||
@@ -1796,6 +1796,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
|||||||
- [Rename A Bunch Of Files By Constructing mv Commands](unix/rename-a-bunch-of-files-by-constructing-mv-commands.md)
|
- [Rename A Bunch Of Files By Constructing mv Commands](unix/rename-a-bunch-of-files-by-constructing-mv-commands.md)
|
||||||
- [Repeat Yourself](unix/repeat-yourself.md)
|
- [Repeat Yourself](unix/repeat-yourself.md)
|
||||||
- [Replace Pattern Across Many Files In A Project](unix/replace-pattern-across-many-files-in-a-project.md)
|
- [Replace Pattern Across Many Files In A Project](unix/replace-pattern-across-many-files-in-a-project.md)
|
||||||
|
- [Reverse Each Line Of A File](unix/reverse-each-line-of-a-file.md)
|
||||||
- [Run A Command Repeatedly Several Times](unix/run-a-command-repeatedly-several-times.md)
|
- [Run A Command Repeatedly Several Times](unix/run-a-command-repeatedly-several-times.md)
|
||||||
- [Run A cURL Command Without The Progress Meter](unix/run-a-curl-command-without-the-progress-meter.md)
|
- [Run A cURL Command Without The Progress Meter](unix/run-a-curl-command-without-the-progress-meter.md)
|
||||||
- [Safely Edit The Sudoers File With Vim](unix/safely-edit-the-sudoers-file-with-vim.md)
|
- [Safely Edit The Sudoers File With Vim](unix/safely-edit-the-sudoers-file-with-vim.md)
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
# Reverse Each Line Of A File
|
||||||
|
|
||||||
|
The [`rev` command](https://man7.org/linux/man-pages/man1/rev.1.html) can be
|
||||||
|
used to reverse each line in a file. Every line is left where it is relative to
|
||||||
|
other lines, but the contents of each line is reversed.
|
||||||
|
|
||||||
|
So a file that contains the following text:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
❯ cat stuff.md
|
||||||
|
Three
|
||||||
|
Two
|
||||||
|
One
|
||||||
|
go racecar go
|
||||||
|
```
|
||||||
|
|
||||||
|
can be piped to `rev` to get the following output:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
❯ rev stuff.md
|
||||||
|
eerhT
|
||||||
|
owT
|
||||||
|
enO
|
||||||
|
og racecar og
|
||||||
|
```
|
||||||
|
|
||||||
|
This is an odd utility that doesn't have too much use that I can imagine. After
|
||||||
|
a brief chat with Claude where I asked for some practical use cases, the one
|
||||||
|
that stood out the most to me is to reverse a list of filenames, sort them, and
|
||||||
|
then reverse them again (putting them back in readable order). This can shuffle
|
||||||
|
filenames with similar endings near each other like source and test files.
|
||||||
|
|
||||||
|
Here is a list of files for me [`py-vmt`
|
||||||
|
project](https://github.com/jbranchaud/py-vmt):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
❯ fd -t f .
|
||||||
|
README.md
|
||||||
|
pyproject.toml
|
||||||
|
src/py_vmt/__init__.py
|
||||||
|
src/py_vmt/cli.py
|
||||||
|
src/py_vmt/session.py
|
||||||
|
src/py_vmt/time_helpers.py
|
||||||
|
tests/src/py_vmt/test_cli.py
|
||||||
|
tests/src/py_vmt/test_session.py
|
||||||
|
```
|
||||||
|
|
||||||
|
Now I can pipe the output of that `fd` command through `rev | sort | rev` to get
|
||||||
|
my files organized in a different way.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
❯ fd -t f . | rev | sort | rev
|
||||||
|
README.md
|
||||||
|
pyproject.toml
|
||||||
|
src/py_vmt/__init__.py
|
||||||
|
tests/src/py_vmt/test_cli.py
|
||||||
|
src/py_vmt/cli.py
|
||||||
|
tests/src/py_vmt/test_session.py
|
||||||
|
src/py_vmt/session.py
|
||||||
|
src/py_vmt/time_helpers.py
|
||||||
|
```
|
||||||
|
|
||||||
|
Again the value of doing something like this is a bit tenuous. At the very least
|
||||||
|
it is fun to know about.
|
||||||
Reference in New Issue
Block a user