0

I have the below content in a file.txt:

"prom-oa-eui-slrcloud-plt4.test1.devops.oa.iqvia.com","auth-oa-eui-slrcloud-plt4.test1.devops.oa.iqvia.com","dex-oa-eui-slrcloud-plt4.test1.devops.oa.iqvia.com","dashboard-oa-eui-slrcloud-plt4.test1.devops.oa.iqvia.com","argocd-oa-eui-slrcloud-plt4.test1.devops.oa.iqvia.com","alert-oa-eui-slrcloud-plt4.test1.devops.oa.iqvia.com","tntcloudid.test1.devops.oa.iqvia.com","tntcloudid-df.test1.devops.oa.iqvia.com","tntcloudid-superset.test1.devops.oa.iqvia.com","tntcloudid-orchflow.test1.devops.oa.iqvia.com","tntcloudid-dmgr.test1.devops.oa.iqvia.com","tntcloudid-ps.test1.devops.oa.iqvia.com","vanity-tntcloud.test1.devops.oa.iqvia.com","vanity-tntcloud-df.test1.devops.oa.iqvia.com","vanity-tntcloud-superset.test1.devops.oa.iqvia.com","vanity-tntcloud-orchflow.test1.devops.oa.iqvia.com","vanity-tntcloud-dmgr.test1.devops.oa.iqvia.com","vanity-tntcloud-ps.test1.devops.oa.iqvia.com","tntcloud1id.test1.devops.oa.iqvia.com","tntcloud1id-df.test1.devops.oa.iqvia.com","tntcloud1id-superset.test1.devops.oa.iqvia.com","tntcloud1id-orchflow.test1.devops.oa.iqvia.com","tntcloud1id-dmgr.test1.devops.oa.iqvia.com","tntcloud1id-ps.test1.devops.oa.iqvia.com","vanity-tntcloud1.test1.devops.oa.iqvia.com","vanity-tntcloud1-df.test1.devops.oa.iqvia.com","vanity-tntcloud1-superset.test1.devops.oa.iqvia.com","vanity-tntcloud1-orchflow.test1.devops.oa.iqvia.com","vanity-tntcloud1-dmgr.test1.devops.oa.iqvia.com","vanity-tntcloud1-ps.test1.devops.oa.iqvia.com","tntcloud2id.test1.devops.oa.iqvia.com","tntcloud2id-df.test1.devops.oa.iqvia.com","tntcloud2id-superset.test1.devops.oa.iqvia.com","tntcloud2id-orchflow.test1.devops.oa.iqvia.com","tntcloud2id-dmgr.test1.devops.oa.iqvia.com","tntcloud2id-ps.test1.devops.oa.iqvia.com","vanity-tntcloud2.test1.devops.oa.iqvia.com","vanity-tntcloud2-df.test1.devops.oa.iqvia.com","vanity-tntcloud2-superset.test1.devops.oa.iqvia.com","vanity-tntcloud2-orchflow.test1.devops.oa.iqvia.com","vanity-tntcloud2-dmgr.test1.devops.oa.iqvia.com","vanity-tntcloud2-ps.test1.devops.oa.iqvia.com"

I want to delete all strings with the "tntcloudid" and "vanity-tntcloud" occurrences, only if I have those pattern in entry.

I tried the below code:

sed -e "s/tntcloudid//g; s/vanity-tntcloud//g" file.txt

But it's remove only the "tntcloudid" and "vanity-tntcloud" and not the remaining string from the line, like ".test1.devops.oa.iqvia.com","-df.test1.devops.oa.iqvia.com" etc...

As I have others strings like vanity-tntcloud1, vanity-tntcloud2 etc... those strings are impacted and I don't want remove those part of strings, but only my pattern ("tntcloudid" and "vanity-tntcloud" only)

I tried with the below code but the result it's the same:

sed -e "s/\<tntcloudid\>//g; s/\<vanity-tntcloud\>//g"

How can I remove only the lines matching with the "tntcloudid" and "vanity-tntcloud" pattern without impacting the others string for which the content is "vanity-tntcloud1" ... "vanity-tntcloud2" etc... ?

markalex
  • 8,623
  • 2
  • 7
  • 32
bessey
  • 51
  • 6

1 Answers1

3

As per your own explanation, you want to replace the entire surrounding token which contains this text, rather than only the text itself. You need to articulate a regular expression which matches everything that should be replaced.

With GNU sed:

TENANTID=tntcloudid
VANITY=vanity-tntcloud

sed -E 's/"[^"]*\<'"$TENANTID"'\>[^"]*"(,|$)//g; s/"[^"]*\<'"$VANITY"'\>[^"]*"(,|$)//g' file.txt
tripleee
  • 175,061
  • 34
  • 275
  • 318
M. Nejat Aydin
  • 9,597
  • 1
  • 7
  • 17
  • It's working fine, many thanks, but the result is to have several commas like : "prom-oa-eui-slrcloud-plt4.test1.devops.oa.iqvia.com","auth-oa-eui-slrcloud-plt4.test1.devops.oa.iqvia.com","dex-oa-eui-slrcloud-plt4.test1.devops.oa.iqvia.com","dashboard-oa-eui-slrcloud-plt4.test1.devops.oa.iqvia.com","argocd-oa-eui-slrcloud-plt4.test1.devops.oa.iqvia.com","alert-oa-eui-slrcloud-plt4.test1.devops.oa.iqvia.com",,,,,,,,,,,,,"tntcloud1id.test1.devops.oa.iqvia.com","tntcloud1id-df.test1.devops.oa.iqvia.com" – bessey May 19 '23 at 08:57
  • Thanks a lot, it's working very fine, but last question, how updating this command if my patterns (tntcloudid and vanity-tntcloud) are variables (e.g : $TENANTID and $VANITY), I'm asking because I'm afraid to have some issue with simple and double quotes, but many thanks for your help – bessey May 19 '23 at 09:07
  • I just tried to replace the pattern by variables and the return is : sed: -e expression #1, char 39: Invalid preceding regular expression Here's my command : sed -E 's/"[^"]*\<${TENANTID}\>[^"]*"(,|$)//g; s/"[^"]*\<${VANITY}\>[^"]*"(,|$)//g' file.txt – bessey May 19 '23 at 09:12
  • @bessey Read the updated answer. Note that if the contents of variables contain special characters (such as `/`, `*`, `[`, etc.) the `sed` command will fail. – M. Nejat Aydin May 19 '23 at 09:18
  • Thanks a lot, it's working as expected. You unblocked me to achieve my script, thanks. – bessey May 19 '23 at 09:22