saurav
saurav

Reputation: 219

How to delete a line using sed?

I have a input file with following pattern-

    8799,,2015-06-29 04:00:00+0000,
    FIT-I6-INL-PCG,['3.MYFIT-LTR-DYN'],2015-03-11 04:00:00+0000,

Here I copied three different field separated by , (comma) I wanted to delete every line which has empty field. in above case 1st line has an empty field SO the o/p should be

   FIT-I6-INL-PCG,['3.MYFIT-LTR-DYN'],2015-03-11 04:00:00+0000,  

Upvotes: 2

Views: 47

Answers (2)

Jahid
Jahid

Reputation: 22428

With grep:

grep -v '^,\|,,' file

With sed:

sed '/^,\|,,/d' file

If you want to change the file in-place, then:

sed -i.bak '/^,\|,,/d' file
# Or with grep:
# echo "$(grep -v '^,\|,,' file)" >file

Upvotes: 2

anubhava
anubhava

Reputation: 784938

You can use this sed:

sed -i.bak -E '/^,|,,/d' file

cat file
FIT-I6-INL-PCG,['3.MYFIT-LTR-DYN'],2015-03-11 04:00:00+0000,

/^,|,,/d will delete a line if comma is 1st char or if there are 2 commas together.

Upvotes: 3

Related Questions