Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How can I use sed or something else to delete a line only if the string I'm searching for is the only thing on the line?

Example output:

    device-address-binding: ?
    database-revision: 20
  -- Found 198 Objects 
  }, 
  { 
    object-identifier: (Analog Input, 1)
    object-type: ({  Analog Input  },)
    object-name: "CA-1 T EXT"
    descrip

I need to delete the lines containing " }," but only on lines that have nothing else. If I use sed to match and delete a line containing " }," on the output above the line "object-type" would also get deleted.

It would be nice to search for " },/n" but we all know sed doesnt work that way.

Thanks for your help.

share|improve this question
    
is there is a space before },? –  Avinash Raj May 6 at 15:46

4 Answers 4

up vote 1 down vote accepted

sed uses $ for the line end:

sed '/^},$/ d'

^ means the line beginning. You might also need to tolerate some whitespace around },.

sed '/^[[:space:]]*},[[:space:]]*$/ d'
share|improve this answer
    
So if I get this right ^[[:space:]]* means anything that begins ^ with a range [] of space char [:space:] and nothing else * before the rest of the string. Do I get an A+ ? –  TCZ8 May 6 at 15:47
    
I guess I could also simply use sed '/^ },$/d' since I know exactly how many spaces are before an after and it never changes. –  TCZ8 May 6 at 15:53
    
A litter smaller sed '/^ *}, *$/d' file –  Avinash Raj May 6 at 15:58
    
I just tried what I and Raj suggested... doesnt work, turns out that last char on the line isnt really a space, its a linefeed which got translated into a space under windows' copy/paste –  TCZ8 May 6 at 16:01
    
Ok so turns out even tough the one and only char after the comma in my string is a linefeed, which is supposedly stripped by SED's interpreter, I had to use [[:blank:]] to match the linefeed char before the end of line $ otherwise sed would'nt match it... weird. So im using this now sed '/^ },[[:space:]]$/d' thanks to everyone. –  TCZ8 May 6 at 16:09

No need for sed:

grep -vx }, file

Or to allow blanks:

grep -vx '[[:blank:]]*},[[:blank:]]*' file
share|improve this answer
sed '/^[[:blank:]]*},[[:blank:]]*$/d' file
share|improve this answer
    
Excellent, could you explain how it works? Im new to sed and I'd like to understand the search pattern. –  TCZ8 May 6 at 15:40

You can do it many ways, here is a perl solution:

$ perl -ple 's#^\s*},\s*##' file
    device-address-binding: ?
    database-revision: 20
  -- Found 198 Objects 

  { 
    object-identifier: (Analog Input, 1)
    object-type: ({  Analog Input  },)
    object-name: "CA-1 T EXT"
    descrip
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.