Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove "Cl." from file with bash without removing all Cl

Tags:

vim

sed

I am trying to delete specifically Cl. without deleting Cl inside a file:

Cl.CCC(N)(CC)C(=O)NC(CC(=O)O)c1ccccc1
CC(C)c1ncc(Cl)c(n1)C(=O)NCC(=O)O

And give me back this:

CCC(N)(CC)C(=O)NC(CC(=O)O)c1ccccc1
CC(C)c1ncc(Cl)c(n1)C(=O)NCC(=O)O

But everytime I tried with sed or vim it removes Cl as well. Someone knows any specific commando to delete it exactly Cl. and not Cl?

Thanks in advance.

Br I have tried this among other....

sed 's/[Cl.]//g' 
sed 's/\Cl.//g'
like image 404
BRUNO DI GERONIMO QUINTERO Avatar asked Oct 31 '25 00:10

BRUNO DI GERONIMO QUINTERO


2 Answers

You can use

sed 's/Cl\.//g' file > newfile

Here, Cl\. matches a Cl. substring. See an online demo:

#!/bin/bash
s='Cl.CCC(N)(CC)C(=O)NC(CC(=O)O)c1ccccc1
CC(C)c1ncc(Cl)c(n1)C(=O)NCC(=O)O'
sed 's/Cl\.//g' <<< "$s"

Output:

CCC(N)(CC)C(=O)NC(CC(=O)O)c1ccccc1
CC(C)c1ncc(Cl)c(n1)C(=O)NCC(=O)O
like image 53
Wiktor Stribiżew Avatar answered Nov 02 '25 05:11

Wiktor Stribiżew


Substitution in sed uses regular expressions to find the bit to be replaced. Period . is a special character in a regular expression that matches any single character except line terminators: \n, \r, \u2028 or \u2029. So you need to escape the period with a back slash \:

sed 's/C1\.//g' input > output

Without escaping the period, s/C1.//g will match and remove all instances of C followed by 1 followed by any character (except line terminators).

As an aside, square brackets [], also special characters, you used in your first attempt indicate a character class. So [C1.] will match the characters individually (any 'C', any '1', in this case any period).

Your second attempt escaped the C which is not a special character, so escaping has no effect.

like image 38
Schlameel Avatar answered Nov 02 '25 05:11

Schlameel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!