1

I have search for a way to do this, I have found similar questions but nothing that matches this.

I have a cvs file with the following text:

nfoabc,infidel
infoghi,infojkl
,
,
,
inferno,infopqr
infoabc,infodef
,
,
,

The desired output is:

infoabc,infidel
infoghi,infojkl
inferno,infopqr
infoabc,infodef

I'm wondering if there is a way to filter the lines with a single , and no other characters… I think this is where I'm having difficulty.

Any assistance would be greatly appreciated. TIA.

anubhava
  • 761,203
  • 64
  • 569
  • 643
cducasse
  • 9
  • 5
  • 1
    Possible duplicate of [How to skip lines matching a string](http://stackoverflow.com/questions/6684857/how-to-skip-lines-matching-a-string) – Benjamin W. Feb 11 '16 at 18:21
  • Thanks everyone. What I was doing wrong was not writing it back to a file, ideally, I'd have it overwrite the existing file but I can deal with this. Thanks! – cducasse Feb 11 '16 at 18:42
  • @cducasse: `cmd file > tmp && mv tmp file`. Replace `cmd` with whatever command you want to execute. – Ed Morton Feb 11 '16 at 18:50

3 Answers3

3
$ grep -v '^,$' file
infoabc,infidel
infoghi,infojkl
inferno,infopqr
infoabc,infodef

or for this particular input file where there's either 1 or 0 commas:

$ grep '[^,]' file
infoabc,infidel
infoghi,infojkl
inferno,infopqr
infoabc,infodef
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
1

If you want to edit the file, you can always use ed:

ed -s file <<< $'g/^,$/d\nw'
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
0

awk to the rescue!

$ awk '!/^,$/' file

nfoabc,infidel
infoghi,infojkl
inferno,infopqr
infoabc,infodef
karakfa
  • 66,216
  • 7
  • 41
  • 56