4

I am trying to use gawk in windows to parse a directory which contains CSV files for the blank rows in the second column in each file. I want to take the whole row where the the 2nd column is blank from ALL the source files and output to a csv. .. what I have so far is below.

I am sure this has to do with properly escaping the single quotes, I've tried using ^ and \ to no avail. Thanks for any tips.

awk -F, \'!length$2\' *.csv > output.csv 
uraimo
  • 19,081
  • 8
  • 48
  • 55
Cace
  • 51
  • 1
  • 4
  • where did you run this? `cmd` or `bash` or others? – Jason Hu Jul 07 '15 at 17:06
  • Why are you escaping those single quotes? You are missing `()` on the length call too. – Etan Reisner Jul 07 '15 at 17:10
  • - running in CMD on Windows . - Escaping the single quotes because otherwise I get " invalid char '''' in expression" - that command worked in bash on a Mac. I am trying to adopt it for windows. I will try the () – Cace Jul 07 '15 at 17:16
  • By modifying the command by adding () , so it looks like this : awk -F, (!length$2) *.csv >output.csv I am now getting an output, but its outputting ALL the data , not just the rows with a blank 2nd column .. am I missing something ? – Cace Jul 07 '15 at 17:20
  • no, it should be `length($2)`. in bash, the command shouldn't work. it should be `'!length($2)'` – Jason Hu Jul 07 '15 at 17:21

1 Answers1

1

Thanks to Etan Reisner and HuStmpHrrr I have it working now. If you folks want to post answers I can mark them so that you get reputation points. Your hints pointed me in the right direction. I now have all the rows with a blank 2nd row in one file. The final working version of the command in my case is :

awk -F, !length($2) *.csv > output.csv    //Works ( in windows)
  • I am not sure why my answer was edited, but the string above doen`t work when you enclose it in single quotes.

    awk -F, '!length($2)' *.csv > output.csv //Doesn`t Work (in windows)

Cace
  • 51
  • 1
  • 4
  • In windows, you should enclose your gawk program in double quotes. If your program contains a space (or any other CMD.com meta-character), the program will fail. It is a bit of a pain, as that means you need to escape the double quotes for string literals inside your program with a backslash. Or, install Cygwin of course. – joepd Jul 12 '15 at 19:10