Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove ˆM chars?

Tags:

bash

windows

cat

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
like image 508
lbedogni Avatar asked Apr 11 '26 20:04

lbedogni


2 Answers

Call dos2unix on exp.csv before further processing.

like image 119
Caleb Hattingh Avatar answered Apr 13 '26 15:04

Caleb Hattingh


^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'`
like image 32
zerm Avatar answered Apr 13 '26 16:04

zerm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!