tags:

views:

126

answers:

4

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 that 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
+4  A: 

^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'`
zerm
+5  A: 

Call dos2unix on exp.csv before further processing.

cjrh
dos2unix should be the one you want if you're operating on a Windows file in Linux.
JohnK813
`cat` is unnecessary.
Dennis Williamson
thx DW and JohnK813
cjrh
+1  A: 

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`
Andy Mortimer
A: 

dos2unix command ont he file

Derek