0% found this document useful (0 votes)
51 views4 pages

Mastering SED Commands in Linux

The SED command is a stream editor that can perform various functions on plain text files like searching, replacing, inserting, and deleting text. It allows editing files without opening them and supports regular expressions. Some key capabilities of SED include viewing a range of lines, replacing or substituting strings, replacing specific occurrences, replacing on a line number range, and deleting lines or patterns.

Uploaded by

Mohammad Arif
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views4 pages

Mastering SED Commands in Linux

The SED command is a stream editor that can perform various functions on plain text files like searching, replacing, inserting, and deleting text. It allows editing files without opening them and supports regular expressions. Some key capabilities of SED include viewing a range of lines, replacing or substituting strings, replacing specific occurrences, replacing on a line number range, and deleting lines or patterns.

Uploaded by

Mohammad Arif
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Every system administrator has to deal with plain text files on a daily basis.

Knowing how to view certain sections, how to replace words, and how to filter
content from those files. The well-known stream editor is helpful in resolving
these issue.
SED command- stands for stream editor.
1. it can perform various function on file like, searching, find and replace,
insertion or deletion. Most common use of SED command in UNIX is
for substitution or for find and replace.
2. By using SED one can edit files even without opening it, which is much
quicker way to find and replace something in file, than first opening
that file in VI Editor and then changing it.
[Link] is a powerful text stream editor that can do insertion, deletion,
search and replace(substitution).
4. SED command supports regular expression which allows it perform
complex pattern matching.
Several examples---------------------------------------------
Content of a file “[Link]”
unix is great operating system.
unix is opensource.
unix is free operating system.
learn operating system.
unix Linux which one you choose.
Unix is easy to learn.
unix is a multiuser os.
Learn unix.
unix is a powerful.
1. Viewing a range of lines of a document
Tools such as head and tail allow us to view the bottom or the top of a file.
What if we need to view a section in the middle? The following command
will return lines 5 through 10 from [Link]:
Example: sed -n '5,10p' [Link]
2. Replacing or substituting string: 
Sed command is mostly used to replace the text in a file. The below
simple sed command replaces the word “unix” with “linux” in the file.

Example: $sed 's/unix/linux/' [Link]


Here the “s” specifies the substitution operation. The “/” are delimiters.
The “unix” is the search pattern and the “linux” is the replacement
string. By default, the sed command replaces the first occurrence of
the pattern in each line and it won’t replace the second, third…
occurrence in the line

3. Replacing the nth occurrence of a pattern in a line: 


Use the “/1, /2” flags to replace the first, second occurrence of a
pattern in a line.

The below command replaces the second occurrence of the word


“unix” with “linux” in a line.

Example: $sed 's/unix/linux/2' [Link]

4. Replacing all the occurrence of the pattern in a line:

The substitute flag /g (global replacement) specifies the sed


command to replace all the occurrences of the string in the line.

Example: $sed 's/unix/linux/g' [Link]

To ignore case (upper and lower case)


Example: $sed 's/unix/linux/gi' [Link]

5. Replacing from nth occurrence to all occurrences in a line:

 Use the combination of /1, /2 and /g to replace all the patterns from
the nth occurrence of a pattern in a line. The following sed command
replaces the third, fourth, fifth… “unix” word with “linux” word in a line.
Example: $sed 's/unix/linux/3g'[Link]

6. Replacing string on a specific line number: 

You can restrict the sed command to replace the string on a specific
line number. Here n indicate line number to replace the pattern

Example: $sed 'n s/unix/linux/' [Link]

7. Duplicating the replaced line with /p flag: 

The /p print flag prints the replaced line twice on the terminal. If a line
does not have the search pattern and is not replaced, then the /p prints
that line only once.

Example: $sed 's/unix/linux/p' [Link]


8. Printing only the replaced lines: Use the -n option along with
the /p print flag to display only the replaced lines. Here the -n option
suppresses the duplicate rows generated by the /p flag and prints
the replaced lines only one time.

Example: $sed -n 's/unix/linux/p' [Link]

9. Replacing string on a range of lines: 

We can specify a range of line numbers to the sed command for


