Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

bash

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.

like image 999
Jxek Avatar asked Aug 19 '14 12:08

Jxek


2 Answers

grep -E '^[^.]+\.[^.]+(,[^,]+){2}$'
like image 21
Kent Avatar answered Sep 28 '22 08:09

Kent


You can use grep successfully:

grep -E '^[^.]+\.[^,]+,[^,]+,[^,]+$' file > temp
mv temp file
like image 124
hjpotter92 Avatar answered Sep 28 '22 07:09

hjpotter92