Removing special characters

Dear Friends,
I want to remove text between two patters.
Problem is, it has random special characters like \ / | * ` ~ ! $ etc.
These random special characters has no fixed length. But these special characters are appearing between a fixed pattern

e.g.
DM&^%#|#!\/?CT

Expected output
DMCT
So in short, I want to remove everything including "Spaces" appearing between DM and CT .

Little challenging as length is not fixed of special characters.
Please guide me.
Thank you in anticipation
Anushree

Doesn't look that challenging, assuming everything might includes DM and CT too:

sed 's/DM.*CT/DMCT/'

eg:

$ echo "qwertyDM&^%#|#!\/?CTabc" | sed 's/DM.*CT/DMCT/'
qwertyDMCTabc
$ echo "qwertyDM&^%#|#!\/?CTthis will be removed tooCTabc" | sed 's/DM.*CT/DMCT/'
qwertyDMCTabc

Hi,

Try this,

#!/usr/bin/perl


$str='DM&^%#|#!\/?CT';

$str =~ s/(\w+)(.+?)(\w+)/$1$3/;

print $str,"\n";

Hi Jlliagre and Pravin,
Thank you for both the suggestions. I have tried both. However its not working may be because these special characters also contain *
so Its not removing instances like follows

e.g.
DM**&^*$#)(*CT

Please suggest.

Hi,

Its works fine for me with the new input.

#!/usr/bin/perl


#$str='DM&^%#|#!\/?CT';

$str='DM**&^*$#)(*CT';
$str =~ s/(\w+)(.+?)(\w+)/$1$3/;

print $str,"\n";

try this

using sed command

cat file | sed 's/\[[]!@#$%^&\*(){}",.><~`\]*//g' | sed "s/'*//g" > file.nospecialchar

using tr command

/usr/bin/tr -d '[:punct:]' < file > file.nospecialchar
[root@sistem1lnx /]# cat file
DM&^%#|#!**&^*$#)(*\/?\/|*`~!$&^%#|#!\/?CT

[root@sistem1lnx /]# sed 's/[[:punct:]][[:punct:]]*//g' file
DMCT
sed 's/[[:punct:]][[:punct:]]*//g' file

It works fine too when "*" are embedded:

$ echo "DM**&^*$#)(*CT" | sed 's/DM.*CT/DMCT/'                                   
DMCT

Please clarify.

Hey friends,
great it worked.
But only one problem is yet to be solved.
I have used following two suggestions given by you

/usr/bin/tr -d '[:punct:]' < file > file.nospecialchar

and

/usr/bin/tr -d '[:punct:]' <test>

Its very well working but its replacing special characters by space. I want to remove that space without disturbing other spaces present in file. But special attention is to be given that, it shouldnt even touch other spaces in entire file as it will disturb entire machine readable file. it just have to remove space got generated between DM and CT

e.g.

DM*&%#^()*%*$|\ / ?CT

expected output

DMCT

where as existing solution is giving o/p as
DM CT (space is not expected).

Thank you for taking pains.

@anushree.a: can you please clarify why the sed solution I suggested doesn't work for you ?

Dear Jlliagre,
I think it did not work because neighter pattern of special character is fixed nor number of special character is fixed. What is fixed is only characters DM and CT. everything appearing between them we have to remove it including spaces
in one case it can be DM^%%^$*^()*&\|/ *^CT
in other case it can be anything like DM^%#%$|?\?\%CT

I think your script can work only if provided special character pattern is fixed.

Please suggest.

Did you care trying my script ?
It certainly removes spaces and whatever characters appears between DM and CT, including spaces.

Have you tried the solution which I have posted ?

Dear Jlliagre,
I am extremely sorry. Your solution wasn't wrking because I didnt make necessary changes in input file while testing.
I regrett. I apologize.

Now I have tested it and results are exactly as expected.

Sorry to trouble you and others.
Thank you very very much for the help.

No problem, you are welcome :slight_smile: