Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Finding line numbers of blank lines

24 views
Skip to first unread message

steve

unread,
Apr 17, 2013, 5:48:13 PM4/17/13
to
Sometimes I have files which are double-spaced and I use

awk 'length > 2' file

to strip away the blank lines.

Now I have some blank lines within a file and I would like to know the
line numbers on which they occur. I thought that I could do something
like:

awk '{length < 5} print NR' file

However, that gives an error.

Any suggestions would be welcome.

Thanks, Steve

steve

unread,
Apr 17, 2013, 5:57:42 PM4/17/13
to
Guess I should have continued googling first:

awk 'BEGIN {x=1}; {if (length < 3) print x; x++}' file

Steve

Janis Papanagnou

unread,
Apr 17, 2013, 5:59:10 PM4/17/13
to
On 17.04.2013 23:48, steve wrote:
> Sometimes I have files which are double-spaced and I use
>
> awk 'length > 2' file
>
> to strip away the blank lines.

awk NF file

will strip away all empty (and blank) lines.

>
> Now I have some blank lines within a file and I would like to know the
> line numbers on which they occur. I thought that I could do something
> like:
>
> awk '{length < 5} print NR' file

You misplaced the brackets. Try

awk 'length < 5 {print NR}' file

But (as above) you may also want to try

awk '!NF {print NR}' file

(The variable NF provides the number of fields.)

Janis

Janis Papanagnou

unread,
Apr 17, 2013, 6:05:40 PM4/17/13
to
On 17.04.2013 23:57, steve wrote:
> On Wed, 17 Apr 2013 21:48:13 +0000, steve wrote:
>
>> Sometimes I have files which are double-spaced and I use
>>
>> awk 'length > 2' file
>>
>> to strip away the blank lines.
>>
>> Now I have some blank lines within a file and I would like to know the
>> line numbers on which they occur. I thought that I could do something
>> like:
>>
>> awk '{length < 5} print NR' file
>>
>> However, that gives an error.
>>
>> Any suggestions would be welcome.
>>
>> Thanks, Steve
>
> Guess I should have continued googling first:

No, you rather should have avoided the web source where you found that code.

>
> awk 'BEGIN {x=1}; {if (length < 3) print x; x++}' file

That's a very clumsy way to do what you want. (See my other post.)

Janis

>
> Steve
>

steve

unread,
Apr 17, 2013, 6:17:00 PM4/17/13
to
awk '!NF {print NR}' file worked properly. The 2nd suggestion didn't
return anything.

Thanks, Janis

steve

unread,
Apr 17, 2013, 6:18:32 PM4/17/13
to
Correction:
awk 'length < 5 {print NR}' file worked properly.

awk '!NF {print NR}' file didn't return anything.

Steve

Janis Papanagnou

unread,
Apr 17, 2013, 6:27:01 PM4/17/13
to
Which "2nd"? - You mean this one...? awk 'length < 5 {print NR}' file
Works for me.

If your awk's version doesn't support the length function without
arguments - but then your own try should as well not work - then try:
awk 'length($0) < 5 {print NR}' file
But I suggest not to compare to some arbitrary value 5 instead of any
real empty-test. But instead of "< 5" you could also just use
!length($0) {print NR}
Though, if you're operating in a WinDOS environment some spurious CR
(^M) might be considered a character and length return something >0.
(But I have no WinDOS to check that.)

Janis

>
> Thanks, Janis
>

Janis Papanagnou

unread,
Apr 17, 2013, 6:31:55 PM4/17/13
to
Maybe spurious ^M in your file that are counted as part of field data?
Are the lines in your file probably CRLF terminated?

od -c file

will show you.

Janis

>
> Steve
>

nag

unread,
Apr 17, 2013, 9:14:43 PM4/17/13
to
awk 'NF<1{print NR}'

Janis Papanagnou

unread,
Apr 18, 2013, 3:01:46 AM4/18/13
to
On 18.04.2013 03:14, nag wrote:
> On Thursday, 18 April 2013 03:48:32 UTC+5:30, steve wrote:
[...]
>>
>>
>> awk '!NF {print NR}' file didn't return anything.
>>
>
> awk 'NF<1{print NR}'

NF can not become negative, so NF<1 is not different from NF<=0, NF==0,
or just !NF.

Janis

>

0 new messages