Masking tape to help commands "do one thing well"
Masking tape to help commands "do one thing well"
$ cat /var/log/secure | teip -c 1-15 -- date -f- +%s$ cat file | teip -g HELLO -- sed 's/WORLD/EARTH/'$ cat file.csv | teip --csv -f 2 -- tr a-z A-Z$ cat file.tsv | teip -D '\t' -f 2-4 -- tr a-z A-Z$ cat access.log | teip -e 'grep -n -C 3 hello' -- sed 's/./@/g'teip allows a command to focus on its own task.
Here is a comparison of the processing time to replace approx 761,000 IP addresses with dummy ones in a 100 MiB text file.
See detail at wiki > Benchmark.
Taping: Help the command "do one thing well"
awk, cut or grep)High performance
teip threads asynchronously.teip can do the same or better performance.Install Homebrew, and
brew install teipdpkgwget https://github.com/greymd/teip/releases/download/v2.3.3/teip-2.3.2.$(uname -m)-unknown-linux-musl.deb
sudo dpkg -i ./teip*.debaptwget https://github.com/greymd/teip/releases/download/v2.3.3/teip-2.3.2.$(uname -m)-unknown-linux-musl.deb
sudo apt install ./teip*.debdnfsudo dnf install https://github.com/greymd/teip/releases/download/v2.3.3/teip-2.3.2.$(uname -m)-unknown-linux-musl.rpmyumsudo yum install https://github.com/greymd/teip/releases/download/v2.3.3/teip-2.3.2.$(uname -m)-unknown-linux-musl.rpmIf necessary, check the hash value from the latest release page.
Files whose filenames end with sha256 have hash values listed.
Download installer from here.
See Wiki > Use on Windows for detail.
Check the latest release page for executables for the platform you are using.
If not present, please build teip from source.
With Rust's package manager cargo
cargo install teipTo enable Oniguruma regular expression (-G option), build with --features oniguruma option.
Please make sure the libclang shared library is available in your environment.
### Ubuntu
$ sudo apt install cargo clang
$ cargo install teip --features oniguruma### Red Hat base OS
$ sudo dnf install cargo clang
$ cargo install teip --features oniguruma### Windows (PowerShell) and choco (chocolatey.org)
PS C:\> choco install llvm
PS C:\> cargo install teip --features oniguruma…Try this at first:
$ echo "100 200 300 400" | teip -f 3The result is almost the same as the input, but "300" is highlighted and surrounded by [...],
because -f 3 specifies the 3rd field of space-separated input.
100 200 [300] 400Understand that the area enclosed in [...] is a hole in the masking tape.
Next, put the sed and its arguments at the end.
$ echo "100 200 300 400" | teip -f 3 sed 's/./@/g'The result is as below.
The highlight and [...] will not be present when a command is added.
100 200 @@@ 400As you can see, the sed command only acted on the input defined by the "hole" and ignored the masked
parts. Technically, teip passes only the highlighted part to the sed process, and replaces the
highlighted part with the result of the sed command.
Of course, any command you like can be specified. It is called the targeted command in this article.
Let's try cut as the targeted command, to extract the first character only.
$ echo "100 200 300 400" | teip -f 3 cut -c 1
teip: Invalid arguments.Oops! Why did this fail?
This is because the cutcommand uses the -c option.
An option of the same name is also provided by teip, which is confusing.
When specifying a targeted command to teip, it is better to give it after --.
Then, teip interprets any arguments after -- as the targeted command and its arguments.
$ echo "100 200 300 400" | teip -f 3 -- cut -c 1
100 200 3 400Great — the first character 3 is extracted from 300!
Although -- is not always necessary, it is always better to use it.
So, -- is used in all the examples from here on.
Now let's double these number with awk.
The command looks like the following (Note that the variable to be doubled is not $3).
$ echo "100 200 300 400" | teip -f 3 -- awk '{print $1*2}'
100 200 600 400OK, the selection in the "hole" went from 300 to 600.
Now, let's change -f 3 to -f 3,4 and run teip.
$ echo "100 200 300 400" | teip -f 3,4 -- awk '{print $1*2}'
100 200 600 800The numbers in the 3rd and 4th fields were doubled!
As you may have noticed, the argument to -f is compatible with the LIST of cut.
You can refer to cut --help to see how it works.
Examples:
$ echo "100 200 300 400" | teip -f -3 -- sed 's/./@/g'
@@@ @@@ @@@ 400
$ echo "100 200 300 400" | teip -f 2-4 -- sed 's/./@/g'
100 @@@ @@@ @@@
$ echo "100 200 300 400" | teip -f 1- -- sed 's/./@/g'
@@@ @@@ @@@ @@@The -c option allows you to specify a range by character.
The below example is specifying the 1st, 3rd, 5th, 7th characters and applying the sed command to them.
$ echo ABCDEFG | teip -c 1,3,5,7
[A]B[C]D[E]F[G]
$ echo ABCDEFG | teip -c 1,3,5,7 -- sed 's/./@/'
@B@D@F@Like -f, the argument to -c is compatible with cut's LIST.
The -f option recognizes delimited fields like awk by default.
Any continuous whitespace (all forms of whitespace categorized by Unicode) is interpreted as a single delimiter.
$ printf "A B \t\t\t\ C \t D" | teip -f 3 -- sed s/./@@@@/
A B @@@@ C DThis behavior might be inconvenient for the processing of CSV and TSV.
However, the -d option in conjunction with -f can be used to specify a delimiter.
You can process a simple CSV file like this:
$ echo "100,200,300,400" | teip -f 3 -d , -- sed 's/./@/g'
100,200,@@@,400In order to process TSV, the TAB character must be given at the command line.
If you are using Bash, type $'\t' which is in the form of ANSI-C Quoting.
$ printf "100\t200\t300\t400\n" | teip -f 3 -d $'\t' -- sed 's/./@/g'
100 200 @@@ 400teip also provides -D option to specify an extended regular expression as the delimiter.
This is useful when you want to ignore consecutive delimiters, or when there are multiple types of delimiter.
$ echo 'A,,,,,B,,,,C' | teip -f 2 -D ',+'
A,,,,,[B],,,,C$ echo "1970-01-02 03:04:05" | teip -f 2-5 -D '[-: ]'
1970-[01]-[02] [03]:[04]:05The TAB character regular expression (\t) can also be specified with the -D option.
$ printf "100\t200\t300\t400\n" | teip -f 3 -D '\t' -- sed 's/./@/g'
100 200 @@@ 400For the available regular expression notations, refer to regular expression of Rust.
If you want to process a complex CSV file, such as the one below, which has columns surrounded by double quotes, use the -f option together with the --csv option.
Name,Address,zipcode
Sola Harewatar,"Doreami Road 123
Sorashido city",12877
Yui Nagomi,"Nagomi Street 456, Nagomitei, Oishina town",26930-0312
"Conectol Motimotit Hooklala Glycogen Comex II a.k.a ""Kome kome""","Cooking dam",513123With --csv, teip will parse the input as a CSV file according to RFC4180. Thus, you can use -f to specify column numbers for CSV files with complex structures.
For example, the CSV above will have a "hole" as shown below.
$ cat tests/sample.csv | teip --csv -f2
Name,[Address],zipcode
Sola Harewatar,["Doreami Road 123]
[Sorashido city"],12877
Yui Nagomi,["Nagomi Street 456, Nagomitei, Oishina town"],26930-0312
"Conectol Motimotit Hooklala Glycogen Comex II a.k.a ""Kome kome""",["Cooking dam"],513123Because -f2 was specified, there is a hole in the second column of each row.
The following command is an example of rewriting all characters in the second column to "@".
$ cat tests/sample.csv | teip --csv -f2 -- sed 's/[^"]/@/g'
Name,@@@@@@@,zipcode
Sola Harewatar,"@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@",12877
Yui Nagomi,"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@",26930-0312
"Conectol Motimotit Hooklala Glycogen Comex II a.k.a ""Kome kome""","@@@@@@@@@@@",513123Notes for the --csv option:
" surrounding fields are also included in the holes."" are treated as-is; two double quotes "" are given as input to the targeted command.-s or -z option is used, such a field is treated as a single hole, and line breaks are included.You can also use -g to select a specific line matching a regular expression as the hole location.
$ echo -e "ABC1\nEFG2\nHIJ3" | teip -g '[GJ]\d'
ABC1
[EFG2]
[HIJ3]By default, the entire line containing the pattern is the range of holes. With the -o option, the range of the holes will ony cover the matched range.
$ echo -e "ABC1\nEFG2\nHIJ3" | teip -og '[GJ]\d'
ABC1
EF[G2]
HI[J3]Note that -og is one of the most useful idioms and is frequently used in this manual.
Here is an example using \d, which matches numbers.
$ echo ABC100EFG200 | teip -og '\d+'
ABC[100]EFG[200]
$ echo ABC100EFG200 | teip -og '\d+' -- sed 's/.*/@@@/g'
ABC@@@EFG@@@This feature is quite versatile and can be useful for handling files that have no fixed form such as logs, markdown, etc.
teip passes the strings from the hole line-by-line, so that each hole is one line of input.
Therefore, a targeted command must follow the below rule.
In the simplest example, the cat command always succeeds,
because the cat prints the same number of lines as it is given in input.
$ echo ABCDEF | teip -og . -- cat
ABCDEFIf the above rule is not satisfied, the result will be inconsistent.
For example, grep may fail.
Here is an example.
$ echo ABCDEF | teip -og .
[A][B][C][D][E][F]
$ echo ABCDEF | teip -og . -- grep '[ABC]'
ABC
teip: Output of given command is exhausted
$ echo $?
1teip did not receive results corresponding to the holes of D, E, and F.
That is why the above exampl
No open issues yet, or sync has not completed.