开发者

How to remove ˆM chars?

I have a file generated from windows that I have to paste into a script under linux. My script works fine, except for the fact tha开发者_StackOverflow中文版t at the end of every line I got a ^M char.

How can I remove it with bash?

Currently my script is:

#/bin/bash
IFS=$'\n'
for CUSTOMER in `cat exp.csv`
do
    echo $CUSTOMER
done


Call dos2unix on exp.csv before further processing.


^M should be windows newline \r, therefore you just need to remove all \r You can achieve this using the tr tool, which you can call from your bash script, this is from the wikipedia article (i did not verify it):

tr -d '\r' < inputfile > outputfile

Therefore, from bash it should be something like

VAR=`echo -n $CUSTOMER | tr -d '\r'`


I do this a lot using tr. The CR character is 013 in octal:

tr -d '\013' < $FILE

So in your loop, that would just be

for CUSTOMER in `tr -d '\013' < exp.csv`


dos2unix command ont he file


there are several ways to remove bad char M from your file

  1. use command dos2unix file_name

  2. open file in vi editor, go to command mode and type :%s/ctrl+v ctrl+m//g this will remove all bad chars from you file. now use :wq for save and exit

  3. sed -e "s/^M//" filename(exist file name) > newfilename(new name)

    sed -e "s/^M//" filename(exist file name) > newfilename(new name)

NOTE: to find bad chars on unix server hit below command for all java files

find . -name "*.java*"  -type f | xargs grep -l ^M *

Similarly you can use "*.*" instead of "*.java" for all type of files

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