Andrew
Andrew

Reputation: 865

Using sed to check make sure each line only has numbers and a comma?

I have a text file that looks like this:

0,16777215
16807368,16807368
621357328,621357328
621357403,621357403
1380962773,1380962773
1768589474,1768589474

Is there a way to use sed to make sure that each line ONLY has numbers and one comma? If a line is missing the comma, contains letters, is blank, has spaces, etc. - then I want to delete it.

Upvotes: 0

Views: 187

Answers (2)

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5298

With sed:

sed -nr '/^[0-9]+,[0-9]+$/p' File

To edit the file in-place:

sed -nri '/^[0-9]+,[0-9]+$/p' File

A portable solution:

sed -nE '/^[0-9]+,[0-9]+$/p' File

Upvotes: 2

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21492

sed -e '/^[0-9]\+,[0-9]\+$/ !d' file

The address is a regular expression. If the line does not match the regular expression, the d (delete) command is applied. (The exclamation mark (!) inverts the condition.)

Upvotes: 0

Related Questions