Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

unix

awk

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?

like image 657
puneet agrawal Avatar asked Oct 25 '12 13:10

puneet agrawal


People also ask

How do you remove the last character in awk?

Using Awk command We can use the built-in functions length and substr of awk command to delete the last character in a text.

What is awk '{ print $3 }'?

txt. If you notice awk 'print $1' prints first word of each line. If you use $3, it will print 3rd word of each line.

What is awk '{ print $2 }'?

awk '{ print $2; }' prints the second field of each line. This field happens to be the process ID from the ps aux output.

What does awk '( print $9 do?

awk '{print $9}' the last ls -l column including any spaces in the file name.


1 Answers

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' ...
like image 196
Thor Avatar answered Oct 24 '22 01:10

Thor