CIS120Linux Fundementals
tr, sed and aspell Commands
tr Command
The tr
command in Linux is used for translating or deleting characters from the input provided. It reads from standard input and writes to standard output.
Common Options for tr
:
Option | Description |
---|---|
-d |
Delete characters from the input |
-s |
Squeeze repeated characters into a single character |
-c |
Complement the set of characters |
Examples:
Consider a file example.txt
with the following content:
hello world
hello universe
To convert all lowercase characters to uppercase, you can use:
cat example.txt | tr 'a-z' 'A-Z'
Output:
HELLO WORLD
HELLO UNIVERSE
To delete all vowels from the text:
cat example.txt | tr -d 'aeiou'
Output:
hll wrld
hll nvrse
To change the blank space between two words to a hyphen:
cat example.txt | tr ' ' '-'
Output:
hello-world
hello-universe
To change all blank spaces (including tabs) to a hyphen using [[:blank:]]
:
cat example.txt | tr '[[:blank:]]' '-'
Output:
hello-world
hello-universe
[[:blank:]]
is a character class that matches all horizontal whitespace characters, which include spaces and tabs. This is useful when you want to ensure that all types of horizontal whitespace are replaced.
sed Command
The sed
command, short for stream editor, is used to perform basic text transformations on an input stream (a file or input from a pipeline).
Common Options for sed
:
Option | Description |
---|---|
-e |
Add the script to the commands to be executed |
-f |
Add the script file |
-i |
Edit files in place |
-n |
Suppress automatic printing of pattern space |
Examples:
Consider a file example.txt
with the following content:
hello world
hello hello universe
hello everyone hello
To replace the first occurrence of the word "hello" with "hi" on each line:
sed 's/hello/hi/' example.txt
Output:
hi world
hi hello universe
hi everyone hello
To replace all occurrences of the word "hello" with "hi" using the global /g
option:
sed 's/hello/hi/g' example.txt
Output:
hi world
hi hi universe
hi everyone hi
To delete lines containing the word "everyone":
sed '/everyone/d' example.txt
Output:
hello world
hello hello universe
aspell Command
The aspell
command is a spell-checking utility in Linux. It can be used interactively to check the spelling of a document.
Common Options for aspell
:
Option | Description |
---|---|
-c |
Check a file |
-a |
Run in 'pipe mode' |
-l |
List available dictionaries |
-d |
Use a specific dictionary |
Examples:
Consider a file example.txt
with the following content:
helo wrld
hello everyone
To check the spelling of the file:
aspell check example.txt
Interactive mode will prompt corrections:
@(#) International Ispell Version 3.1.20 (but really Aspell 0.60.8)
& helo 5 0: hello, he lo, halo, help, helot
& wrld 8 0: world, word, weld, wild, wold, wrld's, WRLD, WLRD
To list available dictionaries:
aspell dicts
Output might include:
en
en_GB
en_US
fr
de
...
These commands are essential for text processing and transformation tasks in Linux, providing powerful tools for manipulating and validating text in various ways.