1

I want to delete a string "www.yahoo.com" only if the line starts with assign where host.name in [ and ends with ] and also if the string is followed by comma(,)  I need to delete that comma as well

assign where host.name in [ "www.google.com","www.yahoo.com","www.facebook.com" ] 

My output should look like

assign where host.name in [ "www.google.com", "www.facebook.com" ] 

I don't know how to achieve this.  Kindly give me some idea about this code.

6
  • 2
    Grep doesn't do this stuff. You need a stream editor (sed) or something no less powerfull eg. Perl. Commented Sep 15, 2019 at 22:30
  • Looks like you don't just want to delete the Yahoo string but you want to replace it with a space.. ? Commented Sep 15, 2019 at 22:43
  • Only you know the source input; how much of the string assign where host.name in [ is sufficient to identify the line where Yahoo is to be removed? Commented Sep 15, 2019 at 22:44
  • is this a school assignment? Commented Sep 15, 2019 at 22:47
  • @roaima yes exactly the same i want to delete a word and replace it with a space Commented Sep 15, 2019 at 23:16

1 Answer 1

4

Problems like this are typically handled in sed, awk or Perl.  The standard sed solution is

sed '/^assign where host.name in \[.*\]$/s/"www.yahoo.com",//' 

Your wording is odd. People normally don’t say

I have a pet ca followed by t.

they say

I have a pet cat.

Similarly, rather than

a string "www.yahoo.com" … if the string is followed by comma (,)

you should just say

a string "www.yahoo.com",

unless you mean something more complicated, like maybe

a string "www.yahoo.com" … if the string is followed by comma (,), optionally separated by a space

in which case you should say that.

The above command does what your question asks for: it deletes the string "www.yahoo.com",.  If you want to do what you showed, and what you said in a comment, add a space:

sed '/^assign where host.name in \[.*\]$/s/"www.yahoo.com",/ /' 
3
  • 1
    Perhaps what they meant was to delete the string optionally followed by a comma (i.e. to handle the case where "www.yahoo.com" is the last entry in the list)? Commented Sep 16, 2019 at 0:04
  • [[:blank:]]*,? Commented Sep 16, 2019 at 5:51
  • @cas: Well, sure, we can sit around all day guessing what pattern the OP really wants to match; for example, s/[[:space:]]*"www.yahoo.com"[[:space:]]*,\{0,1\}[[:space:]]*/ /'. But remember that ? and + aren’t defined in POSIX regular expressions. Commented Sep 16, 2019 at 8:36

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.