How do I remove multiple special characters from a file?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












The script below currently removes the ^M character (Ctrl+V+M). I feel it's a bit long winded but I also need to add ^I and any other characters I might see in the future.



Is there an easier way to add ^I (Ctrl+V+I)? This is the first script I wrote for myself about 6 months ago after attending a 2 day shell programming class. I'm not sure if I made it longer than it needs to be, so any general tips would also be appreciated.



#!/bin/bash 

echo "$# item(s) to review."
question='Do you want to remove the ^M characters?'

for file
do
if grep "^M" "$file" >> /dev/null 2> /dev/null
then
echo "$file contains special characters"
echo $question
read answer
if [[ "$answer" == [yY] ]]
then
cat "$file" | sed "s/^M//" > "$file.safe"
echo "Special characters have been removed and $file.safe has been created."
elif [[ "$answer" == [yY][eE][sSaA]* ]]
then
cat "$file" | sed "s/^M//" > "$file.safe"
echo "Special characters have been removed and $file.safe has been created."
else
echo "Special characters have NOT been removed."
fi
elif [[ -d $file ]]
then
echo "$file is a directory"
else
echo "No special characters in $file"
fi
done






share|improve this question






















  • The tr command may be of interest.
    – thrig
    Nov 28 '17 at 21:44














up vote
1
down vote

favorite












The script below currently removes the ^M character (Ctrl+V+M). I feel it's a bit long winded but I also need to add ^I and any other characters I might see in the future.



Is there an easier way to add ^I (Ctrl+V+I)? This is the first script I wrote for myself about 6 months ago after attending a 2 day shell programming class. I'm not sure if I made it longer than it needs to be, so any general tips would also be appreciated.



#!/bin/bash 

echo "$# item(s) to review."
question='Do you want to remove the ^M characters?'

for file
do
if grep "^M" "$file" >> /dev/null 2> /dev/null
then
echo "$file contains special characters"
echo $question
read answer
if [[ "$answer" == [yY] ]]
then
cat "$file" | sed "s/^M//" > "$file.safe"
echo "Special characters have been removed and $file.safe has been created."
elif [[ "$answer" == [yY][eE][sSaA]* ]]
then
cat "$file" | sed "s/^M//" > "$file.safe"
echo "Special characters have been removed and $file.safe has been created."
else
echo "Special characters have NOT been removed."
fi
elif [[ -d $file ]]
then
echo "$file is a directory"
else
echo "No special characters in $file"
fi
done






share|improve this question






















  • The tr command may be of interest.
    – thrig
    Nov 28 '17 at 21:44












up vote
1
down vote

favorite









up vote
1
down vote

favorite











The script below currently removes the ^M character (Ctrl+V+M). I feel it's a bit long winded but I also need to add ^I and any other characters I might see in the future.



Is there an easier way to add ^I (Ctrl+V+I)? This is the first script I wrote for myself about 6 months ago after attending a 2 day shell programming class. I'm not sure if I made it longer than it needs to be, so any general tips would also be appreciated.



#!/bin/bash 

echo "$# item(s) to review."
question='Do you want to remove the ^M characters?'

for file
do
if grep "^M" "$file" >> /dev/null 2> /dev/null
then
echo "$file contains special characters"
echo $question
read answer
if [[ "$answer" == [yY] ]]
then
cat "$file" | sed "s/^M//" > "$file.safe"
echo "Special characters have been removed and $file.safe has been created."
elif [[ "$answer" == [yY][eE][sSaA]* ]]
then
cat "$file" | sed "s/^M//" > "$file.safe"
echo "Special characters have been removed and $file.safe has been created."
else
echo "Special characters have NOT been removed."
fi
elif [[ -d $file ]]
then
echo "$file is a directory"
else
echo "No special characters in $file"
fi
done






share|improve this question














The script below currently removes the ^M character (Ctrl+V+M). I feel it's a bit long winded but I also need to add ^I and any other characters I might see in the future.



Is there an easier way to add ^I (Ctrl+V+I)? This is the first script I wrote for myself about 6 months ago after attending a 2 day shell programming class. I'm not sure if I made it longer than it needs to be, so any general tips would also be appreciated.



#!/bin/bash 

echo "$# item(s) to review."
question='Do you want to remove the ^M characters?'