replacing a string. To print line 1 to 3.
Example1: $sed '1,3 s/unix/linux/' [Link]

Example2: $sed '2,$ s/unix/linux/' [Link]

Here $ indicates the last line in the file. So, the sed command replaces
the text from second line to last line in the file.

Deleting lines from a particular file

SED command can also be used for deleting lines from a particular
file. It is used for performing deletion operation without even opening
the file
Examples:

1. To Delete a particular line, say n in this example

Syntax: $ sed 'nd' [Link]


Example: $ sed '5d' [Link]

2. To Delete a last line


Syntax: $ sed '$d'[Link]
3. . To Delete line from range x to y
Syntax: $ sed 'x,yd' [Link]
Example:$ sed '3,6d' [Link]
4. . To Delete from nth to last line
Syntax: $ sed 'nth,$d' [Link]
Example: $ sed '12,$d' [Link]
5. To Delete pattern matching line
Syntax: $ sed '/pattern/d' [Link]
Example: $ sed '/abc/d' [Link]

Common questions

Powered by AI

The flags '/1', '/2', '/g' in the SED command alter its substitution behavior. By default, SED replaces only the first occurrence in each line, but '/1' and '/2' are used to specify the first and second occurrences for substitution, respectively. The '/g' flag causes substitutions to occur globally for all occurrences within the line. This flexibility allows precise control over which specific instances of a pattern are altered, crucial for tasks where partial updates are necessary without affecting other elements .

Using the -n option with the /p flag in the SED command suppresses the default duplicate printing behavior of the /p flag, ensuring that only the lines with substitutions are printed once, rather than twice. This is useful in reducing output clutter and focusing on relevant changes, especially in scripts where only modified lines need to be reviewed or logged .

Regular expressions play a fundamental role in SED's capabilities, allowing complex pattern matching and text manipulation beyond simple string replacements. They enable SED to perform sophisticated operations such as conditional replacements, pattern-based deletions, and transformations across multiple occurrences and contexts within text streams. This allows users to handle intricate text processing scenarios, making SED an indispensable tool for advanced text editing and scripting tasks where precision and flexibility are required .

The SED command allows users to perform editing operations such as search and replace, insertion, and deletion directly on text files without having to open them in an editor. This is advantageous because it is a much quicker method than manually opening the file in an editor like VI, making batch processing and automation of text transformations much more efficient .

You can use SED to replace a string on a specific line number by specifying the line number before the substitution command, such as $sed '5 s/unix/linux/' myfile.txt. This specificity is important when you want to target modifications precisely, avoiding broad changes that could affect unintended parts of the text, which is crucial in text files where context or position dictates the content's meaning or usage .

Specifying a line range in SED enhances functionality by allowing targeted modifications within a defined section of a file, such as replacing a string only in lines 1 to 3 or from line 2 to the last line. This capability is crucial for efficiently managing large files, enabling focused edits without affecting the entire document, which is beneficial for updating specific sections like headers, footers, or configuration sections without unintended alterations .

The 'g' flag in the SED command is particularly useful when you need to replace all occurrences of a pattern within a line rather than just the first occurrence. This is beneficial in scenarios where a file contains multiple instances of a pattern on the same line and you need consistent replacement throughout, such as updating a repeated word or variable name across a document .

Using the SED command for deleting lines offers benefits such as automation and efficiency, as it allows batch processing of large files without the need for manual intervention or opening files, which is particularly useful for programmatically editing text. However, the potential drawbacks include the possibility of making irreversible errors if commands are executed incorrectly, since there is no interactive feedback or undo functionality as would be available in a text editor .

SED is preferred over traditional file viewing commands like head or tail for viewing specific sections because it allows extraction of arbitrary line ranges, not limited to just the top or bottom of a file. This flexibility enables precise viewing of any file segment directly from the command line, simplifying workflows and enabling easier inspection or editing of specific file sections without needing additional steps to locate them individually using a separate text editor .

Yes, SED can be used for pattern-based deletions in a file, allowing users to delete lines that match a specific pattern. This is highly practical for cleaning up files by removing unnecessary data, such as comments, redundant entries, or sensitive information, enhancing data integrity and readability in logs or configuration files .

You might also like