1

I have a file.csv that contains:

ID,FN,LN,NUM
576,Pra,Mar,3
688,India,Desa,3
645,Ami,Path,5
245,Me,Yo,4

When I try to print all column fields, it works fine

awk '{print $1,$2,$3,$4}' FS=, OFS=, file.csv

ID,FN,LN,NUM
576,Pra,Mar,3
688,India,Desa,3
645,Ami,Path,5
245,Me,Yo,4

But if I try to add an extra column to the end, it won't work

awk '{print $1,$2,$3,$4,"R"}' FS=, OFS=, file.csv

,R,FN,LN,NUM
,R576,Pra,Mar,3
,R688,India,Desa,3
,R645,Ami,Path,5
245,Me,Yo,4,R

I want the output to be

ID,FN,LN,NUM,R
576,Pra,Mar,3,R
688,India,Desa,3,R
645,Ami,Path,5,R
245,Me,Yo,4,R

What did I do wrong?

4

If your input file has carriage-return/line-feed endings it will do that.

Here is what that looks like in vi (you may need list-mode for clarity):

Wed Mar 30 18:53:01 EDT 2016
ID,FN,LN,NUM^M
576,Pra,Mar,3^M
688,India,Desa,3^M
645,Ami,Path,5^M
245,Me,Yo,4^M
Wed Mar 30 18:53:01 EDT 2016
ID,FN,LN,NUM^M,R
576,Pra,Mar,3^M,R
688,India,Desa,3^M,R
645,Ami,Path,5^M,R
245,Me,Yo,4^M,R

In the second example, the ^M characters precede the ",R", making that print at the beginning of the line.

If you view this in vim rather than vi, you will see something similar.

You can convert line-endings in different ways:

  • in vim, if you type controlG, you will notice a "[dos]" in the status line, and then by typing ":set" (enter) see "dos" in the output. You can convert by
    :set ff=unix
    :w!
  • on the command-line, you may have dos2unix (that works as well).

Further reading:

| improve this answer | |
  • This is what my input file look like with list-modem, it has $ instead of ^M 576,Pra,Mar,3$ – Lee Mar 30 '16 at 23:42
  • vi and vim do different things. – Thomas Dickey Mar 30 '16 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.