awk: printing newline with last column

I was trying to simplify this from what I'm actually doing, but I started getting even more confused so I gave up. Here is the content of my input file:

Academic year,Term,Course name,Period,Last name,Nickname
2012-2013,First Semester,English 12,7th Period,Davis,Lucille

When I do this:

awk 'BEGIN { FS = "," } NR>1{print $6,$5}' CLASSES.csv 

I get this output:

 Davise

Note the whitespace before "Davise."
I expect to see this

Lucille Davis

assume it has something to do with the newline that is part of the last column, because if I change the data to:

Academic year,Term,Course name,Period,Last name,Nickname
2012-2013,First Semester,English 12,7th Period,Davis,Lucy,0

It works as expected. What am I doing wrong?

I can only assume there's something weird about your input data which doesn't post because it works perfectly here.

Perhaps your data is full of carriage returns? That can do weird things when printed to a console, because the carriage return would return the cursor to the beginning of the line.

That would fit actually. It prints 'lucille\r', returning the cursor to the beginning of the line, then prints a space, then prints 'Davis\n', which with the space is one character short of overwriting 'lucille'.

So the lesson here is, stop editing your input files in Microsoft Notepad :slight_smile:

It's probably not a newline but a carriage return. Try running

dos2unix CLASSES.csv

to fix your csv file.

Ah, that makes sense. This file was output in excel format by a Windows app called Blackbaud and then exported to CSV from excel. I'll try it. Thanks!