1

A csv file has two columns. The code below outputs the entire file (all rows and the two columns separated by a comma)

BEGIN {FS=","}
{print $0}

However, the code below, outputs only one value viz. the Column 1 of the first row:

BEGIN {FS=","}
{print $1}

Above code is a .awk file and run in Windows using the command gawk -f test.awk xyz.csv > output.csv

What am I doing wrong?

Edited: Output after changing the {Print $1} to "{print "<" NR "><" $1 "><" $0 ">"}"``

<1><naskar><naskar,sahoo ,1
sahoo ,18290,
ree ,6379,
mukherjee ,4609,
user1955215
  • 733
  • 2
  • 11
  • 31
  • 1
    This can't happen unless you have stray characters in your `xyz.csv`. Post the output of `file xyz.csv` – Inian Mar 06 '20 at 18:37
  • The output when $1 is used is "naskar" – user1955215 Mar 06 '20 at 18:46
  • The output when $0 is used is "naskar, sahoo" and all the rest of the rows in the file below that. – user1955215 Mar 06 '20 at 18:47
  • I think there is something wrong ("stray characters") with the file as the Gawk commands are working as expected in another file. How do I identity and fix the problem that is causing this odd behavior? – user1955215 Mar 06 '20 at 18:53
  • @EdMorton, Thanks. I think your inference is correct. The file does not seem to be a DOS file. Is there any way of visually or otherwise determining whether the file is DOS or not? – user1955215 Mar 06 '20 at 19:11
  • https://stackoverflow.com/questions/32255747/on-windows-how-would-i-detect-the-line-ending-of-a-file , gives a link to unix tool `file`, or how to check it in notepad++ – Luuk Mar 06 '20 at 19:17

2 Answers2

1

Try calling gawk as gawk -v RS='\n' ... since the most likely problem is that you have UNIX line endings (\n) in your input file but your call to gawk is expecting DOS line endings (\r\n) and so thinks the file is a single line. If that's not it then change {print $1} to {print "<" NR "><" $1 "><" $0 ">"} and create a file with 5 lines run the script on it then edit your question to show the input file and the output you get.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • Is there any way to visually or otherwise determine whether the file has DOS line endings (for use in future)? – user1955215 Mar 06 '20 at 19:23
  • I don't know Windows tools but on UNIX you can run the `file` command on it or run `cat -Ev` on it. On Windows you can install `cygwin` or similar to get a UNIX environment to run UNIX tools from. I updated my comment to be an answer since that is your problem. – Ed Morton Mar 06 '20 at 19:24
0

Under windows you can create a batchfile like this:

@echo off
powershell -Command "write \"$(\"CRLF:\") - $((Get-Content '%1' -Raw) -match '\r\n$')\""
powershell -Command "write \"$(\"CR:\") - $((Get-Content '%1' -Raw) -match '\r$')\""
powershell -Command "write \"$(\"LF:\") - $((Get-Content '%1' -Raw) -match '\n$')\""

It will output something like this for a file with LF as line separator:

D:\TEMP>file.bat textfile.txt
CRLF: - False
CR: - False
LF: - True
D:\TEMP>
Luuk
  • 12,245
  • 5
  • 22
  • 33