for file
do
if grep "^M" "$file" >> /dev/null 2> /dev/null
then
echo "$file contains special characters"
echo $question
read answer
if [[ "$answer" == [yY] ]]
then
cat "$file" | sed "s/^M//" > "$file.safe"
echo "Special characters have been removed and $file.safe has been created."
elif [[ "$answer" == [yY][eE][sSaA]* ]]
then
cat "$file" | sed "s/^M//" > "$file.safe"
echo "Special characters have been removed and $file.safe has been created."
else
echo "Special characters have NOT been removed."
fi
elif [[ -d $file ]]
then
echo "$file is a directory"
else
echo "No special characters in $file"
fi
done








share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '17 at 22:04









Gilles

507k12010031531




507k12010031531










asked Nov 28 '17 at 21:30









Emile

989




989











  • The tr command may be of interest.
    – thrig
    Nov 28 '17 at 21:44
















  • The tr command may be of interest.
    – thrig
    Nov 28 '17 at 21:44















The tr command may be of interest.
– thrig
Nov 28 '17 at 21:44




The tr command may be of interest.
– thrig
Nov 28 '17 at 21:44










4 Answers
4






active

oldest

votes

















up vote
1
down vote













You can put a loop around your script. So:



 for c in "^I" "^M"; do
for file; do
if grep "$c" "$file"; then
...
etc.
...
fi
done
done





