开发者

Unix Find Replace Special Characters in Multiple Files

开发者 https://www.devze.com 2022-12-13 14:28 出处:网络
I\'ve got a set of files in a web root that all contain special characters that I\'d like to remove (Â,€,â,etc).

I've got a set of files in a web root that all contain special characters that I'd like to remove (Â,€,â,etc).

My command

find . -type f -name '*.*' -exec grep -il "Â" {} \;

finds & lists out the files just fine, but my command

find . -type f -name '*.*' -exec tr -d 'Â' '' \;

doesn't produce the results I'm look开发者_JAVA技巧ing for.

Any thoughts?


to replace all non-ascii characters in all files inside the current directory you could use:

find . -type f | xargs perl -pi.bak -e 's,[^[:ascii:]],,g'

afterwards you will have to find and remove all the '.bak' files:

find . -type f -a -name \*.bak | xargs rm


I would recommend looking into sed. It can be used to replace the contents of the file.

So you could use the command:

find . -type f -name '*.*' -exec sed -i "s/Â//" {} \;

I have tested this with a simple example and it seems to work. The -exec should handle files with whitespace in their name, but there may be other vulnerabilities I'm not aware of.


Use

tr -d 'Â' 

What does the ' ' stands for? On my system using your command produces this error:

tr: extra operand `'

Only one string may be given when deleting without squeezing repeats.

Try `tr --help' for more information.


sed 's/ø//' file.txt

That should do the trick for replacing a special char with an empty string.

find . -name "*.*" -exec sed 's/ø//' {} \


It would be helpful to know what "doesn't produce the results I'm looking for" means. However, in your command tr is not provided with the filenames to process. You could change it to this:

find . -type f -name '*.*' -exec tr -d 'Â' {} \;

Which is going to output everything to stdout. You probably want to modify the files instead. You can use Grundlefleck's answer, but one of the issues alluded to in that answer is if there are large numbers of files. You can do this:

find . -type f -name '*.*' -print0 | xargs -0 -I{} sed -i "s/Â//" \{\}

which should handle files with spaces in their names as well as large numbers of files.


with bash shell

for file in *.*
do
  case "$file" in 
   *[^[:ascii:]]* )
         mv "$file" "${file//[^[:ascii:]]/}"
   ;;
  esac
done


I would use something like this.

for file in `find . -type f`
do
    # Search for char end remove it. Save file as file.new
    sed -e 's/[ۉ]//g' $file > $file.new
    # mv file.new to file DON'T RUN IF YOU WILL NOT OVERITE ORIGINAL FILE
    mv $file.new $file
done

The above script will fail as levislevis85 has mentioned it with spaces in filenames. This would not be the case if you use the following code.

find . -type f | while read file
do 
    # Search for char end remove it. Save file as file.new
    sed -e 's/[ۉ]//g' "$file" > "$file".new
    # mv file.new to file DON'T RUN IF YOU WILL NOT OVERITE ORIGINAL FILE
    mv "$file".new "$file"
done
0

精彩评论

暂无评论...
验证码 换一张
取 消