0

Everyone on the internet says you should use this:

if ($2 == "")

or this:

if (!length($2))

or this:

if (!$2)

All of these do not work for me. I got a csv, separated by ";" with 2 columns. In some cases the second column is empty (I can see that in gedit).

I want to distinguish the 2 sets in that file (2 sets = last column empty vs last column not empty). If I just print length($2), I see that those lines where there is nothing in column 2 actually have a length of 1.

So there seems to be something in there, but I cannot see what it is?

How can I make that thing visible, which makes length equal to 1? It seems to be preventing those 3 expressions from above from matching.

Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45
user3182532
  • 1,097
  • 5
  • 22
  • 37
  • 1
    Please show some sample data and a bigger piece of the code that fails on your system. One thing that comes to my mind is that you have Windows line endings `\r\n`. – James Brown Aug 01 '17 at 14:11

1 Answers1

5

Everyone on the internet...

Seriously? :)

My guess is that the second column contains a \n or a \r, which is invisible, but counts as having a length of 1. Have a look at this:

How can I trim white space from a variable in awk?

Peter Abolins
  • 1,520
  • 1
  • 11
  • 18
  • 1
    yes seriously, in every thread they teach those :P thank you, it was indeed windows newlines which caused the trouble (\r\n -> just kicked out the \r with sed). with linux newlines all those 3 expressions mentioned above work! – user3182532 Aug 01 '17 at 14:49
  • I've never said it. I guess I'm not on the internet. – Davy M Aug 02 '17 at 01:44
  • Well, at least [the girls](http://www.urbandictionary.com/define.php?term=Rule%2037) won't say that. – James Brown Aug 03 '17 at 15:50