Rohan Majumdar
Rohan Majumdar

Reputation: 63

Deleting lines with sed where the variable value is empty

I have a file which has data in the format
A=1234
B=3456
C=5689

Now sometimes there might not be variable values with the variables i.e.
A=1234
B=2566
C=
D=555

I want to delete the lines in which there is nothing after "=".
What sed command can i use to achieve it?

Upvotes: 1

Views: 189

Answers (4)

captcha
captcha

Reputation: 3756

Code for

awk -F= '$2!=""' file

Upvotes: 2

potong
potong

Reputation: 58578

This might work for you (GNU sed):

sed -i '/^[^=]*=\s*$/d' file

Upvotes: 1

anubhava
anubhava

Reputation: 786291

Following sed should work:

sed -n '/= *$/!p' file

EDIT: To save these changes back to the file, use inline flag -i:

sed -i.bak -n '/= *$/!p' file

To delete blank lines with 0 or more spaces:

sed -i.bak -rn '/= *$|^ *$/!p' file

Upvotes: 1

perreal
perreal

Reputation: 98118

Delete the line if there is nothing except possible whitespace after the =:

sed  '/=[ ]*$/d' input

Upvotes: 1

Related Questions