Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/rtrPublic

Command-line text processing tool

License

NotificationsYou must be signed in to change notification settings

4rtzel/rtr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 fromstdin.

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 athree 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.tomldrwxr-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 LICENSEdrwxr-xr-x 3 4rtzel 4rtzel 4.0K May  6 11:24 srcdrwxr-xr-x 3 4rtzel 4rtzel 4.0K Apr 29 16:41 target

could be presented in the following grid:

-012345678
0-rw-r--r--14rtzel4rtzel134May611:24Cargo.lock
1-rw-r--r--14rtzel4rtzel212May611:24Cargo.toml
2drwxr-xr-x74rtzel4rtzel4.0KMay611:42.git
3-rw-r--r--14rtzel4rtzel8May611:24.gitignore
4-rw-r--r--14rtzel4rtzel1.1KMay611:24LICENSE
5drwxr-xr-x34rtzel4rtzel4.0KMay611:24src
6drwxr-xr-x34rtzel4rtzel4.0KApr2916:41target

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 andto 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 = 0to = -1step = 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 -20dr-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 schedstatdr-xr-xr-x  4 root             root                0 May  6 12:54 scsilrwxrwxrwx  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 swapsdr-xr-xr-x  1 root             root                0 Mar 29 14:00 sys--w-------  1 root             root                0 May  6 12:54 sysrq-triggerdr-xr-xr-x  5 root             root                0 May  6 12:54 sysvipclrwxrwxrwx  1 root             root                0 Mar 29 14:00 thread-self -> 2658052/task/2658052-r--------  1 root             root                0 May  6 12:54 timer_listdr-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 :2dr-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-triggerdr-xr-xr-x 5 root root 0 May 6 12:54 sysvipclrwxrwxrwx 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 ::4dr-xr-xr-x 5 root root 0 May 6 12:54 pressurelrwxrwxrwx 1 root root 0 Mar 29 14:00 self -> 2658052-r--r--r-- 1 root root 0 Mar 29 14:00 swapslrwxrwxrwx 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_listdr-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 ,-1pressuresched_debugschedstatscsi2658052slabinfosoftirqsstatswapssyssysrq-triggersysvipc2658052/task/2658052timer_listttyuptimeversionvmallocinfovmstatzoneinfo

Print last 3 lines but the words are reversed:

$ ll /proc| tail -20| rtr -3:,::-1vmallocinfo 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,,::-1x-rx-rx-rd 5 toor toor 0 yaM 6 45:21 erusserp

Reverse the whole first line:

$ ll /proc| tail -20| rtr 0,::-1,::-1erusserp 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

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp