Skip to content

sed MacOS Command Guide

The MacOS sed command is a powerful tool for text manipulation. It allows users to perform various text editing operations on files or streams. With sed, you can search for patterns, replace text, delete lines, and more. The syntax of sed may seem daunting at first, but once you understand the basics, you can perform complex text transformations with ease. This guide will cover the essential sed options and commands, along with examples to help you master the MacOS sed command. Whether you need to automate text editing tasks or streamline your workflow, sed is a valuable tool for any MacOS user.

Terminal window
sed [options] [script] [input-file]
OptionDescription
-nSuppresses automatic printing of pattern space.
-eAdd the script to the commands to be executed.
-fRead the script from a file.
-iEdit files in place.
-rUse extended regular expressions.
ParameterDescription
scriptSpecifies the script to be executed.
input-fileSpecifies the input file to be processed.
Terminal window
sed 's/old_text/new_text/g' file.txt

This command searches for all occurrences of “old_text” in “file.txt” and replaces them with “new_text”.

Terminal window
sed -n '/pattern/p' file.txt

Prints only the lines in “file.txt” that match the specified “pattern”.

Terminal window
sed '/^$/d' file.txt

Deletes all empty lines in “file.txt”.

Terminal window
sed -n '$=' file.txt

Counts the number of lines in “file.txt”.

Terminal window
sed 's/^/new_text/' file.txt

Inserts “new_text” at the beginning of each line in “file.txt”.

Terminal window
sed -n '/start_pattern/,/end_pattern/p' file.txt

Prints all lines between “start_pattern” and “end_pattern” in “file.txt”.

Terminal window
sed '3s/old_text/new_text/' file.txt

Replaces “old_text” with “new_text” only in line 3 of “file.txt”.

To use the sed command in MacOS, execute the following command:

Terminal window
sed --option <value>

What is the purpose of the sed command in MacOS?

Section titled “What is the purpose of the sed command in MacOS?”

The sed command in MacOS is primarily used for transforming and manipulating text files. It is particularly useful for performing search and replace operations within a file.

How can I search and replace text using sed in MacOS?

Section titled “How can I search and replace text using sed in MacOS?”

You can search and replace text using sed in MacOS by providing the search pattern and replacement string as arguments.

Terminal window
sed 's/pattern/replacement/g' file.txt

Can I edit a file in place using sed in MacOS?

Section titled “Can I edit a file in place using sed in MacOS?”

Yes, you can edit a file in place using sed in MacOS by using the -i flag.

Terminal window
sed -i '' 's/pattern/replacement/g' file.txt

To delete specific lines from a file using sed in MacOS, you can specify the line numbers or patterns to be deleted.

Terminal window
sed '3d' file.txt

How can I append text to the end of each line using sed in MacOS?

Section titled “How can I append text to the end of each line using sed in MacOS?”

You can append text to the end of each line in a file using sed in MacOS by using the substitute command.

Terminal window
sed 's/$/ new_text/' file.txt
  • Text substitution
  • Text deletion
  • Text insertion
  • Text transformation
  • Text extraction
  • Line numbering
  • Pattern matching and filtering
  • Batch editing of files