Computing.Net > Forums > Programming > add line numbers to a text file

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.

add line numbers to a text file

Reply to Message Icon

Name: vanderend
Date: August 16, 2008 at 04:14:08 Pacific
OS: -
CPU/Ram: -
Product: -
Comment:

I was wondering if someone would help me.

I would like to add the line numbers to a text file using a .bat! it is not possible to do this with gawk. I had the script a few years ago but I lost it. so:

Such as:

hello
Sinterklaas is jarig
stoomboot leuk hoor

Run the .bat and the file would look like this:

1: hello
2: Sinterklaas is jarig
3: stoomboot leuk hoor



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: August 16, 2008 at 05:07:16 Pacific
Reply:

find /n /v "" < myfile


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: vanderend
Date: August 16, 2008 at 05:39:26 Pacific
Reply:

tnnx but is it also possible don't show this [] because this is very unwanted.

I want make the list as follow;


movie001=hello

or as follow;

movie1=hello

Do you have mebay another trick for me?

thanx.



0

Response Number 3
Name: IVO
Date: August 16, 2008 at 07:00:36 Pacific
Reply:

@echo off & setlocal EnableDelayedExpansion
set count=0
for /F "tokens=* delims=" %%j in (myfile) do (
set /A count+=1
echo.!count!: %%j >> newfile
)
:: End_Of_Batch


0

Response Number 4
Name: ghostdog
Date: August 16, 2008 at 10:03:27 Pacific
Reply:

>> it is not possible to do this with gawk.
It IS possible to do this in gawk. Its different from "not possible to do this with gawk because environment doesn't allow"

Anyway, here's an gawk solution nonetheless.


{print NR":"$0}

save as script.awk and on command line (or batch)

c:\test> gawk -f script.awk file


0

Response Number 5
Name: FishMonger
Date: August 16, 2008 at 13:55:44 Pacific
Reply:

It can also be done with a perl command without the need of the intermediate script file that gawk needs.

c:\test>perl -ne "print qq/$.: $_/" file.txt


0

Related Posts

See More



Response Number 6
Name: ghostdog
Date: August 16, 2008 at 18:01:19 Pacific
Reply:

an intermediate script file is NOT needed actually as gawk can be used like what you did with Perl, a one liner.


c:\test> gawk "{print NR\":\"$0}" file


0

Response Number 7
Name: vanderend
Date: August 17, 2008 at 07:33:38 Pacific
Reply:

tnnx a lot! I have it done with dos.


0

Response Number 8
Name: Mechanix2Go
Date: August 17, 2008 at 22:19:31 Pacific
Reply:

"tnnx a lot! I have it done with dos."

I doubt it, but I'll stay tuned.


=====================================
If at first you don't succeed, you're about average.

M2


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 Programming Forum Home


Sponsored links

Ads by Google


Results for: add line numbers to a text file

add line number to text file www.computing.net/answers/programming/add-line-number-to-text-file/16631.html

Writting a few numbers to a file www.computing.net/answers/programming/writting-a-few-numbers-to-a-file/11211.html

Export HTA values to text file www.computing.net/answers/programming/export-hta-values-to-text-file/16462.html