dos2unix command

前提是你 提交于 2019-12-02 10:33:18

问题


I have this script

#!/bin/sh
for i in `ls -R`
do
  echo "Changing $i"
  fromdos $i 
done

I want to remove "^M" charcaters from many files which are in more subdirectories. I got this:

fromdos: Unable to access file

Is there somethig i'm missing?

Thanks in advance.


回答1:


I guess you don't need a for loop.

Here is a quick panorama of solutions for files with extension ".ext" (such commands shall be somehow restrictive)

note : ^M is obtained with CTRL-V" + "CTRL-M"

# PORTABLE SOLUTION 
find /home -type f -name "*.ext" -exec sed -i -e 's/^M$//' {} \;

# GNU-sed
find /home -type f -name "*.ext" -exec sed -i -e "s/\x0D$//g" {} \;

# SED with more recent nux
find /home -type f -name "*.ext" -exec sed -i -e "s/\r$//g" {} \;

# DOS2UNIX
find /home -type f -name "*.ext" -print0 | while read -r -d "$(printf "\000")" -r path; do dos2unix $path $path"_new"; done

# AWK
 find /home -type f -name "*.ext" -print0 | while read -r -d "$(printf "\000")" -r path; do awk '{ sub("\r$", ""); print }' $path > $path"_new"; done

# TR
 find /home -type f -name "*.ext" -print0 | while read -r -d "$(printf "\000")" -r path; do cat $path | tr -d '\r' > $path"_new"; done

# PERL
 find /home -type f -name "*.ext" -exec perl -pi -e 's/\r//g' {} \;



回答2:


ls -R lists everything, including directories. So you're telling fromdos to act on actual directories is some cases.

Try something like this:

find . -type f -exec fromdos {} \;


来源:https://stackoverflow.com/questions/5436761/dos2unix-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!