user2632704
user2632704

Reputation: 71

Deleting String using sed

I am trying to delete a SPECIFIC string on a file using sed.

test.txt contains

123
456
789
111
1
000136

For example i need to remove only the 1 text in the file. I tried to use the command

sed '/1/d' test.txt

the output will be

456
789

How can i only delete 1 using sed? Is there any way to do it?

Thank you!

Upvotes: 0

Views: 371

Answers (1)

setempler
setempler

Reputation: 1751

The problem with your example is, that the pattern used in sed matches all lines containing a 1 character.

Adjust the pattern instead: sed '/^1$/d' test.txt

This will match only lines containing a single character, namely 1.

This would not match lines containing extra whitespace!

Read more about regular expressions (and the special characters ^ and $) at: https://www.gnu.org/software/sed/manual/html_node/Regular-Expressions.html

Upvotes: 2

Related Questions