Notification texts go here Contact Us Buy Now!

sed separator (delimiter) replacement not working with p command

Addressing Command

Why sed Separator (Delimiter) Replacement Not Working with p Command?

Preamble

sed is a powerful stream editor. It can be used to perform various text editing tasks, including searching, replacing, and deleting text. The s command is used to replace text, and the p command is used to print text.

However, there is a caveat when using the s and p commands together. If the delimiter used in the s command is the same as the delimiter used in the p command, the p command will not work as expected.

Address Range and Address for Commands

The reason for this behavior is that the s and p commands are executed in two different passes. The s command is executed first, and then the p command is executed.

When the s command is executed, it replaces all instances of the specified delimiter with the specified replacement text. This means that when the p command is executed, the delimiter used in the s command is no longer present in the text.

As a result, the p command will not be able to find the delimiter and will not print the text as expected.

Solution

To fix this issue, you need to use a different delimiter for the s and p commands. For example, you can use a backslash \ as the delimiter for the s command and a different character, such as a pipe |, as the delimiter for the p command.

Example

    $ echo "This is a test" | sed -e 's/ /_/g' -e 'p|cat'
    This_is_a_test
  

In this example, the s command uses a space as the delimiter, and the p command uses a pipe as the delimiter. This allows the p command to print the text correctly.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.