How to remove all special characters in Linux text

后端 未结 4 1706
囚心锁ツ
囚心锁ツ 2020-12-14 20:51

How to remove the special characters shown as blue color in the picture 1 like: ^M, ^A, ^@, ^[. In my understanding, ^M is a windows newline character, I can use sed

相关标签:
4条回答
  • 2020-12-14 20:52

    Try running below command on linux command prompt

    Option - 1: (If dos2unix command is installed on Linux machine)

    dos2unix sample_file.txt
    

    Option - 2:

    cat sample_file.txt | tr -d '\015' > new_sample_file.txt
    
    0 讨论(0)
  • 2020-12-14 21:01

    To ensure that the command works with limited scope in Sed, force use of the "C" (POSIX) character classifications to avoid unpredictable behavior with non-ASCII characters:

    LC_ALL=C sed 's/[^[:blank:][:print:]]//g' file.txt
    
    0 讨论(0)
  • 2020-12-14 21:08

    Try this inside vi or vim:

    [in ESC mode] type: :%s/^M//g

    or:

    sed -e "s/^M//" filename > newfilename
    

    Important: To enter ^M, type CTRL-V, then CTRL-M

    0 讨论(0)
  • Remove everything except the printable characters (character class [:print:]), with sed:

    sed $'s/[^[:print:]\t]//g' file.txt
    

    [:print:] includes:

    • [:alnum:] (alpha-numerics)
    • [:punct:] (punctuations)
    • space

    The ANSI C quoting ($'') is used for interpreting \t as literal tab inside $'' (in bash and alike).

    0 讨论(0)
提交回复
热议问题