add text string to a text file
Moderator: DosItHelp
add text string to a text file
Hello,
Hello,
I found the following code from IVO
------------------------------------------------------------------------------------------------------
:: NUM.BAT Usage: NUM File_IN > File_OUT
@echo off
for /F "tokens=1* delims=[]" %%a in ('find /V /N "" ^< %*') do (
echo.%%a:%%b
)
:: End_Of_batch
--------------------------------------------------------------------------------------------------------
How do I add a text string such as ### to the file, I like this text string as part of the batch command line.
ie NUM.bat ### File_IN > File_OUT
input file:
TEST
TEST
TEST
output
###1:TEST
###2:TEST
###3:TEST
Thank you
Bob
Hello,
I found the following code from IVO
------------------------------------------------------------------------------------------------------
:: NUM.BAT Usage: NUM File_IN > File_OUT
@echo off
for /F "tokens=1* delims=[]" %%a in ('find /V /N "" ^< %*') do (
echo.%%a:%%b
)
:: End_Of_batch
--------------------------------------------------------------------------------------------------------
How do I add a text string such as ### to the file, I like this text string as part of the batch command line.
ie NUM.bat ### File_IN > File_OUT
input file:
TEST
TEST
TEST
output
###1:TEST
###2:TEST
###3:TEST
Thank you
Bob
Re: add text string to a text file
Code: Select all
:: NUM.BAT Usage: NUM Pre File_IN > File_OUT
@echo off
for /F "tokens=1* delims=[]" %%a in ('find /V /N "" ^< %2') do (
echo.%~1%%a:%%b
)
:: End_Of_batch
Call NUM.bat "" File_IN > File_OUT for add nothing
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: add text string to a text file
Use a good text processing tool to do the job. download gawk for windows, then use this one liner
Code: Select all
C:\test>gawk "{print \"###\"NR\":\"$0}" file
###1:TEST
###2:TEST
###3:TEST
Re: add text string to a text file
Thanks !K & ghostmachine4.
Hi !K,
It works great. Is there any solutions for special characters such as &, it doesn't seem to work.
Regards
Bob
Hi !K,
It works great. Is there any solutions for special characters such as &, it doesn't seem to work.
Regards
Bob
Re: add text string to a text file
It is hard.
Use the gawk.
Use the gawk.
Re: add text string to a text file
Not that hard, !k.
Try
Call NUM.bat "a&b" File_IN > File_OUT
Regards
aGerman
Code: Select all
:: NUM.BAT Usage: NUM Pre File_IN > File_OUT
@echo off &setlocal enabledelayedexpansion
set "Pre=%~1"
for /F "tokens=1* delims=[]" %%a in ('find /V /N "" ^< %2') do (
echo\!Pre!%%a:%%b
)
:: End_Of_batch
Try
Call NUM.bat "a&b" File_IN > File_OUT
Regards
aGerman
Re: add text string to a text file
aGerman
You are clever. But try "!"
You are clever. But try "!"
Re: add text string to a text file
Of course, you're right !k.
But there is a simple solution.
Regards
aGerman
But there is a simple solution.
Code: Select all
:: NUM.BAT Usage NUM Pre File_IN > File_OUT
@echo off
set "Pre=%~1"
for /F "tokens=1* delims=[]" %%a in ('find /V /N "" ^< %2') do (
set "line=%%b"
call :proc %%a
)
goto :eof
:proc
setlocal enabledelayedexpansion
echo\!Pre!%1:!line!
endlocal
goto :eof
Regards
aGerman
Re: add text string to a text file
Thanks aGerman.
Can you help with a small modification?
input:
TestA
TestB
TestC
call NUM.bat "###" File_IN > File_OUT
output:
###:TestA
###:TestB
###:TestC
call NUM.bat "###" File_IN /i > File_OUT
output:
###1:TestA
###2:TestB
###3:TestC
TIA
Regards
Bob
Can you help with a small modification?
input:
TestA
TestB
TestC
call NUM.bat "###" File_IN > File_OUT
output:
###:TestA
###:TestB
###:TestC
call NUM.bat "###" File_IN /i > File_OUT
output:
###1:TestA
###2:TestB
###3:TestC
TIA
Regards
Bob
Re: add text string to a text file
No prob.
Regards
aGerman
Code: Select all
:: NUM.BAT Usage NUM Pre File_IN [/i] > File_OUT
@echo off
set "Pre=%~1"
for /F "tokens=1* delims=[]" %%a in ('find /V /N "" ^< %2') do (
set "line=%%b"
call :proc %%a %3
)
goto :eof
:proc
setlocal enabledelayedexpansion
if /i "%~2"=="/i" (
echo\!Pre!%1:!line!
) else (
echo\!Pre!:!line!
)
endlocal
goto :eof
Regards
aGerman
Re: add text string to a text file
aGerman, thank you so much!
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: add text string to a text file
why maintain so much code, when you can have a one liner
how about putting some tabs in between
Code: Select all
C:\test>gawk "{print \"!!!\"NR\":\"$0}" file
!!!1:TEST
!!!2:
!!!3:TEST
!!!4:TEST
C:\test>gawk "{print \"!@$\"NR\":\"$0}" file
!@$1:TEST
!@$2:
!@$3:TEST
!@$4:TEST
how about putting some tabs in between
Code: Select all
C:\test>gawk "{print \"!@$\t!\t@\t#\"NR\":\"$0}" file
!@$ ! @ #1:TEST
!@$ ! @ #2:
!@$ ! @ #3:TEST
!@$ ! @ #4:TEST
Re: add text string to a text file
Hello ghostmachine4,
gawk seems so complicated , I don't know where to start, but I will give a try sometime. However thanks for showing me how to use the add string feature.
I also use search and replace feature often, in both command line or from strings stored in a file. Here is some examples:
rp "old string" "new string" < File_IN.txt > File_Out.txt
rp -f old_new_string.txt < File_IN.txt > File_Out.txt
where old_new_string.txt has the following format:
old_string1 new_string1
old_string2 new_string2
etc...
Can gawk do this? If it can, please show me how.
I am mostly interested in text processing, can you point me to right section from gawk, as I said there are so MANY functions, I just don't know where to start.
Thannks in advance
Regards
Bob
gawk seems so complicated , I don't know where to start, but I will give a try sometime. However thanks for showing me how to use the add string feature.
I also use search and replace feature often, in both command line or from strings stored in a file. Here is some examples:
rp "old string" "new string" < File_IN.txt > File_Out.txt
rp -f old_new_string.txt < File_IN.txt > File_Out.txt
where old_new_string.txt has the following format:
old_string1 new_string1
old_string2 new_string2
etc...
Can gawk do this? If it can, please show me how.
I am mostly interested in text processing, can you point me to right section from gawk, as I said there are so MANY functions, I just don't know where to start.
Thannks in advance
Regards
Bob
-
- Posts: 319
- Joined: 12 May 2006 01:13
Re: add text string to a text file
bobquoy wrote:gawk seems so complicated , I don't know where to start, but I will give a try sometime. However thanks for showing me how to use the add string feature.
Everything is complicated in the beginning when you know nothing. It happens to everything you do. All you need is to take the first step and start learning it. Go to the manualto learn about it if you are interested. gawk is a little programming language capable of replacing what cmd.exe can do. So does languages like Vbscript/Python/Perl etc.
I also use search and replace feature often, in both command line or from strings stored in a file. Here is some examples:
Can gawk do this? If it can, please show me how.
Of course!! Gawk is an excellent string/file processing tool, (as well as sed). Here's how you do it
Code: Select all
c:\test> gawk "{gsub(\"old string\",\"new string\" ); print }" file
Re: add text string to a text file
Thank you, ghostmachine4.
I am definitely interested and will start learning it, it might take sometime.
Thanks for your help, maybe you can help me out if I am really stuck in the future.
Regards
Bob
I am definitely interested and will start learning it, it might take sometime.
Thanks for your help, maybe you can help me out if I am really stuck in the future.
Regards
Bob