5

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.

Jxek
  • 502
  • 1
  • 4
  • 13

3 Answers3

4

You can use grep successfully:

grep -E '^[^.]+\.[^,]+,[^,]+,[^,]+$' file > temp
mv temp file
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • This is the solution I have used. I was focused too much on using sed to think of using grep. Simple and it works, thanks. – Jxek Aug 19 '14 at 13:04
  • grep is simpler but for inline edit it would be better if you prefer sed. – Avinash Raj Aug 19 '14 at 13:06
4
grep -E '^[^.]+\.[^.]+(,[^,]+){2}$'
Kent
  • 189,393
  • 32
  • 233
  • 301
3

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 
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274