Stands for “Flow Editor”. “sed” is a utility command that takes file contents or standard input (stdin) and modifies the output via the RegEx pattern. It’s usually a handy command to use to modify the contents of a file.
General syntax for sed
ordered:
$ sed [OPTIONS] [FILE]...
Text Replacement
echo "Text" | sed 's/Replaceable_Word/The_Word_That_Replaces/'
Use the sed
command to find and replace any part of the text. 's'
indicates a search and replace task.
Example: find the word “css” in the text “CSS Libraries” and replacing it with the word “JavaScript”.

Replace text on a specific line in a file
sed '[line] s/harder/easier/g' [file]
the 'g'
option of the sed
The command is used to replace anything that matches the pattern.
Example: replace the word “Stronger” in the first line of libraries.txt case. To note: “Stronger” occurs twice in the line.

Replace first match with new text
sed 's/harder/easier/' [file]
This command replaces only the first match of the search pattern.
Example: Replacement of the first occurrence of the word “css” in the example.txt file with the word “JavaScript”

Delete matching rows
sed '/Something/d' example.txt
Use the d
option of the sed
command to remove any line from a file.
Example: Deleting these lines in the example.txt file containing text “Something”

Find a word case insensitive + delete it
sed '/Sample/Id' example.txt
the I
option of the sed
The command is used to search for a matching pattern regardless of case.
Example: Find a line containing the word ‘sample’ and see the row delete with and without option in the example.txt case.

Replace words with capitals
sed 's/\(libraries\)/\U\1/Ig' example.txt
Use the U
option of the sed
command to convert any text to uppercase letters.
Example: Looking for the word “libraries” in the example.txt file and replacing it with capital letters.

Replace words with lowercase
sed 's/\(libraries\)/\L\1/Ig' example.txt
the L
option of the sed
The command is used to convert any text to lowercase letters.
Example: Looking for the word “libraries” in the example.txt file and replacing it with lowercase letters.

Insert empty lines in a file
sed G [file]
Use the G
option of the sed
command to insert blank lines after each line of the file.
Example: Viewing the file before using the option. Then we use the command and see that after each line blank lines appeared.

Print file line numbers
sed '=' [file]
the =
sign is used to print a line number before each line of text in a file.
Example: As a result of using the command, line numbers appear after each line in the example.txt case.

Source link