awk print is adding a control-m character at end of line

awk, unix

Solution

The record separator is automatically set to the line-ending of the current system, LF (`\n`) on the Unix-based systems, CR-LF (`\r\n`) on MS systems and CR (`\r`) on Mac OS prior to Mac OS X. So to work on a file recorded on an MS system set the record separator appropriately, in your case:

awk -v RS='\r\n' ...

Problem

I am using awk in a bash script and do something like: ``` awk -F, -v result_file=$2'{ print $2 $1 > result_file }' $data_file ``` In the output file, I am getting a control-M '^M' character at end of each line. What is wrong?

Original source