Grep and neglect a specific string

Hi,

I have a file with "n" number of lines. I need to get rid of a specific line having a specific string from the file. I tried some possibilities but not successful.

For ex: in a file named "test"
hope should be removed along with the line.

PASS=test1/worked:fail/withthe/test/:go-to-school/grab/some:get/the/hope/to/work:success
LIB_WORK=/lets/hope/for/the/best \

output should be

PASS=test1/worked:fail/withthe/test/:go-to-school/grab/some:success

What did you try, and what wrong output did you get?

Unclear: you write you want to remove line(s) but in your sample, only parts of the line are removed. Pls. clarify.

@RudiC My requirement is both as you could see from the first line "hope" lies between :*hope*: the other line doesn't have any : so, it should remove the entire content.

@MadeInGermany
I have used the regex for replacing words with others, not in an expected way. so, I am not aware of the pattern to be used.

sed -i -re "s,hope,,g" test

Like so?

sed 's/\(:*\)[^:]*hope[^:]*:*/\1/g; /^$/d' file
PASS=test1/worked:fail/withthe/test/:go-to-school/grab/some:success

Hi RudiC
This is works really great when the content is in two seperate lines as

PASS=test1/worked:fail/withthe/test/:go-to-school/grab/some:get/the/hope/to/work:success

LIB_WORK=/lets/hope/for/the/best \
:~/Desktop/test1$ sed 's/\(:*\)[^:]*hope[^:]*:*/\1/g; /^$/d' te
PASS=test1/worked:fail/withthe/test/:go-to-school/grab/some:success

but some output come within a single line, in that case, it loses some data,
Could you give a modification according to that

PASS=test1/worked:fail/withthe/test/:go-to-school/grab/some:get/the/hope/to/work:success LIB_WORK=/lets/hope/for/the/best \
:~/Desktop/test1$ sed 's/\(:*\)[^:]*hope[^:]*:*/\1/g; /^$/d' te
PASS=test1/worked:fail/withthe/test/:go-to-school/grab/some:

if some explanation provided on the logic, it will much better to me to understand make use of it in, instead of wasting your valuable time.

---------- Post updated at 01:33 PM ---------- Previous update was at 01:20 PM ----------

Hi

If the regex is given in two different conditions too, its welcome.
Like one condition check for

:

as delimiter and remove the content with along with on delimiter

hope

2nd can check for the

hope

and if no delimiter was there it can delete the entire content

LIB_WORK=/lets/hope/for/the/best

Adding this to make clear in need

It is usually best and most efficient for all parties to carefully phrase the request in the first place, cogitating all the possible ramifications e.g. difference in structures and positions in files. Better than letting drip in information droplet by droplet after a solution was found for the first but non-fitting sample.

The easiest way would be to force the long lines into several independent ones:

tr ' ' '\n' < file | sed ...

The logics lie in the regex: find a string of zero or more colons (parenthesized for later "back reference") then any number of non-colons, the "hope" string, non-colons again, and finally zero or more colons. Replace by the first colon if exists else the empty string (i.e. remove)

Thanks for the explanation,
But it needs some changes as, I could send all the lines in the file, Some lines occur with the colon in between.

PASS=test1/worked:fail/withthe/test/:go-to-school/grab/some:get/the/hope/to/work:success LIB_WORK=/lets/hope/for/the/best TEST=come/home/with/hope:/if/you/reach/call/me:/dont/worry
RESULT=/I/hope/so/he/will/get/success PUBLISH=This/is/really:a/good/score:to/achieve

For this case, the proposed condition is failing,
Sorry for not representing this earlier, because, I didn't predict that my output will yield this patterns

The following deletes a :something/hope/something that follows a PASS= at the beginning of the line.

sed -n 's#\(^PASS=[^ ]*\):[^: ]*/hope/[^: ]*#\1#p' file

The -n option suppresses the default print, the p modifier prints if a substitution was made.
The part that is captured between \( \) is restored by the \1 .