Question: If I have a file called `text.txt`
and it contains one line of text, `ijbiaubiu`
is it possible to move each letter one space forward or back in the alphabet in order to encrypt it manually, with what I believe would be a ROT cipher?

How is it done? 

I have tried `sed` and `perl -pi -e` but I don't know how to read the contents of the file before applying commands, and these utilities never did the trick.

To clarify, I am not looking for help encrypting my files.
Rather, with help learning how to use the CLI to shift about alphabetical characters in a file manually.

Answer: 
`sed`
-----

can do that with [the transform command `y/…/…/`](http://www.grymoire.com/Unix/Sed.html#uh-49), e.g. to replace “a” with “b” and so forth:

```
sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'

```

or with capitals:

```
sed 'y/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA/'

```

To simplify this you can use `bash` Parameter expansion, to be exact [substring expansion](http://wiki.bash-hackers.org/syntax/pe#substring_expansion):

```
a=abcdefghijklmnopqrstuvwxyz
b=ABCDEFGHIJKLMNOPQRSTUVWXYZ
r=1
sed "y/$a$b/${a:$r}${a::$r}${b:$r}${b::$r}/"

```

To alter the rotation just set `r` to the desired rotation, e.g. `r=13` for ROT13. It works with negative numbers as well. The same approach can be used for the below `perl` version.

To read from a file I recommend redirection ([here’s why](https://unix.stackexchange.com/a/70759/246819)), e.g. `<text.txt sed …`, see below for an example. You can however also give the file as an argument to `sed`, e.g. `sed '…' text.txt`.

### Example run

```
$ <<<"ijbiaubiu" sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv
$ <<<"ijbiaubiu" sed 'y/abcdefghijklmnopqrstuvwxyz/zabcdefghijklmnopqrstuvwxy/'
hiahztaht
$ echo "ijbiaubiu" >rot_file
$ <rot_file sed 'y/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv

```

---

`perl`
------

has an easy-to-use transliterator tool called `tr`, e.g. to replace “a” with “b” and so forth:

```
perl -npe 'tr/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'

```

`-n` is to iterate over filename arguments and `-p` to print lines afterwards, same as adding `;print` to the expression would do.

### Example run

```
$ <<<"ijbiaubiu" perl -npe 'tr/abcdefghijklmnopqrstuvwxyz/bcdefghijklmnopqrstuvwxyza/'
jkcjbvcjv

```
```
$ cat text.txt 
ijbiaubiu

```

A 1-rotation with Python, after setting `ROT = 1`:

```
$ python -c "with open('text.txt') as f: ROT = 1; print(''.join([chr((ord(char)- ord('a') + ROT)%26+ord('a')) for char in f.read().strip()]))"
jkcjbvcjv

```

Or setting `ROT = -1`:

```
$ python -c "with open('text.txt') as f: ROT = -1; print(''.join([chr((ord(char)- ord('a') + ROT)%26+ord('a')) for char in f.read().strip()]))"
hiahztaht

```

What it's doing is:

1. reading letters in from the file,
2. getting their integer (or **ord**inal) values,
3. subtracting the ordinal value of lowercase `a` to get a number from 0 - 25,
4. adding the rotation value,
5. taking `% 26` so after `z` you wrap around to `a` if necessary,
6. adding the ordinal value of lowercase `a` back and
7. converting the ordinal value to a string value.

If you know Python, it might be easier to read if it's not a 1-liner:

```
ROT = 1

def rotate_char(char, rotate_amount):
    return chr((ord(char) - ord('a') + rotate_amount)%26 + ord('a'))

with open('text.txt') as f:
    line = f.read().strip()
    print(''.join([rotate_char(char, ROT) for char in line]))

```