add text string to a text file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bobquoy
Posts: 6
Joined: 17 Oct 2010 00:54

add text string to a text file

#1 Post by bobquoy » 17 Oct 2010 01:02

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: add text string to a text file

#2 Post by !k » 17 Oct 2010 04:09

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: add text string to a text file

#3 Post by ghostmachine4 » 17 Oct 2010 04:46

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


bobquoy
Posts: 6
Joined: 17 Oct 2010 00:54

Re: add text string to a text file

#4 Post by bobquoy » 17 Oct 2010 11:50

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: add text string to a text file

#5 Post by !k » 17 Oct 2010 13:46

It is hard.
Use the gawk.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: add text string to a text file

#6 Post by aGerman » 17 Oct 2010 15:46

Not that hard, !k.

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: add text string to a text file

#7 Post by !k » 17 Oct 2010 16:11

aGerman
You are clever. But try "!"

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: add text string to a text file

#8 Post by aGerman » 17 Oct 2010 16:34

Of course, you're right !k.
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

bobquoy
Posts: 6
Joined: 17 Oct 2010 00:54

Re: add text string to a text file

#9 Post by bobquoy » 17 Oct 2010 16:56

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: add text string to a text file

#10 Post by aGerman » 17 Oct 2010 17:06

No prob.

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

bobquoy
Posts: 6
Joined: 17 Oct 2010 00:54

Re: add text string to a text file

#11 Post by bobquoy » 17 Oct 2010 17:11

aGerman, thank you so much!

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: add text string to a text file

#12 Post by ghostmachine4 » 17 Oct 2010 18:20

why maintain so much code, when you can have a one liner

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


bobquoy
Posts: 6
Joined: 17 Oct 2010 00:54

Re: add text string to a text file

#13 Post by bobquoy » 17 Oct 2010 22:10

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: add text string to a text file

#14 Post by ghostmachine4 » 18 Oct 2010 00:28

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 

bobquoy
Posts: 6
Joined: 17 Oct 2010 00:54

Re: add text string to a text file

#15 Post by bobquoy » 18 Oct 2010 00:48

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

Post Reply