Computing.Net > Forums > Programming > batch edit specific line in txt

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 edit specific line in txt

Reply to Message Icon

Name: timothy_aah
Date: August 3, 2008 at 13:29:39 Pacific
OS: xp/vista
CPU/Ram: amd x2 4200+/2GB
Comment:

hi,

is there any way to find a specific line in a txt/ini/inf file, alter that line from the batch file and save the edited file?

example:
the file blabla.ini contains this line somewhere:
DefaultFOV=90.000000

I want to locate that line, and change it to
DefaultFOV=110.00000

when the batch file terminates, blabla.ini has to be saved, containing that new information!


if it cannot be saved, maybe it's possible to copy blabla.ini's content up until that specific line into a new file, then add the new line, then copy the remaining content from blabla.ini, then delete blabla.ini and rename the newly made file to blabla.ini?



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: August 3, 2008 at 19:32:51 Pacific
Reply:

if you can download gawk from here:http://gnuwin32.sourceforge.net/packages/gawk.htm


BEGIN{FS="="}
$1 ~ /DefaultFOV/{
$2=110.00000
}{print}


save the above as script.awk and on command line


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


0

Response Number 2
Name: Mechanix2Go
Date: August 3, 2008 at 20:25:19 Pacific
Reply:

@echo off > newfile
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (myfile) do (
echo %%a | find "DefaultFOV=90.000000" > nul
if !errorlevel! equ 0 (
echo DefaultFOV=110.00000 >> newfile
) else (
echo %%a >> newfile
)
)


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

M2


0

Response Number 3
Name: ghostdog
Date: August 3, 2008 at 21:21:27 Pacific
Reply:

@mech, why don't you find the pattern in the file instead.?


for .... in (find "DefaultFOV=90.000000" file)
do (
....
)


0

Response Number 4
Name: Mechanix2Go
Date: August 3, 2008 at 21:26:21 Pacific
Reply:

gd,

you lost me


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

M2


0

Response Number 5
Name: ghostdog
Date: August 4, 2008 at 01:58:54 Pacific
Reply:

@mech, in your example, you are going through the file and then piping each line to find. If the file has 1 million lines, your example will execute find 1 million times. What i am proposing is using find itself to iterate the file, because the find command takes in a file as input and it would be faster.


0

Related Posts

See More



Response Number 6
Name: timothy_aah
Date: August 4, 2008 at 02:54:13 Pacific
Reply:

thx guys!
@mech:
it works!

@gd:
haven't tested gawk yet, the idea is to be able to run this script from an imagefile/cd/dvd, so it can't depend on any non-standard software


so is it possible to speed up the script like ghostdog suggested?


0

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

you just have to bring gawk.exe into your DVD. Its a standalone program very small in size and no installation is needed.


0

Response Number 8
Name: timothy_aah
Date: August 4, 2008 at 03:42:51 Pacific
Reply:

ah, ok, that's what I was wondering
I'll give it a shot!


0

Response Number 9
Name: Mechanix2Go
Date: August 4, 2008 at 03:59:37 Pacific
Reply:

ghostdog is right about FIND slowing things down. This version avoids FIND.

::==========================
@echo off > newfile
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in (myfile) do (
if "%%a"=="DefaultFOV=90.000000" (
echo DefaultFOV=110.00000 >> newfile
) else (
echo %%a >> newfile
)
)


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

M2


0

Response Number 10
Name: timothy_aah
Date: August 4, 2008 at 04:06:55 Pacific
Reply:

wow ok, that definately speeds the script up A LOT!

I'm gonna go with Mech's script, that way I don't have to add anything else^^

@gd:
thx for suggesting Gawk anyway dude, I'm sure it'll come in handy!


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: batch edit specific line in txt

Batch to del specific lines in txt www.computing.net/answers/programming/batch-to-del-specific-lines-in-txt/13482.html

echo text to specific line in file www.computing.net/answers/programming/echo-text-to-specific-line-in-file/11965.html

Reading Specific Lines in C++ www.computing.net/answers/programming/reading-specific-lines-in-c/2843.html