2013年8月7日 星期三

remove windows new line & carrier return symbol


[bash, Perl] A script to remove new line & carrier return symbol derived from Windows



#!/bin/bash
# Shell script to find out all the files under a directory and
#its subdirectories. This takes into consideration only those files
#or directories which do not have spaces or newlines in their names

# how to use it?
#    bash remove_cr.sh your_directoryname

DIR=`pwd`/$1

function list_files()
{
    if !(test -d $1)
    then echo $1; return;
    fi

    cd $1
    echo; echo `pwd`:; #Display Directory name

    for i in * ## [TODO] this will list all of files in the directory?
    do
               if test -d $i  #if dictionary
                then
                    list_files $i #recursively list files
                    cd ..
                else
                    echo $i; #Display File name
                    perl -p -i -e 's/\r\n$/\n/g' $i   ## use Perl to replace end \r\n with \n
                fi

    done
}

## if no argument input, then start from current directory ($# : number of argument input )
if [ $# -eq 0 ]
then list_files .
exit 0
fi



for i in $*
do
    DIR=$1
    list_files $DIR
    shift 1 #To read next directory/file name
done


## $* means whole arguments , shift could help to shift argement to next one.
## http://linuxpoison.blogspot.tw/2012/07/bash-script-using-shift-statement-to.html




沒有留言:

張貼留言