share|improve this answer



























    up vote
    1
    down vote













    I prefer this perl one liner. The 'cM' is the control-M character.
    The original file(s) will be backed up with the extension '.bak' This extension can be your choice.



    perl -i.bak -pe 's/cM//g;' file(s)


    Example using a class of characters to remove. In the brackets perl will find control-I and control-M and remove them. I've not tested this exactly though.



    perl -i.bak -pe 's/[cMcI]//g;' files(s)





    share|improve this answer





























      up vote
      0
      down vote













      This is certainly much, much longer than it needs to be. All you need is the tr utility, plus a loop and redirections to act on the files that are passed as arguments to the script.



      #!/bin/sh
      for file do
      tr -d 'rt' <"$file" >"$file.safe"
      done


      With the option -d, tr removes the specified characters. The characters to remove are passed together as the first non-option argument. You can use backslash escapes to represent special characters: n for a newline (^J), r for a carriage return (^M), t for a tab (^I), etc.



      I haven't reproduced the code for asking the user because it's pointless. Directories will cause an error with redirection anyway, and it's really the job of the caller not to request a nonsensical action such as treating a directory as a regular file, so I also skipped that part.



      If you want to replace the original file, write to a temporary file then move the result in place.



      #!/bin/sh
      for file do
      tmp="$(TMPDIR=$(dirname -- "$file") mktemp)"
      tr -d 'rt' <"$file" >"$tmp" && mv -f -- "$tmp" "$file"
      done


      The temporary file name is constructed using mktemp so that the script is robust. It will work as long as you have write permission to the directory containing the file, without risking overwriting an existing file. It's secure even if that directory is writable by other users who might try to inject other data (a potential problem in /tmp).



      The mv command is only invoked if the call to tr succeeded, so there's no risk of losing data if tr fails, e.g. because the disk becomes full midway through.



      If you want to avoid replacing the file by a new, identical file if it doesn't contain any special characters, there are two ways:




      • You can check for the special characters first. There are several ways to do it. One way is to remove everything except those special characters and count the number of resulting characters. As an optimization, pipe through head -c 1 so that you don't need to go through the whole file if a special character is found close to the top: that way the count is 0 if there's nothing to do and 1 otherwise.



        if [ "$(tr -dc 'rt' <"$file" | head -c 1 | wc -c)" -ne 0 ]; then
        tr -d 'rt' <"$file" >"$tmp" && mv -f -- "$tmp" "$file"
        fi



      • You can do the transformation, then check if it's identical to the original. This can be slower if the files are often already in the desired state. On the other hand, this technique generalizes to cases where it isn't easy to determine whether the file is in the desired state.



        tr -d 'rt' <"$file" >"$tmp" &&
        if cmp -s "$tmp" "$file"; then
        rm -- "$tmp"
        else
        mv -f -- "$tmp" "$file"
        fi






      share|improve this answer




















      • The 2 reasons for asking are 1. I wanted to see if I could do it, and 2. When I use * as $1 to check all files within the directory and certain files, like this script, needs to keep the special characters. Also, I'm the only one who uses the script, and I use it to clean up files before I insert them into autosys or if I have copied them from a Windows environment.
        – Emile
        Nov 28 '17 at 22:40


















      up vote
      0
      down vote













      Have you thought of using



       tr -d .....<characterlist>....


      For example, get rid of any non-printable characters and put into another file:



       cat filename | tr -cd '[:print:]' >/tmp/x.out


      Modify the characterlist to suit your application....see the tr man apage for more information.



      Also it is nice because regex ranges are allowed:



       echo '01020304' | tr -d '[01-03]' | od -c





      share|improve this answer






















        Your Answer







        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "106"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407597%2fhow-do-i-remove-multiple-special-characters-from-a-file%23new-answer', 'question_page');

        );

        Post as a guest






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote













        You can put a loop around your script. So:



         for c in "^I" "^M"; do
        for file; do
        if grep "$c" "$file"; then
        ...
        etc.
        ...
        fi
        done
        done





        share|improve this answer
























          up vote
          1
          down vote













          You can put a loop around your script. So:



           for c in "^I" "^M"; do
          for file; do
          if grep "$c" "$file"; then
          ...
          etc.
          ...
          fi
          done
          done





          share|improve this answer






















            up vote
            1
            down vote










            up vote
            1
            down vote









            You can put a loop around your script. So:



             for c in "^I" "^M"; do
            for file; do
            if grep "$c" "$file"; then
            ...
            etc.
            ...
            fi
            done
            done





            share|improve this answer












            You can put a loop around your script. So:



             for c in "^I" "^M"; do
            for file; do
            if grep "$c" "$file"; then
            ...
            etc.
            ...
            fi
            done
            done






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 28 '17 at 21:38









            TomáÅ¡ PospíÅ¡ek

            542110




            542110






















                up vote
                1
                down vote













                I prefer this perl one liner. The 'cM' is the control-M character.
                The original file(s) will be backed up with the extension '.bak' This extension can be your choice.



                perl -i.bak -pe 's/cM//g;' file(s)


                Example using a class of characters to remove. In the brackets perl will find control-I and control-M and remove them. I've not tested this exactly though.



                perl -i.bak -pe 's/[cMcI]//g;' files(s)





                share|improve this answer


























                  up vote
                  1
                  down vote













                  I prefer this perl one liner. The 'cM' is the control-M character.
                  The original file(s) will be backed up with the extension '.bak' This extension can be your choice.



                  perl -i.bak -pe 's/cM//g;' file(s)


                  Example using a class of characters to remove. In the brackets perl will find control-I and control-M and remove them. I've not tested this exactly though.



                  perl -i.bak -pe 's/[cMcI]//g;' files(s)





                  share|improve this answer
























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    I prefer this perl one liner. The 'cM' is the control-M character.
                    The original file(s) will be backed up with the extension '.bak' This extension can be your choice.



                    perl -i.bak -pe 's/cM//g;' file(s)


                    Example using a class of characters to remove. In the brackets perl will find control-I and control-M and remove them. I've not tested this exactly though.



                    perl -i.bak -pe 's/[cMcI]//g;' files(s)





                    share|improve this answer














                    I prefer this perl one liner. The 'cM' is the control-M character.
                    The original file(s) will be backed up with the extension '.bak' This extension can be your choice.



                    perl -i.bak -pe 's/cM//g;' file(s)


                    Example using a class of characters to remove. In the brackets perl will find control-I and control-M and remove them. I've not tested this exactly though.



                    perl -i.bak -pe 's/[cMcI]//g;' files(s)






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 28 '17 at 22:08

























                    answered Nov 28 '17 at 21:52









                    DannyK

                    1843




                    1843




















                        up vote
                        0
                        down vote













                        This is certainly much, much longer than it needs to be. All you need is the tr utility, plus a loop and redirections to act on the files that are passed as arguments to the script.



                        #!/bin/sh
                        for file do
                        tr -d 'rt' <"$file" >"$file.safe"
                        done


                        With the option -d, tr removes the specified characters. The characters to remove are passed together as the first non-option argument. You can use backslash escapes to represent special characters: n for a newline (^J), r for a carriage return (^M), t for a tab (^I), etc.



                        I haven't reproduced the code for asking the user because it's pointless. Directories will cause an error with redirection anyway, and it's really the job of the caller not to request a nonsensical action such as treating a directory as a regular file, so I also skipped that part.



                        If you want to replace the original file, write to a temporary file then move the result in place.



                        #!/bin/sh
                        for file do
                        tmp="$(TMPDIR=$(dirname -- "$file") mktemp)"
                        tr -d 'rt' <"$file" >"$tmp" && mv -f -- "$tmp" "$file"
                        done


                        The temporary file name is constructed using mktemp so that the script is robust. It will work as long as you have write permission to the directory containing the file, without risking overwriting an existing file. It's secure even if that directory is writable by other users who might try to inject other data (a potential problem in /tmp).



                        The mv command is only invoked if the call to tr succeeded, so there's no risk of losing data if tr fails, e.g. because the disk becomes full midway through.



                        If you want to avoid replacing the file by a new, identical file if it doesn't contain any special characters, there are two ways:




                        • You can check for the special characters first. There are several ways to do it. One way is to remove everything except those special characters and count the number of resulting characters. As an optimization, pipe through head -c 1 so that you don't need to go through the whole file if a special character is found close to the top: that way the count is 0 if there's nothing to do and 1 otherwise.



                          if [ "$(tr -dc 'rt' <"$file" | head -c 1 | wc -c)" -ne 0 ]; then
                          tr -d 'rt' <"$file" >"$tmp" && mv -f -- "$tmp" "$file"
                          fi



                        • You can do the transformation, then check if it's identical to the original. This can be slower if the files are often already in the desired state. On the other hand, this technique generalizes to cases where it isn't easy to determine whether the file is in the desired state.



                          tr -d 'rt' <"$file" >"$tmp" &&
                          if cmp -s "$tmp" "$file"; then
                          rm -- "$tmp"
                          else
                          mv -f -- "$tmp" "$file"
                          fi






                        share|improve this answer




















                        • The 2 reasons for asking are 1. I wanted to see if I could do it, and 2. When I use * as $1 to check all files within the directory and certain files, like this script, needs to keep the special characters. Also, I'm the only one who uses the script, and I use it to clean up files before I insert them into autosys or if I have copied them from a Windows environment.
                          – Emile
                          Nov 28 '17 at 22:40















                        up vote
                        0
                        down vote













                        This is certainly much, much longer than it needs to be. All you need is the tr utility, plus a loop and redirections to act on the files that are passed as arguments to the script.



                        #!/bin/sh
                        for file do
                        tr -d 'rt' <"$file" >"$file.safe"
                        done


                        With the option -d, tr removes the specified characters. The characters to remove are passed together as the first non-option argument. You can use backslash escapes to represent special characters: n for a newline (^J), r for a carriage return (^M), t for a tab (^I), etc.



                        I haven't reproduced the code for asking the user because it's pointless. Directories will cause an error with redirection anyway, and it's really the job of the caller not to request a nonsensical action such as treating a directory as a regular file, so I also skipped that part.



                        If you want to replace the original file, write to a temporary file then move the result in place.



                        #!/bin/sh
                        for file do
                        tmp="$(TMPDIR=$(dirname -- "$file") mktemp)"
                        tr -d 'rt' <"$file" >"$tmp" && mv -f -- "$tmp" "$file"
                        done


                        The temporary file name is constructed using mktemp so that the script is robust. It will work as long as you have write permission to the directory containing the file, without risking overwriting an existing file. It's secure even if that directory is writable by other users who might try to inject other data (a potential problem in /tmp).



                        The mv command is only invoked if the call to tr succeeded, so there's no risk of losing data if tr fails, e.g. because the disk becomes full midway through.



                        If you want to avoid replacing the file by a new, identical file if it doesn't contain any special characters, there are two ways:




                        • You can check for the special characters first. There are several ways to do it. One way is to remove everything except those special characters and count the number of resulting characters. As an optimization, pipe through head -c 1 so that you don't need to go through the whole file if a special character is found close to the top: that way the count is 0 if there's nothing to do and 1 otherwise.



                          if [ "$(tr -dc 'rt' <"$file" | head -c 1 | wc -c)" -ne 0 ]; then
                          tr -d 'rt' <"$file" >"$tmp" && mv -f -- "$tmp" "$file"
                          fi



                        • You can do the transformation, then check if it's identical to the original. This can be slower if the files are often already in the desired state. On the other hand, this technique generalizes to cases where it isn't easy to determine whether the file is in the desired state.



                          tr -d 'rt' <"$file" >"$tmp" &&
                          if cmp -s "$tmp" "$file"; then
                          rm -- "$tmp"
                          else
                          mv -f -- "$tmp" "$file"
                          fi






                        share|improve this answer




















                        • The 2 reasons for asking are 1. I wanted to see if I could do it, and 2. When I use * as $1 to check all files within the directory and certain files, like this script, needs to keep the special characters. Also, I'm the only one who uses the script, and I use it to clean up files before I insert them into autosys or if I have copied them from a Windows environment.
                          – Emile
                          Nov 28 '17 at 22:40













                        up vote
                        0
                        down vote










                        up vote
                        0
                        down vote









                        This is certainly much, much longer than it needs to be. All you need is the tr utility, plus a loop and redirections to act on the files that are passed as arguments to the script.



                        #!/bin/sh
                        for file do
                        tr -d 'rt' <"$file" >"$file.safe"
                        done


                        With the option -d, tr removes the specified characters. The characters to remove are passed together as the first non-option argument. You can use backslash escapes to represent special characters: n for a newline (^J), r for a carriage return (^M), t for a tab (^I), etc.



                        I haven't reproduced the code for asking the user because it's pointless. Directories will cause an error with redirection anyway, and it's really the job of the caller not to request a nonsensical action such as treating a directory as a regular file, so I also skipped that part.



                        If you want to replace the original file, write to a temporary file then move the result in place.



                        #!/bin/sh
                        for file do
                        tmp="$(TMPDIR=$(dirname -- "$file") mktemp)"
                        tr -d 'rt' <"$file" >"$tmp" && mv -f -- "$tmp" "$file"
                        done


                        The temporary file name is constructed using mktemp so that the script is robust. It will work as long as you have write permission to the directory containing the file, without risking overwriting an existing file. It's secure even if that directory is writable by other users who might try to inject other data (a potential problem in /tmp).



                        The mv command is only invoked if the call to tr succeeded, so there's no risk of losing data if tr fails, e.g. because the disk becomes full midway through.



                        If you want to avoid replacing the file by a new, identical file if it doesn't contain any special characters, there are two ways:




                        • You can check for the special characters first. There are several ways to do it. One way is to remove everything except those special characters and count the number of resulting characters. As an optimization, pipe through head -c 1 so that you don't need to go through the whole file if a special character is found close to the top: that way the count is 0 if there's nothing to do and 1 otherwise.



                          if [ "$(tr -dc 'rt' <"$file" | head -c 1 | wc -c)" -ne 0 ]; then
                          tr -d 'rt' <"$file" >"$tmp" && mv -f -- "$tmp" "$file"
                          fi



                        • You can do the transformation, then check if it's identical to the original. This can be slower if the files are often already in the desired state. On the other hand, this technique generalizes to cases where it isn't easy to determine whether the file is in the desired state.



                          tr -d 'rt' <"$file" >"$tmp" &&
                          if cmp -s "$tmp" "$file"; then
                          rm -- "$tmp"
                          else
                          mv -f -- "$tmp" "$file"
                          fi






                        share|improve this answer












                        This is certainly much, much longer than it needs to be. All you need is the tr utility, plus a loop and redirections to act on the files that are passed as arguments to the script.



                        #!/bin/sh
                        for file do
                        tr -d 'rt' <"$file" >"$file.safe"
                        done


                        With the option -d, tr removes the specified characters. The characters to remove are passed together as the first non-option argument. You can use backslash escapes to represent special characters: n for a newline (^J), r for a carriage return (^M), t for a tab (^I), etc.



                        I haven't reproduced the code for asking the user because it's pointless. Directories will cause an error with redirection anyway, and it's really the job of the caller not to request a nonsensical action such as treating a directory as a regular file, so I also skipped that part.



                        If you want to replace the original file, write to a temporary file then move the result in place.



                        #!/bin/sh
                        for file do
                        tmp="$(TMPDIR=$(dirname -- "$file") mktemp)"
                        tr -d 'rt' <"$file" >"$tmp" && mv -f -- "$tmp" "$file"
                        done


                        The temporary file name is constructed using mktemp so that the script is robust. It will work as long as you have write permission to the directory containing the file, without risking overwriting an existing file. It's secure even if that directory is writable by other users who might try to inject other data (a potential problem in /tmp).



                        The mv command is only invoked if the call to tr succeeded, so there's no risk of losing data if tr fails, e.g. because the disk becomes full midway through.



                        If you want to avoid replacing the file by a new, identical file if it doesn't contain any special characters, there are two ways:




                        • You can check for the special characters first. There are several ways to do it. One way is to remove everything except those special characters and count the number of resulting characters. As an optimization, pipe through head -c 1 so that you don't need to go through the whole file if a special character is found close to the top: that way the count is 0 if there's nothing to do and 1 otherwise.



                          if [ "$(tr -dc 'rt' <"$file" | head -c 1 | wc -c)" -ne 0 ]; then
                          tr -d 'rt' <"$file" >"$tmp" && mv -f -- "$tmp" "$file"
                          fi



                        • You can do the transformation, then check if it's identical to the original. This can be slower if the files are often already in the desired state. On the other hand, this technique generalizes to cases where it isn't easy to determine whether the file is in the desired state.



                          tr -d 'rt' <"$file" >"$tmp" &&
                          if cmp -s "$tmp" "$file"; then
                          rm -- "$tmp"
                          else
                          mv -f -- "$tmp" "$file"
                          fi







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 28 '17 at 22:04









                        Gilles

                        507k12010031531




                        507k12010031531











                        • The 2 reasons for asking are 1. I wanted to see if I could do it, and 2. When I use * as $1 to check all files within the directory and certain files, like this script, needs to keep the special characters. Also, I'm the only one who uses the script, and I use it to clean up files before I insert them into autosys or if I have copied them from a Windows environment.
                          – Emile
                          Nov 28 '17 at 22:40

















                        • The 2 reasons for asking are 1. I wanted to see if I could do it, and 2. When I use * as $1 to check all files within the directory and certain files, like this script, needs to keep the special characters. Also, I'm the only one who uses the script, and I use it to clean up files before I insert them into autosys or if I have copied them from a Windows environment.
                          – Emile
                          Nov 28 '17 at 22:40
















                        The 2 reasons for asking are 1. I wanted to see if I could do it, and 2. When I use * as $1 to check all files within the directory and certain files, like this script, needs to keep the special characters. Also, I'm the only one who uses the script, and I use it to clean up files before I insert them into autosys or if I have copied them from a Windows environment.
                        – Emile
                        Nov 28 '17 at 22:40





                        The 2 reasons for asking are 1. I wanted to see if I could do it, and 2. When I use * as $1 to check all files within the directory and certain files, like this script, needs to keep the special characters. Also, I'm the only one who uses the script, and I use it to clean up files before I insert them into autosys or if I have copied them from a Windows environment.
                        – Emile
                        Nov 28 '17 at 22:40











                        up vote
                        0
                        down vote













                        Have you thought of using



                         tr -d .....<characterlist>....


                        For example, get rid of any non-printable characters and put into another file:



                         cat filename | tr -cd '[:print:]' >/tmp/x.out


                        Modify the characterlist to suit your application....see the tr man apage for more information.



                        Also it is nice because regex ranges are allowed:



                         echo '01020304' | tr -d '[01-03]' | od -c





                        share|improve this answer


























                          up vote
                          0
                          down vote













                          Have you thought of using



                           tr -d .....<characterlist>....


                          For example, get rid of any non-printable characters and put into another file:



                           cat filename | tr -cd '[:print:]' >/tmp/x.out


                          Modify the characterlist to suit your application....see the tr man apage for more information.



                          Also it is nice because regex ranges are allowed:



                           echo '01020304' | tr -d '[01-03]' | od -c





                          share|improve this answer
























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Have you thought of using



                             tr -d .....<characterlist>....


                            For example, get rid of any non-printable characters and put into another file:



                             cat filename | tr -cd '[:print:]' >/tmp/x.out


                            Modify the characterlist to suit your application....see the tr man apage for more information.



                            Also it is nice because regex ranges are allowed:



                             echo '01020304' | tr -d '[01-03]' | od -c





                            share|improve this answer














                            Have you thought of using



                             tr -d .....<characterlist>....


                            For example, get rid of any non-printable characters and put into another file:



                             cat filename | tr -cd '[:print:]' >/tmp/x.out


                            Modify the characterlist to suit your application....see the tr man apage for more information.



                            Also it is nice because regex ranges are allowed:



                             echo '01020304' | tr -d '[01-03]' | od -c






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 29 '17 at 0:33

























                            answered Nov 29 '17 at 0:28









                            mdpc

                            4,78521835




                            4,78521835



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407597%2fhow-do-i-remove-multiple-special-characters-from-a-file%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Bahrain

                                Postfix configuration issue with fips on centos 7; mailgun relay