简体   繁体   English

如何使用 Linux shell 脚本从文件中删除 ^[ 字符?

[英]How to Remove ^[ characters from a file using Linux shell scripting?

Using Linux shell scripting, how can I remove the ^[ characters from something like this:使用 Linux shell 脚本,我怎样才能从这样的东西中删除 ^[ 字符:

^[[0mCAM1> 
^[[0^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H^H ^H
 rcv-multicast: 0
      tx-bytes: 33649974
    tx-packets: 99133

You can use sed to remove chars from files like this:您可以使用sed从文件中删除字符,如下所示:

sed -i '' -e 's/^[//g' somefile

The -i '' causes it to change the file in-place (not make a copy). -i ''导致它就更改文件(而不是复制)。

You can make that with sed for example:您可以使用 sed 来实现,例如:

sed 's/^\[//g' oldfile > newfile;
mv newfile oldfile;

(it will remove only the trailing brackets, if you want to remove all of them, remove the ^ sign from the sed expression) (它只会删除尾括号,如果要删除所有括号,请从 sed 表达式中删除^符号)

You may remove these control characters by:您可以通过以下方式删除这些控制字符:

tr -d "[:cntrl:]" file.txt

however it'll remove also new line endings, so here is a trick, define this alias:但是它也会删除新的行尾,所以这是一个技巧,定义这个别名:

alias clean='tr "\r\n" "\275\276" | tr -d "[:cntrl:]" | tr "\275\276" "\r\n"'

then try like:然后尝试:

cat file.txt | clean > new_file.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Linux shell 脚本从文件中删除 ^H 和 ^M 字符 - Remove ^H and ^M characters from a file using Linux shell scripting 如何使用 linux shell 脚本删除文件中的 ^[ 和所有转义序列 - How to remove ^[, and all of the escape sequences in a file using linux shell scripting Linux Shell脚本:如何删除单词列表文件中的最终数字? - Linux shell scripting: How can I remove final numbers in a word list file? Linux Shell脚本:如何删除单词列表文件中的首字母? - Linux shell scripting: How can I remove initial numbers in a word list file? 从linux文件中删除^@字符 - Remove ^@ characters from linux file 使用bash shell脚本检查linux上目录中的最新文件更新 - Check latest file updates in directory on linux using bash shell scripting 从 linux shell 脚本中的文本文件打印下一个序列号 - print next sequence number from a text file in linux shell scripting Linux Shell:如何从输入文件中删除以模式开头的字符串 - Linux shell: how to remove a string starting with a pattern from an input file 如何检查正则表达式的字符/(斜杠)和^使用Shell脚本Linux - How to check Regex for character / (escape slash) and ^ Using Shell Scripting Linux 如何在linux中使用shell脚本登录到ftp服务器? - How to login to an ftp server using shell scripting in linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM