Skip to content

Latest commit

 

History

History
224 lines (192 loc) · 7.57 KB

README.md

File metadata and controls

224 lines (192 loc) · 7.57 KB

rtr

rtr is a command-line tool for text processing based on Python-like slice syntax.

Usage

rtr slice [file]

slice is a mandatory argument that specifies a slice that will be used for text processing.

file is an optional argument that specifies a filename to read the input from. If it's omitted then the input will be read from stdin.

Features

GridSlice

Provides a way to perform text filtering and transformation based on a simple Python-like slice indexing. All the text could be treated like a three dimensional array where: each line represents a row (1D), each word in the line represents a column (2D), and each character in the word represents a depth (3D). For example this text:

-rw-r--r-- 1 4rtzel 4rtzel  134 May  6 11:24 Cargo.lock
-rw-r--r-- 1 4rtzel 4rtzel  212 May  6 11:24 Cargo.toml
drwxr-xr-x 7 4rtzel 4rtzel 4.0K May  6 11:42 .git
-rw-r--r-- 1 4rtzel 4rtzel    8 May  6 11:24 .gitignore
-rw-r--r-- 1 4rtzel 4rtzel 1.1K May  6 11:24 LICENSE
drwxr-xr-x 3 4rtzel 4rtzel 4.0K May  6 11:24 src
drwxr-xr-x 3 4rtzel 4rtzel 4.0K Apr 29 16:41 target

could be presented in the following grid:

- 0 1 2 3 4 5 6 7 8
0 -rw-r--r-- 1 4rtzel 4rtzel 134 May 6 11:24 Cargo.lock
1 -rw-r--r-- 1 4rtzel 4rtzel 212 May 6 11:24 Cargo.toml
2 drwxr-xr-x 7 4rtzel 4rtzel 4.0K May 6 11:42 .git
3 -rw-r--r-- 1 4rtzel 4rtzel 8 May 6 11:24 .gitignore
4 -rw-r--r-- 1 4rtzel 4rtzel 1.1K May 6 11:24 LICENSE
5 drwxr-xr-x 3 4rtzel 4rtzel 4.0K May 6 11:24 src
6 drwxr-xr-x 3 4rtzel 4rtzel 4.0K Apr 29 16:41 target

and each cell will have an additional dimensional for characters.

We then could use a Python-like slice indexing to extract the text that we want. The indexing syntax looks like that:

range = <from>:<to>:<step>
slice = range,range,range
  • <from> -- first index to extract text from (e.g. 20: will extract text from index 20 onward).
  • <to> -- last index to extract text from (e.g. :30 will extract all text before index 31).
  • <step> -- step to use for text extraction (e.g. ::2 will extract every second index).

from and to could have negative values. In that case, the actual number will be calculated by subtracting this value from the input's length (e.g. -5: means the fifth index from the end).

step could also be negative (but not 0). In that case, the output will be reversed (e.g. ::-2 for step means to reverse the output and extract every second word).

All these indexes have default values and thus could be omitted. The default values are the following:

from = 0
to = -1
step = 1

Additionally, each range could be prefixed with "!" to exclude this particular range. So !1:3 will output every line except for lines from 1 to 3.

Another example: if you want to print only the first and the last line one could use: !1:-2.

Examples

We'll be using the following input for all examples belove:

$ ll /proc | tail -20
dr-xr-xr-x  5 root             root                0 May  6 12:54 pressure
-r--r--r--  1 root             root                0 May  6 12:54 sched_debug
-r--r--r--  1 root             root                0 May  6 12:54 schedstat
dr-xr-xr-x  4 root             root                0 May  6 12:54 scsi
lrwxrwxrwx  1 root             root                0 Mar 29 14:00 self -> 2658052
-r--------  1 root             root                0 May  6 12:54 slabinfo
-r--r--r--  1 root             root                0 May  6 12:18 softirqs
-r--r--r--  1 root             root                0 May  6 12:18 stat
-r--r--r--  1 root             root                0 Mar 29 14:00 swaps
dr-xr-xr-x  1 root             root                0 Mar 29 14:00 sys
--w-------  1 root             root                0 May  6 12:54 sysrq-trigger
dr-xr-xr-x  5 root             root                0 May  6 12:54 sysvipc
lrwxrwxrwx  1 root             root                0 Mar 29 14:00 thread-self -> 2658052/task/2658052
-r--------  1 root             root                0 May  6 12:54 timer_list
dr-xr-xr-x  6 root             root                0 May  6 12:54 tty
-r--r--r--  1 root             root                0 May  6 12:18 uptime
-r--r--r--  1 root             root                0 May  6 12:54 version
-r--------  1 root             root                0 May  6 12:54 vmallocinfo
-r--r--r--  1 root             root                0 May  6 12:54 vmstat
-r--r--r--  1 root             root                0 May  6 12:54 zoneinfo

in the following way:

ll /proc | tail -20 | rtr <slice>

Print all lines from line 15:

$ ll /proc | tail -20 | rtr 15:
-r--r--r-- 1 root root 0 May 6 12:18 uptime
-r--r--r-- 1 root root 0 May 6 12:54 version
-r-------- 1 root root 0 May 6 12:54 vmallocinfo
-r--r--r-- 1 root root 0 May 6 12:54 vmstat
-r--r--r-- 1 root root 0 May 6 12:54 zoneinfo

Print first 3 lines (indexes start at 0):

$ ll /proc | tail -20 | rtr :2
dr-xr-xr-x 5 root root 0 May 6 12:54 pressure
-r--r--r-- 1 root root 0 May 6 12:54 sched_debug
-r--r--r-- 1 root root 0 May 6 12:54 schedstat

Print lines from 10 to 12:

$ ll /proc | tail -20 | rtr 10:12
--w------- 1 root root 0 May 6 12:54 sysrq-trigger
dr-xr-xr-x 5 root root 0 May 6 12:54 sysvipc
lrwxrwxrwx 1 root root 0 Mar 29 14:00 thread-self -> 2658052/task/2658052

Print last 3 lines:

$ ll /proc | tail -20 | rtr -3:
-r-------- 1 root root 0 May 6 12:54 vmallocinfo
-r--r--r-- 1 root root 0 May 6 12:54 vmstat
-r--r--r-- 1 root root 0 May 6 12:54 zoneinfo

From the last 5 lines print the first 2:

$ ll /proc | tail -20 | rtr -5:-4
-r--r--r-- 1 root root 0 May 6 12:18 uptime
-r--r--r-- 1 root root 0 May 6 12:54 version

Print only line 10:

$ ll /proc | tail -20 | rtr 10
--w------- 1 root root 0 May 6 12:54 sysrq-trigger

Print every 4th line:

$ ll /proc | tail -20 | rtr ::4
dr-xr-xr-x 5 root root 0 May 6 12:54 pressure
lrwxrwxrwx 1 root root 0 Mar 29 14:00 self -> 2658052
-r--r--r-- 1 root root 0 Mar 29 14:00 swaps
lrwxrwxrwx 1 root root 0 Mar 29 14:00 thread-self -> 2658052/task/2658052
-r--r--r-- 1 root root 0 May 6 12:54 version

Print lines from 10 to 15 with the step of 2 reversed:

$ ll /proc | tail -20 | rtr 10:15:-2
-r--r--r-- 1 root root 0 May 6 12:18 uptime
-r-------- 1 root root 0 May 6 12:54 timer_list
dr-xr-xr-x 5 root root 0 May 6 12:54 sysvipc

Print only the last word in each line:

$ ll /proc | tail -20 | rtr ,-1
pressure
sched_debug
schedstat
scsi
2658052
slabinfo
softirqs
stat
swaps
sys
sysrq-trigger
sysvipc
2658052/task/2658052
timer_list
tty
uptime
version
vmallocinfo
vmstat
zoneinfo

Print last 3 lines but the words are reversed:

$ ll /proc | tail -20 | rtr -3:,::-1
vmallocinfo 12:54 6 May 0 root root 1 -r--------
vmstat 12:54 6 May 0 root root 1 -r--r--r--
zoneinfo 12:54 6 May 0 root root 1 -r--r--r--

Reverse all characters in each word in line 1:

$ ll /proc | tail -20 | rtr 0,,::-1
x-rx-rx-rd 5 toor toor 0 yaM 6 45:21 erusserp

Reverse the whole first line:

$ ll /proc | tail -20 | rtr 0,::-1,::-1
erusserp 45:21 6 yaM 0 toor toor 5 x-rx-rx-rd

Print only the first character in each word for the last line:

$ ll /proc | tail -20 | rtr -1,,0
- 1 r r 0 M 6 1 z

Print only the first and the last word in the last three lines:

$ ll /proc | tail -20 | rtr '-3:,!1:-2'
-r-------- vmallocinfo
-r--r--r-- vmstat
-r--r--r-- zoneinfo