1.

Solve : MS-DOS Text Manipulation Question?

Answer»

Hey all,

I am trying to manipulate some text using a batch file. I need to find a specific string of text then append text right after it. It is for a script to code in multiple printers in client computers for a specific PROGRAM that uses a .ini file to find printers.

I can use FIND to get the string, but how do I append to it right after it?

Thanks.go through the text file in a FOR LOOP.

FOR /F "delims==" etc (

Test each line with eg
echo %%L >> output.txt

If it gets found do your append
echo %%L | find "some text" > nul && echo appended.txt >> output.txt

did you want to go around again?

)

Ok awesome! I will try this. Thanks for the help.[thought about it a bit more]

go through the text file in a FOR loop.

FOR /F "delims==" etc (

copy each line as you go
echo %%L >> output.txt

Test each line and
If it gets found do your append

echo %%L | find "some text" > nul && (
echo some appended text >> output.txt
echo more appended text >> output.txt
echo more appended text >> output.txt
etc...
)

did you want to go around again?
Or did you want to stop here?

)
and more...

Code: [Select]@echo off
REM make example files
echo apples> "fruit.txt"
echo oranges>> "fruit.txt"
echo lemons>> "fruit.txt"
echo bananas>> "fruit.txt"

echo --- are yellow> "append.txt"
echo --- and bitter>> "append.txt"
echo --- and citric>> "append.txt"

if exist "output.txt" del "output.txt"

for /f "delims==" %%F in (fruit.txt) do (
echo %%F >> "output.txt"
echo %%F | find "lemons">nul && (
type "append.txt" >> "output.txt"
)
)
type "output.txt"
pause

output...

Code: [Select]apples
oranges
lemons
--- are yellow
--- and bitter
--- and citric
bananas
Press any key to continue . . .
you can try edlin:
1)create a edlin command file with these 2 lines using an editor that can interpret Ctrl-Z
Code: [Select]1,#Roldstring<ctrl-Z>stringnewstring
e
the above says to substitute your search pattern with string+newstring
2) save it as test.ed
3) then from command line
Code: [Select]C:\> edlin yourfile < test.ed
edlin CREATES a bakup of your original file, and "inserts" a new string after the search string.Oh wow edlin!!!! Takes me back 25 years!!! (Is this a joke?)
Quote from: contrex on May 19, 2007, 02:14:21 AM

(Is this a joke?)
what do you think?Quote from: ghostdog74 on May 19, 2007, 02:59:35 AM
Quote from: contrex on May 19, 2007, 02:14:21 AM
(Is this a joke?)
what do you think?

You can never tell in this place
Quote from: contrex on May 19, 2007, 03:01:40 AM
Quote from: ghostdog74 on May 19, 2007, 02:59:35 AM
Quote from: contrex on May 19, 2007, 02:14:21 AM
(Is this a joke?)
what do you think?

You can never tell in this place

well..
sample input:
Code: [Select]C:\TEMP>more input
This is a sample text file
to show that using edlin is a joke.

sample edlin command file:
Code: [Select]C:\temp>more test.ed
1,#Ra joke<ctrl-Z>not a joke
e

output:
Code: [Select]C:\temp>edlin input < test.ed
End of input file
*1,#Ra joke^Znot a joke
2: to show that using edlin is not a joke.
*e

final result:
Code: [Select]C:\temp>more input
This is a sample text file
to show that using edlin is not a joke.

I never intended to convey that the suggestion to use Edlin was a joke, in the sense that it wouldn't work. What I meant was that as someone who has been using PCs since 1981, I was so glad when better tools than Edlin came along! Edlin was created by Tim Paterson in two weeks in 1980, and was expected to have a six-month shelf life. Use of Microsoft's Edlin in today's environments is somewhat limited as it does not support long filenames. When attempting to describe Edlin's features, words like "crude" and "rudimentary" are often chosen. (Especially by me.)


Quote from: contrex on May 19, 2007, 03:21:33 AM
I never intended to convey that the suggestion to use Edlin was a joke, in the sense that it wouldn't work.
glad to hear that.
Quote
What I meant was that as someone who has been using PCs since 1981, I was so glad when better tools than Edlin came along!
edlin was just part of a myriad of tools that was available back then. sorry but i am curious , out of the box back then, can you suggest to me what better tools than edlin to do automated string manipulations like this? delete line number 10 to 15 for example?


Quote
Edlin in today's environments is somewhat limited as it does not support long filenames.
so just keep it short? FWIW, this should not be the reason for not using it when the need arises.

Quote
When attempting to describe Edlin's features, words like "crude" and "rudimentary" are often chosen. (Especially by me.)
well, batch is "crude"/"rudimentary" to me too if i am comparing against better tools to do the task at hand.
however, if i just want to move/copy files, i can quickly write in batch. its quick and fast. Its the same when i decided to use edlin.


Discussion

No Comment Found