0

I am working on a property file containing list of Users: For Example

[email protected],[email protected]
[email protected],[email protected]
[email protected]
[email protected]
[email protected]

Now, In case I just want to remove [email protected] only from AdminList. How can we do it from Linux using sed or awk? I am new to linux please help

EDIT: I don't want to delete the whole value part. I just want to delete the specific string. For Example if my file looks like :

[email protected],[email protected]
[email protected],[email protected]
[email protected]
[email protected],[email protected],[email protected],[email protected]
[email protected]

In this case I just want to get rid of [email protected] from AdminList

1
  • This looks familiar, but I can't yet find a previous version of this question. Could the email address exist at any point in the list -- beginning, middle, or end? What should an empty list look like? Commented Jan 13, 2020 at 12:33

4 Answers 4

2

Case 1: Remove complete "value" part

Assuming your file only contains "simple" key=value statements, i.e. such that the "value" part contains no = sign, you can use sed as follows:

sed '/^AdminList/s/=.*$/=/' propertyfile.txt

Output for your example would look like:

[email protected],[email protected]
[email protected],[email protected]
[email protected]
AdminList=
[email protected]

The idea here is to substitute (the s in the command) the expression consisting of =, followed by any number of characters (the .* part) up to the end of line (the $ symbol), with a simple =, but only on lines starting with AdminList.

Case 2: Excise specific value from value list

If you want to remove only one specific value from a comma-separated list, I would recommend an awk-based approach:

awk '/^AdminList/ {sub(/[email protected],?/,""); print;next} {print}' propertyfile.txt

This would match the line starting with AdminList and replace the pattern [email protected], possibly with a trailing , with the empty string, print the modified line, and skip execution to the next line. For all other lines, it will simply print the entire line.

Given your second example input, this produces:

[email protected],[email protected]
[email protected],[email protected]
[email protected]
[email protected],[email protected],[email protected]
[email protected]

The syntax chosen should be fairly portable; I have tested it with GNU Awk and Mawk.

1
  • Awesome, that's exactly what I was looking for. You're right if we want to remove the last entry there would be a trailing ",". Thanks for the quick help! Appretiate it. Commented Jan 14, 2020 at 9:58
0

Save your data in file(for test) test.txt

[email protected],[email protected]
[email protected],[email protected]
[email protected]
[email protected]
[email protected]

And use sed or grep

cat test.txt | sed /AdminList/d
cat test.txt | grep -v AdminList

Edit:

cat test.txt | grep AdminList | awk -F"=" '{print $2}' | sed -n 1'p' | tr ',' '\n' | while read word; do if [ "$word" = "[email protected]" ]; then continue; else echo $word; fi; done
1
  • This would just delete the line containing AdminList entirely, but I think the OP wants to keep the "key" part, so that the desired output looks like AdminList= for that line. Commented Jan 13, 2020 at 12:02
0

To edit the file directly with sed, use

sed -i '/[email protected]/d' file
0
0

command

sed "/AdminList/s/abc1\@abc\.com,//g"  filename

input

[email protected],[email protected]
[email protected],[email protected]
[email protected]
[email protected],[email protected],[email protected],[email protected]
[email protected]

output

[email protected],[email protected]
[email protected],[email protected]
[email protected]
AdminList=,[email protected],[email protected],[email protected]
[email protected]
2
  • That looks very good, but you may want to provide for the trailing , by using /abc1\@abc\.com,\?/. Commented Jan 17, 2020 at 15:26
  • Corrected @AdminBee Commented Jan 17, 2020 at 18:51

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.