Steve Ngo
Steve Ngo

Reputation: 1

how do I use sed to delete lines with single digit instead of double

I have this line.

sed -i '/Total number 1/d' /tmp/test.txt

This will delete lines,

Total number 1

but it also deletes,

Total number 11

Total number 12

Total number 13

how do I set it to delete single digit only?

Upvotes: 0

Views: 50

Answers (2)

hek2mgl
hek2mgl

Reputation: 157947

The precise and generic solution would be:

sed '/\b[[:digit:]]\b/d'

\b stands for a word boundary.

Pass the -i option once you made sure that the above command works for you since it would effectively change your input files.

Upvotes: 1

twm
twm

Reputation: 1458

Add a dollar sign to the end sed -i '/Total number 1$/d' /tmp/test.txt

Also, if you want to delete any single digit, replace 1: sed -i '/Total number [0-9]$/d' /tmp/test.txt

Finally, if the number isn't necessarily at the end of the line, you could also have the pattern end when either the end of line or a non-digit is found: sed -i -E '/Total number [0-9]($|[^0-9])/d' /tmp/test.txt

Upvotes: 3

Related Questions