how to remove unnecessary commas using sed in the shell script?

Asked

Viewed 91 times

-2

I’m trying to create a script to automate a job I do. Everything was going well until I found this problem. When I use the sed, to remove a word from the string, the output shows 2 commas together, and this can cause faults in the edited parameter. Ex: Removing "BBB"

Domain.vmfull="AAAAA,BBBBB,CCCCC,ABCDE"

Domain.vmfull="AAAAA,,CCCCC,ABCDE"

Question 1 How can I remove one of these 2 commas? The output should look like this:

Domain.vmfull="AAAAA,CCCCC,ABCDE"

Question 2 Otherwise, when the word to be removed is the last or the first on the list, the unnecessary comma will be the first or the last. How can I avoid both possibilities? Ex: as below by removing AAAAA:

Domain.vmfull=",BBBBB,CCCCC,ABCDE"

the exit must be:

Domain.vmfull="BBBBB,CCCCC,ABCDE"

bash-3.2$ echo "$options_sched"
"-vmfulltype=vstor -vmbackuptype=fullvm -asnodename=NODE1 -MODE=IFIncremental -domain.vmfull="AAAAA,BBBBB,CCCCC,ABCDE""

bash-3.2$ options_final=$(echo $options_sched | sed "s#BBBBB##gI")

bash-3.2$ echo $options_final
"-vmfulltype=vstor -vmbackuptype=fullvm -asnodename=NODE1 -MODE=IFIncremental -domain.vmfull="AAAAA,,CCCCC,ABCDE"" 



#Question - 2
bash-3.2$ echo "$options_sched"
"-vmfulltype=vstor -vmbackuptype=fullvm -asnodename=NODE1 -MODE=IFIncremental -domain.vmfull="AAAAA,BBBBB,CCCCC,ABCDE""

bash-3.2$ options_final=$(echo $options_sched | sed "s#AAAAA##gI")

bash-3.2$ echo $options_final
"-vmfulltype=vstor -vmbackuptype=fullvm -asnodename=NODE1 -MODE=IFIncremental -domain.vmfull=",BBBBB,CCCCC,ABCDE"" 

1 answer

0

I believe that the most correct way to solve this problem is to write a script that interprets the text, understands and divides the parts that matter to you to handle it more easily. (For example find the parameter domain.vmfull, split the content passed to it by commas, remove the desired item and make a Join back. This can be done quietly in a bash or python script.)

Try to perform this type of more complex word processing using only sed may end up being a foot shot.

As your question asks using sed, an option not very elegant, but easy to understand would concatenate multiple sed's cascading:

For example:

  • Question 1: You can replace "," by "," (Two commas by one):
$  echo $A | sed 's/BBBBB//g' | sed 's/,,/,/g'
-vmfulltype=vstor -vmbackuptype=fullvm -asnodename=NODE1 -MODE=IFIncremental -domain.vmfull="AAAAA,CCCCC,ABCDE"

Of course, you need to make sure that outside of that range you don’t have somewhere else "two commas in a row" that really needed to be two commas in a row, as they will also be replaced. I could also think of a more complex regular expression (regex), but that would require a very strong analysis and assurance that the text follows a specific mold.

  • Question 2: You can replace vmfull=", for vmfull=" , and replace ," at the end of the sentence by ". Below, $(dollar sign) represents the end of the sentence:
$ echo $A | sed 's/ABCDE//g' | sed 's/,,/,/g' | sed 's/vmfull=",/vmfull="/g' | sed 's/,"$/"/g'
-vmfulltype=vstor -vmbackuptype=fullvm -asnodename=NODE1 -MODE=IFIncremental -domain.vmfull="AAAAA,BBBBB,CCCCC"

That gives the desired feedback. As you can see, I also joined the commands of your two questions.

Again, this type of substitution will always assume some things in the text that if they are not true the command will not work. He assumes for example that the argument vmfull is the last on the list, or that there is no case of "double comma". One can think of a more complex regex, but I do not believe it is the best approach in this case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.