1. Change a Letter or Number in a Text File to a Different One (tr)
The tr (translate) command is used to replace characters in a text file or stream. It can perform a simple character-by-character replacement or delete characters.
Basic Syntax:
tr [SET1] [SET2]Where:
SET1is the set of characters to be replaced.SET2is the set of characters to replace them with.
Example 1: Change a Letter
If you want to replace the letter a with z in a text file, you can use:
tr 'a' 'z' < input.txtThis will replace all occurrences of a with z in input.txt.
Example 2: Change Multiple Letters
If you want to replace multiple letters at once, you can specify multiple characters in each set. For example, replacing a with x and b with y:
tr 'ab' 'xy' < input.txtThis will replace:
a→xb→y
Example 3: Replace Numbers
You can replace digits too. For example, to replace the number 1 with 9:
tr '1' '9' < input.txtIf you wanted to replace all digits (0-9) with a different set, like turning them into letters (e.g., 1 → a, 2 → b), use:
tr '0123456789' 'abcdefghij' < input.txt2. Translate a Text File into Octal and Hexadecimal Formats (od)
The od (octal dump) command is used to display the contents of a file in various formats, including octal, hexadecimal, ASCII, and others.
Basic Syntax:
od -[format] [file]Where:
[format]specifies the format in which to display the content, such as-xfor hexadecimal,-ofor octal, etc.[file]is the file to be displayed.
Example 1: Hexadecimal Format
To display the contents of file.txt in hexadecimal format, use:
od -x file.txtThis will show the file’s contents in hexadecimal pairs (byte-by-byte).
Example 2: Octal Format
To display the contents in octal format, use:
od -o file.txtThis will show the file’s contents in octal (base 8).
Example 3: ASCII with Hexadecimal
If you want to see both ASCII characters and their corresponding hexadecimal values:
od -c -x file.txtHere:
-cshows the ASCII characters.-xshows the hexadecimal representation.
Example output:
0000000 T h i s i s a t e s t .
0000014
3. Stream Editor for Filtering and Transforming Text (sed)
The sed (stream editor) command is one of the most powerful tools in Linux for text manipulation. It processes a file line by line, and you can use it to perform a wide range of transformations like replacing text, deleting lines, inserting text, etc.
Basic Syntax:
sed 's/pattern/replacement/' fileWhere:
sstands for substitution.patternis the text you want to replace.replacementis the text to replace the pattern with.
Example 1: Replace Text in a File
To replace apple with orange in a file:
sed 's/apple/orange/' input.txtThis replaces the first occurrence of apple with orange on each line.
Example 2: Replace All Occurrences
To replace all occurrences of apple with orange in the file:
sed 's/apple/orange/g' input.txtThe g after the replacement means global replacement (replace all occurrences on a line).
Example 3: Delete a Line Containing a Pattern
To delete lines that contain the word apple:
sed '/apple/d' input.txtThis will delete all lines that contain the word apple.
Example 4: Delete Blank Lines
To delete all blank lines from a file:
sed '/^$/d' input.txtThis matches empty lines (^$ stands for lines that start and end with nothing) and deletes them.
Example 5: In-place Editing
To edit the file directly (i.e., change the original file rather than outputting to the screen), use the -i option:
sed -i 's/apple/orange/g' input.txtThis will replace all instances of apple with orange in input.txt, modifying the file in place.
Example 6: Insert Text Before or After a Line
To insert a line of text before or after a line containing a pattern:
- Insert before a line:
sed '/pattern/i\This is the inserted line' input.txt- Insert after a line:
sed '/pattern/a\This is the inserted line' input.txtExample 7: Replace the N-th Occurrence
You can replace only the N-th occurrence of a pattern on a line. For example, replacing only the second occurrence of apple with orange:
sed 's/apple/orange/2' input.txtSummary of Commands:
tr: Used for character replacement in text files (e.g.,tr 'a' 'z').od: Converts text files into different formats like octal, hexadecimal, ASCII, etc. (e.g.,od -xfor hexadecimal).sed: Stream editor for transforming text in files (e.g.,sed 's/old/new/'for substitution).