Jxek
Jxek

Reputation: 513

Removing lines from a file that don't match a pattern using sed

I want to remove all the lines from a file that don't have the form:

something.something,something,something

For example if the file was the following:

A sentence, some words  
ABCD.CP3,GHD,HDID  
Hello. How are you?  
A.B,C,D  
dbibb.yes,whoami,words  

I would be left with:

ABCD.CP3,GHD,HDID  
A.B,C,D  
dbibb.yes,whoami,words

I have tried to branch to the end of the sed script if I match the pattern I don't want to delete but continue and delete the line if it doesn't match:

cp $file{,.tmp}
sed "/^.+\..+,.+,.+$/b; /.+/d" "$file.tmp" > $file
rm "$file.tmp"

but this doesn't seem to have any affect at all.

I suppose I could read the file line by line, check if matches the pattern, and output it to a file if it does, but I'd like to do it using sed or similar.

Upvotes: 5

Views: 530

Answers (3)

Avinash Raj
Avinash Raj

Reputation: 174696

Instead of deleting the lines which didn't satisfies the pattern, you could print the lines that matches this something.something,something,something pattern.

Through sed,

$ sed -n '/^[^.]*\.[^,]*,[^,]*,[^,.]*$/p' file
ABCD.CP3,GHD,HDID  
A.B,C,D  
dbibb.yes,whoami,words

Use inline edit option -i[suffix] to save the changes made.

sed -ni.bak '/^[^.]*\.[^,]*,[^,]*,[^,.]*$/p' file

Note: -i[suffix] make a backup if suffix is provided.

Through awk,

$ awk '/^[^.]*\.[^,]*,[^,]*,[^,.]*$/{print}' file
ABCD.CP3,GHD,HDID  
A.B,C,D  
dbibb.yes,whoami,words 

Upvotes: 3

Kent
Kent

Reputation: 195029

grep -E '^[^.]+\.[^.]+(,[^,]+){2}$'

Upvotes: 4

hjpotter92
hjpotter92

Reputation: 80629

You can use grep successfully:

grep -E '^[^.]+\.[^,]+,[^,]+,[^,]+$' file > temp
mv temp file

Upvotes: 4

Related Questions