sed(Text Editor) command

"I am currently a Software Engineering student at ALX. I'm passionate about technology and enjoy conducting research to find answers on my own. I have a natural inclination to ask 'WHY' more often than 'HOW'.
"While working on projects at ALX, I have acquired a wealth of interesting and diverse knowledge about software engineering and computer science in general. Therefore, I needed a place to store and save all this information, allowing me to refer back to it whenever I forget."
sed
The sed command, short for "stream editor," is a powerful text processing tool commonly used in Unix-like operating systems. It operates on text input, line by line, and allows for various types of editing operations such as substitution, deletion, insertion, and more.
The basic syntax of the sed command is as follows:
sed [options] 'command' [input-file]
Here's a breakdown of the components:
options: Optional flags that modify the behavior ofsed, such as-efor specifying the sed script directly on the command line.'command': The editing command or set of commands enclosed in single quotes.input-file: Optional. Specifies the file to be processed bysed. If not provided,sedreads from standard input.
The editing commands within the single quotes are specified in the form address + action, where:
address: Specifies the lines to which the followingactionshould be applied. It can be a line number, a regular expression, or a range of lines.action: Specifies the operation to be performed on the selected lines, such as substitution (s/.../.../), deletion (d), printing (p), and more.
For example, the sed command with the substitution action (s) is commonly used for search and replace operations. Here's an example that replaces all occurrences of "apple" with "orange" in a file named fruits.txt:
sed 's/apple/orange/g' fruits.txt
This is just a brief introduction to the sed command. It has many advanced features, options, and editing capabilities. To learn more about its usage and available commands, you can refer to the sed manual (man sed).
Examples on how to use sed
Substitute text in a file:
sed 's/pattern/replacement/' file.txtThis command replaces the first occurrence of "pattern" with "replacement" in the file "file.txt". To replace all occurrences, add the
g(global) flag at the end:sed 's/pattern/replacement/g' file.txt.Substitute text and save changes in-place:
sed -i 's/pattern/replacement/' file.txtThis command replaces the first occurrence of "pattern" with "replacement" in the file "file.txt" and saves the changes directly to the file.
The
-ioption in thesedcommand stands for "in-place editing." When used withsed, it allows you to modify a file directly, saving the changes to the original file instead of just displaying the modified output on the terminal.Delete lines from a file matching a pattern:
sed '/pattern/d' file.txtThis command deletes all lines in "file.txt" that contain the specified "pattern".
Print specific lines from a file:
sed -n '5,10p' file.txtThis command prints lines 5 to 10 from "file.txt". The
-noption suppresses automatic printing, andpat the end instructssedto print the specified lines.
"Automatic printing" refers to the default behavior ofsedto print each line after applying the specified commands, whereas the-noption allows you to suppress this behavior and selectively print specific lines as desired.Append text after a specific line:
sed '/pattern/a\New line of text' file.txtThis command finds the first occurrence of "pattern" in "file.txt" and appends "New line of text" immediately after that line.
The backslash (\) is used to escape the newline character (\n) and indicate that the text "New line of text" is part of the same line as theacommand.
Without the backslash, the newline character would be interpreted as the end of theacommand and a syntax error would occur. By escaping the newline character with a backslash,sedtreats it as part of the text to be appended rather than as a command delimiter.Replace text only on specific lines:
sed '10s/pattern/replacement/' file.txtThis command replaces the first occurrence of "pattern" with "replacement" only on line 10 in "file.txt". To replace on multiple lines, use a range like
5,10.Delete lines from a file based on line numbers:
sed '3d;7,10d' file.txtThis command deletes line 3 and lines 7 to 10 from "file.txt". Multiple expressions can be separated by a semicolon.
Insert text before a specific line:
sed '/pattern/i\New line of text' file.txtThis command finds the first occurrence of "pattern" in "file.txt" and inserts "New line of text" immediately before that line.
Transform text using regular expressions:
sed 's/\bword\b/replacement/g' file.txtThis command replaces all occurrences of "word" as a whole word with "replacement" in "file.txt". The
\bdenotes word boundaries.
\b: This is a regular expression construct representing a word boundary. It matches the position between a word character (such as letters or digits) and a non-word character (such as whitespace or punctuation marks). It ensures that the pattern "word" is matched only as a whole word, not as part of a longer word.word: This is the pattern to be matched. It represents the word you want to find and replace.\b: This is another word boundary, indicating the end of the pattern.Execute multiple editing commands:
sed -e 's/pattern1/replacement1/' -e 's/pattern2/replacement2/' file.txtThis command applies multiple editing commands sequentially. In this example, it replaces "pattern1" with "replacement1" and then replaces "pattern2" with "replacement2" in "file.txt".



