脚本从ASCII转换为Unix风格的LF

时间:2014-11-18 01:53:10

标签: shell unix

我正在尝试编写一个脚本,将给定的文件从ASCII CR / LF转换为Unix风格的LF。我坚持if if声明如何工作。我在sudo代码中有它,但不知道从那里去哪里。有人能指出我正确的方向。

!/bin/sh
file=$1
for file in "$@"
do case `file "$file"` in
   *"ASCII text, with CRLF line terminators" )
echo ""$file": Windows ASCII"
;;
* )
echo ""$file": Something else"
;;
esac
if file = somethingElse
do
echo converting $filename
convert to unix(I was thinking of using the tr command)
done

1 个答案:

答案 0 :(得分:0)

试试这个:

$ cat ctrl-m.txt -vte
ONE ^M$
two ^M$
three$
four$

#!/bin/sh
for file in "$@"
do
        if file $file | grep "with CRLF"; then
                echo "$file has CR/LF. Fixing.."
                sed -i 's/^M//g' $file
        fi  
done

针对ctrl-m.txt运行脚本后:

$ ./run.sh ctrl-m.txt 
ctrl-m.txt: ASCII text, with CRLF, LF line terminators
ctrl-m.txt has CR/LF. Fixing..


$ cat -vet ctrl-m.txt 
ONE $
two $
three$
four$

要在vi / vim的脚本中输入^M,您需要输入CTRL-V,然后输入CTRL-M。其他编辑器将有类似的方式输入控制字符。