I need to build a script that adds comma separated values into a particular line in a file.

Say I have a file with contents:

DL1 : abc@kk.com,cde@kk.com,xyz@kk.com
DL2 : hjk@kk.com,nbv@kk.com,tuy@kk.com

now I have to remove xyz@kk.com from DL1


It's the same pattern like in your related post

sed '/^DL1 :/ s/,xyz@kk.com//' file

This substitute ,xyz@kk.com with nothing in lines that start with DL1 :.

  • 1
    ... on the assumption that it's not the first element in the list... – Jeff Schaller May 9 at 22:57
  • You're right. Changed it. – finswimmer May 10 at 8:47


perl -lpe 'if (/^DL1 :/) {s/,?xyz\@kk\.com//; s/: \K,//}' file


awk '/^DL1 :/ {sub(/,?xyz@kk\.com/,"");sub(": ,",": ")} 1' file


sed -E '/^DL1 :/ s/,?cde@kk.com//; /^DL1 :/ s/: ,/: /' file


These may not be the best tools to manipulate what is essentially a variation of a csv file. I suggest you add more context about where the data comes from and its intended use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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