Skip to content
This repository was archived by the owner on Jun 5, 2024. It is now read-only.

Commit df809f8

Browse files
transliteration examples
1 parent e423306 commit df809f8

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

perl_the_swiss_knife.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
* [split](#split)
4848
* [Fixed width processing](#fixed-width-processing)
4949
* [String and file replication](#string-and-file-replication)
50+
* [transliteration](#transliteration)
5051
* [Executing external commands](#executing-external-commands)
5152
* [Further Reading](#further-reading)
5253

@@ -3006,6 +3007,97 @@ $ perl -le '@x=glob "{1,3}" x 3; print "@x"'
30063007
30073008
<br>
30083009
3010+
#### <a name="transliteration"></a>transliteration
3011+
3012+
* See `tr` under [perldoc - Quote-Like Operators](https://perldoc.perl.org/perlop.html#Quote-Like-Operators) section for details
3013+
* similar to substitution, by default `tr` acts on `$_` variable and modifies it unless `r` modifier is specified
3014+
* however, characters `$` and `@` are treated as literals - i.e no interpolation
3015+
* similar to `sed`, one can also use `y` instead of `tr`
3016+
3017+
```bash
3018+
$ # one-to-one mapping of characters, all occurrences are translated
3019+
$ echo 'foo bar cat baz' | perl -pe 'tr/abc/123/'
3020+
foo 21r 31t 21z
3021+
3022+
$ # use - to represent a range in ascending order
3023+
$ echo 'Hello World' | perl -pe 'tr/a-zA-Z/n-za-mN-ZA-M/'
3024+
Uryyb Jbeyq
3025+
$ echo 'Uryyb Jbeyq' | perl -pe 'tr|a-zA-Z|n-za-mN-ZA-M|'
3026+
Hello World
3027+
```
3028+
3029+
* if arguments are of different lengths
3030+
3031+
```bash
3032+
$ # when second argument is longer, the extra characters are ignored
3033+
$ echo 'foo bar cat baz' | perl -pe 'tr/abc/1-9/'
3034+
foo 21r 31t 21z
3035+
3036+
$ # when first argument is longer
3037+
$ # the last character of second argument gets padded to make it equal
3038+
$ echo 'foo bar cat baz' | perl -pe 'tr/a-z/123/'
3039+
333 213 313 213
3040+
```
3041+
3042+
* modifiers
3043+
3044+
```bash
3045+
$ # no padding, absent mappings are deleted
3046+
$ echo 'fob bar cat baz' | perl -pe 'tr/a-z/123/d'
3047+
2 21 31 21
3048+
$ echo 'Hello:123:World' | perl -pe 'tr/a-z//d'
3049+
H:123:W
3050+
3051+
$ # c modifier complements first argument characters
3052+
$ echo 'Hello:123:World' | perl -lpe 'tr/a-z//cd'
3053+
elloorld
3054+
3055+
$ # s modifier to keep only one copy of repeated characters
3056+
$ echo 'FFoo seed 11233' | perl -pe 'tr/a-z//s'
3057+
FFo sed 11233
3058+
$ # when replacement is done as well, only replaced characters are squeezed
3059+
$ # unlike 'tr -s' which squeezes characters specified by second argument
3060+
$ echo 'FFoo seed 11233' | perl -pe 'tr/A-Z/a-z/s'
3061+
foo seed 11233
3062+
3063+
$ perl -e '$x="food"; $y=$x=~tr/a-z/A-Z/r; print "x=$x\ny=$y\n"'
3064+
x=food
3065+
y=FOOD
3066+
```
3067+
3068+
* since `-` is used for character ranges, place it at the start/end to represent it literally
3069+
* similarly, to represent `\` literally, use `\\`
3070+
3071+
```bash
3072+
$ echo '/foo-bar/baz/report' | perl -pe 'tr/-a-z/_A-Z/'
3073+
/FOO_BAR/BAZ/REPORT
3074+
3075+
$ echo '/foo-bar/baz/report' | perl -pe 'tr|/-|\\_|'
3076+
\foo_bar\baz\report
3077+
```
3078+
3079+
* return value is number of replacements made
3080+
3081+
```bash
3082+
$ echo 'Hello there. How are you?' | grep -o '[a-z]' | wc -l
3083+
17
3084+
3085+
$ echo 'Hello there. How are you?' | perl -lne 'print tr/a-z//'
3086+
17
3087+
```
3088+
3089+
* unicode examples
3090+
3091+
```bash
3092+
$ echo 'hello!' | perl -CS -pe 'tr/a-z/\x{1d5ee}-\x{1d607}/'
3093+
𝗵𝗲𝗹𝗹𝗼!
3094+
3095+
$ echo 'How are you?' | perl -Mopen=locale -Mutf8 -pe 'tr/a-zA-Z/𝗮-𝘇𝗔-𝗭/'
3096+
𝗛𝗼𝘄 𝗮𝗿𝗲 𝘆𝗼𝘂?
3097+
```
3098+
3099+
<br>
3100+
30093101
#### <a name="executing-external-commands"></a>Executing external commands
30103102
30113103
* External commands can be issued using `system` function

0 commit comments

Comments
 (0)