Computing.Net > Forums > Windows XP > Batch to add line numbers

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch to add line numbers

Reply to Message Icon

Name: Cruisader03
Date: April 1, 2008 at 12:08:28 Pacific
OS: XP
CPU/Ram: any
Product: any
Comment:

I am trying to add line numbers to a text file through a batch. any thoughts?

I have this:

SET /a count=0

FOR /f %%f in (%1) DO CALL :countLines %%f
GOTO :showLineCount

:countLines

@ECHO %count%
SET /a count+=1
GOTO :end

:showLineCount

ECHO %count%

:end

But i'm wanting to add the number to the beginning any give text file, not just count the numbers



Sponsored Link
Ads by Google

Response Number 1
Name: spoonman184
Date: April 1, 2008 at 12:49:13 Pacific
Reply:

What I am thinking is parse each line of your text file to a new file for each of them, while adding %count% before each line. Then type all those files to your main file, then do something like
del line*.txt
to get rid of the files created from this file.
crappy OEM comptuers...


0

Response Number 2
Name: Cruisader03
Date: April 1, 2008 at 13:16:51 Pacific
Reply:

So, I almost have what I am needing. Here is what I am working with now. It counts, skips the first line, and types...but only the first word of the file.

---------------
SET /a count=0

FOR /f "TOKENS=* skip=1" %%f in ('type %1') DO CALL :countLines %%f
GOTO :end

:countLines

@ECHO %count% %1
SET /a count+=1
GOTO :end

:end

---------------
Using the text:
Mary had a little lamb
Its fleece was white as snow
And every where that Mary went
The lamb was sure to go

here is what I get

0 Its
1 And
2 The

It's not typing the entire line, just the first word.


0

Response Number 3
Name: Razor2.3
Date: April 1, 2008 at 18:01:44 Pacific
Reply:

What's wrong with

findstr /n ".*" <whatever>


0

Response Number 4
Name: ghostdog
Date: April 2, 2008 at 00:39:53 Pacific
Reply:

if you can download and install gawk for windows here..: http://gnuwin32.sourceforge.net/pac...

save the below as script.awk


NR>1{print NR-1":"$0}

on the command line:


c:\test> more file
Mary had a little lamb
Its fleece was white as snow
And every where that Mary went
The lamb was sure to go

c:\test> gawk -f script.awk file
1:Its fleece was white as snow
2:And every where that Mary went
3:The lamb was sure to go



0

Response Number 5
Name: Cruisader03
Date: April 2, 2008 at 07:09:35 Pacific
Reply:

ghostdog...that is exacly what I was needing. gawk.exe is not dependant on anything else, and it did the job perfectly.

the purpose of the batch was to ping server or workstation names within a file and give me a final output in a csv like this

0, machine name, ip
1, machine name, ip
2, machi.......

thnx a mill...


0

Related Posts

See More



Response Number 6
Name: Cruisader03
Date: April 2, 2008 at 11:35:41 Pacific
Reply:

Razor2.3

the format isn't condusive for what i was trying to do.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Windows XP Forum Home


Sponsored links

Ads by Google


Results for: Batch to add line numbers

batch to Insert line to text file www.computing.net/answers/windows-xp/batch-to-insert-line-to-text-file/161650.html

Use Batch to sort lines in a text f www.computing.net/answers/windows-xp/use-batch-to-sort-lines-in-a-text-f/165530.html

Batch to edit Batch www.computing.net/answers/windows-xp/batch-to-edit-batch/171398.html