FOR loop to read BLANK LINES from file

January 14, 2010 at 08:38:30
Specs: Windows XP
Hello all,
I tried to search everywhere but failed to get answer. I would like to read a text file using FOR loop in batch script, but it ignores BLANK LINES. I would like to count/read all lines including BLANK LINES.

example of text file:

abc
def

ghi
jkl
mno


pqr

Now if I use For loop to read this text file, it ignores blank lines and gives me count of total 6 instead of 9 lines.

Any Idea how can I accomplish this task?

Thank you


See More: FOR loop to read BLANK LINES from file

Report •

#1
January 14, 2010 at 09:09:17
To count the lines in a file including the blank ones

find /V /C "" file.txt


Report •

#2
January 14, 2010 at 09:25:20
Thank you for your reply IVO. But my main purpose was to actually read the text file including the blank lines using FOR loop or by any other means. Can you please let me know if you have any idea?

Thanks


Report •

#3
January 14, 2010 at 09:53:47
Here how to read via For; what you want to achieve with each line is let to your ingenuity.

for /F "tokens=1* delims=]" %%j in ('type "file.txt" ^| find /V /N ""') do (
  if "%%k"=="" (echo.) else (echo.%%k)
)


Report •

Related Solutions

#4
January 16, 2010 at 04:42:43
Doesn't this seems to be a bug in "FOR /F" ???

The Help-pages state that EACH line is being reviewed, which for me means non-empty and empty lines ... weird.


Report •

#5
January 16, 2010 at 05:04:40
@tvc

From "for /?" on xp:


*snip*
By default, /F passes the first blank separated token from each line of each file. Blank lines are skipped.
*snip*


Batch Variable how to


Report •

#6
January 16, 2010 at 05:33:19
You're right, I missed that. No bug !

Report •

#7
January 16, 2010 at 09:54:05
Exactly. that's not a bug. but it shouldn't be that way. This is an unwanted feature (Blank Lines Skipped) that some developers may not like. I wanted the for loop to read through each line no matter if it is blank or not.

Solution from "IVO" works, but for huge text file like mine, it takes very long time to read each line using For loop. I thought Batch "FOR" had any option so that blank lines are not skipped.

Any idea if that's resolved in any new version of Windows DOS?


Report •

#8
January 16, 2010 at 10:33:11
Any idea if that's resolved in any new version of Windows DOS?
No, but I still hold out hope for Windows OS X.

Report •

#9
January 16, 2010 at 11:33:10
i wonder if they'll fix the tokens problem as well while they're at it (similar problem: null tokens are ignored/skipped, even
when they're delimited.)
f/e it sucks that
,,1
puts 1 into %%a (when "delimiters=,")
so you have to read each line and test for the nulls.
(strange, i was just bitching about this last night in post...)
edit: posted this before i saw the other thread response to same issue

Report •

#10
January 16, 2010 at 14:43:48
I suspect, unfortunately, Microsoft may have lost interest in
CMD, as there's nothing "new" like .NET in it, and they would
rather we all "upgraded" to Powershell. I simply don't see that
happening. It has not gained much popularity at all.

Report •

#11
January 16, 2010 at 18:04:38
It could even be a deliberate ploy leaving all of the little annoyances in, to push people away.


Batch Variable how to


Report •

#12
January 16, 2010 at 18:14:47
yeah,
i see linux in my future... better brush up on bash
(actually, i've been meaning to set a linux box anyhow, now my motivation factor is reaching near activation levels)

Report •

#13
January 17, 2010 at 00:43:50
You may get andvanced FOR options in winders2525.

If I ain't there, start without me.

LOL


=====================================
Helping others achieve escape felicity

M2


Report •

#14
January 17, 2010 at 04:49:21
Linux/Unix is superior in many ways. It's not a coincedence that the "DOS interface" (whatever it is called these days) grows more and more towards what is standard in Linux/Unix. Possibly in 2030 they will get the same level, if they continue this speed.

Report •

#15
January 17, 2010 at 06:02:23
tvc: It's not a coincedence that the "DOS interface" (whatever it is called these days) grows more and more towards what is standard in Linux/Unix.
I should probably point out that MS-DOS (as in DOS, not CMD) had an internal switch that changed the path separator to '/', and the switches character to '-'. Also, the WinNT API has always accepted either slash as a directory separator.

I don't see MS adopting ls anytime soon, though. Especially since PS! replaces dir with Get-ChildItem.


Report •

#16
January 17, 2010 at 10:05:18
I refer to functional difference, not whether or not somebody prefers / as opposed to - for a choice of parameter recognition. That's all details, also how commands are named, what is important is what they do.

Report •

#17
January 17, 2010 at 11:15:06
In some ways DOS is superior to Unix shells, e.g. to change
the file extensions of a group of files in Bourne shell it's
something like this (excuse syntax errors, I'm a bit rusty):

foreach $f in (*.c) do;
mv $f `basename $f`.cpp
od;

whereas in DOS it's just:

ren *.c *.cpp

If you type this in shell:

mv *.c *.cpp

and you already have the files A.c and B.cpp in the directory,
if you are from a DOS background you might expect A.c to be
renamed A.cpp, when in fact it gets renamed B.cpp
overwriting the original B.cpp (not funny if you haven't backed
up B.cpp)


Report •

#18
January 17, 2010 at 11:44:37
I'll keep that in my list : 5 examples where DOS is better than unix/linux

I'll add one myself, you can do a command called TITLE in XP, which I haven't found any replacement for in unix/linux


Report •

#19
January 17, 2010 at 12:02:12
tvc: I refer to functional difference
This means, what, exactly? Specifically, what do you see UNIX doing something that CMD does not, but PS! does? This isn't an attack; I'm honestly interested in this stuff. An increased number of "simple" commands? Better flow of logic? Windows certainly doesn't share one of UNIX's basic concepts: everything is a file.

What I was hinting at before was that MS has been aware of UNIX, and has borrowed features as its developers deem necessary. (I'd love to see better SSH support in Windows, but we can't have everything, I suppose.)

EDIT: To contribute, I'll throw up POWERSHELL!'s version of cat file | wc -l (assuming standard aliases):

gc file | Measure-Object -line


Report •

#20
January 18, 2010 at 01:02:35
I like TITLE. Never heard of it. LOL


=====================================
Helping others achieve escape felicity

M2


Report •

#21
January 19, 2010 at 13:27:46
Haha, greatness can be in little things. Of course, it doesn't work on the old DOS, since you need a window to have a title ... ;)

Razor; particularly stuff like WC, ... even PS is still not a standard DOS command ?! Let me think what else. Euhm, KILL ? Or a command to reboot your computer ... And what about DU ? And DF, handy as well. And did I mention LN ? I'm sure I forgot some ... but it's not just commands, it's also the set of features included in commands.

Take IF for example, now you can do an ELSE (since XP ?); but the IF command on unix/linux is still much better : ELSE, ELSIF

I'll not mention AWK, since that is a special case, but I use it a lot, and there's nothing standard on DOS ...

OK, there ya go. I'm sure that one of the above has got a DOS replacement ... if you know one, please mention. Of course, I mean STANDARD commands!

edit: And some more : TOP (how could I forgot that one), TAIL (TAIL -F !!!), HEAD, TAR, ZIP, BASENAME, DIRNAME


Report •

#22
January 19, 2010 at 14:22:42
@tvc

The advantage of all those commands is that they are included as standard, while most can be implemented as external commands(as gnu win32 ports show) for the command processor they simply are not. The problem is the lack of development/bug fixes/improvements.

Some of those commands have loose(very loose in some cases) equivalents, fsutil, shutdown, taskill or tskill, though some of the really useful ones don't(like head/tail). The really frustrating part is that a utility like windows version of sfk can implement so much of it in one .exe.


If you want to discuss further I would suggest starting a new thread, the OP is probably still getting messages stating that the thread is receiving replys.....


Batch Variable how to


Report •

#23
January 19, 2010 at 16:38:14
Hi Vishalshar91

This help any better, not tested on large file

@echo off
dir > dir.txt
for /f "delims=" %%a in ('type dir.txt ^| find /c /v ""') do set NoLines=%%a & type dir.txt
echo No Lines=%NoLines%


Report •

#24
January 23, 2010 at 06:27:31
Judago, no need to start a thread, I have no question, I'm just saying that the set of standard commands is low on features, in Windows, and you are saying the same, in other words.

Report •

Ask Question