简体   繁体   中英

Replacing multiple special characters from a file with a perl one-liner

I'm trying to replace some special characters (Ascii characters 1,5,7,23,26,30) from a file. I want to do it with a one-liner so I tried using the following.

perl -pi -e 's/([\x1 \x5 \x7 \x17 \x1a \x1e])//eg' dummy.txt

It replaces the special characters fine but I see there are some spaces also being removed from the file. For Ex: 1257 ST 02 BW becomes 1257ST02BW . This is not happening if I write replace for individual character separately like below:

perl -pi -e 's/([\x1])//eg' dummy.txt
perl -pi -e 's/([\x3])//eg' dummy.txt
.....
.....
perl -pi -e 's/([\x1e])//eg' dummy.txt

Can anybody please help me with this .

Try,

perl -pi -e 's/[\x1\x5\x7\x17\x1a\x1e]//g' dummy.txt

or

perl -pi -e 'tr/\x1\x5\x7\x17\x1a\x1e//d' dummy.txt

You've included spaces in char class definition, so they were removed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM