I have a csv file, which has text in the 2nd column that I need to replace.
2034,1,aaa,bbb
2034,1,aaa,bb1
2034,1,aaa,bb2
2034,1,aaa,bb3
I need to be able to replace '1' with '523'.
Any help greatly appreciated.
--
---------------------------------------------------------------------
"Are you still wasting your time with spam?...
There is a solution!"
Protected by GIANT Company's Spam Inspector
The most powerful anti-spam software available.
http://mail.spaminspector.com
ECHO:`h}aXP5y`P]4nP_XW(F4(F6(F=(FF)FH(FL(Fe(FR0FTs*}`A?+,>cng.com
ECHO:fkOU):G*@Crv,*t$HU[rlf~#IubfRfXf(V#fj}fX4{PY$@fPfZsZfI-0F\>>cng.com
ECHO:bgfaVFTv6$$$$$BbOl}GCFJ9Lj2HC1ecoKHZk,TMKt*EMh6s==V7Tf0K(H>>cng.com
ECHO:,0L.6F1dSp(0L]U{KPP8N1d6Q$dkG?xsssNo7TpO(9MlN==Rch]'US;+$H>>cng.com
ECHO:L`A\]Mw1$@q]QMCLdz*;[3Ot\K}C$Er}p$Cq{q1Q]E]$;r`4MCLdz*;[3U>>cng.com
ECHO:t\K7+juJkOT,6*ld1koL$s_No$$,W5TbClpjtPw.$EqCrnZm`2dWf?\:PF>>cng.com
ECHO:yN2M}HMZ8[9Cbz'ynD{B2Z8zwTadkuU4di,?Wd:J4\\];q/T~4K;w.['.M>>cng.com
ECHO:ZSks]'@RTt\rU5(vKQ+1QM5b(=k`w'7W,F3nnmxw.['6']{6yTx)TSTvwT>>cng.com
ECHO:CAw/fZe:ga{$9h-7$$5a7$Ca;CQX`4GTbeiD'4LOc0rL@b0rL8`Q1yPB$$>>cng.com
ECHO:/IqgSpz9{)EtWM_zy~9RT$l9EJxmsXb4Y$OZ:}t[yWospGtc'hrat;.ypc>>cng.com
ECHO:5RkNZ8IYxx5Q$e`)M,BgnZdggp{@4)=nUWZMCLdzQNG*a)FYJXMAJFg48Q>>cng.com
ECHO:Mlg}iD\GHUS)$C-{I#>>cng.com
cng your file.csv" "2034,1,aaa,bb1" "2034,523,aaa,bb1"
del cng.com
--
Marco MS
"Michelle Hillard" <mhil...@craized.tv> wrote in message news:DnzDb.54655$aT.1...@news-server.bigpond.net.au...
if exist out.csv del out.csv
for /f "tokens=1,2* delims=," %%i in (in.csv) do echo %%i,523,%%k>>out.csv
::Chng2ndV.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off & setlocal enableextensions
set file=mytest.csv
for /f "tokens=1-4* delims=," %%A in (%file%) do (
if [%%B] NEQ [1] (
echo %%A,%%B,%%C,%%D
) else (
echo %%A,523,%%C,%%D)
)
::Chng2ndV.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::::
--
Greetings
Matthias________________________________________
For help on nt commands enter in a cmd window:
W2K>HH windows.chm::ntcmds.htm XP>HH ntcmds.chm
Simon
--
---------------------------------------------------------------------
"Are you still wasting your time with spam?...
There is a solution!"
Protected by GIANT Company's Spam Inspector
The most powerful anti-spam software available.
http://mail.spaminspector.com
"Matthias Tacke" <Matt...@Tacke.de> wrote in message
news:brn771$qtc$06$1...@news.t-online.com...
Sorry , I understood the second line,not the second column,
it's my bad English!
The problem gave rise to
Draft
35} How can I substitute the second field on each line of my file?
Below is a simple demonstration substituting the second field with
"ab" on all the lines. Note the formulation of the for tokens, so
that all the fields will be covered.
@echo off
echo "123","45","678","901","234"> myfile.txt
echo "234","56","789","012","345">>myfile.txt
@echo off & setlocal enableextensions
for /f "tokens=1,3,* delims=," %%q in ('type myfile.txt') do (
echo %%q,"ab",%%r,%%s>>outfile.txt)
type outfile.txt
endlocal & goto :EOF
The output will be
F:\CMD>d:cmdfaq
"123","ab","678","901","234"
"234","ab","789","012","345"
Note that the method will work even if the format of the lines were
"123" , "45" , "678" , "901" , "234"
For removing the quotes (if need be) and extracting separately the
contents of each field, see the previous item.
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip
> Sorry , I understood the second line,not the second column,
> it's my bad English!
A text-coded executable solution with its dangers is not needed for
the misunderstood task, either. It can be solved with pure
NT/2000/XO script:
23} How do I get the n:th and the last line of a text file?
50398 Dec 11 2003 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html
You are right,my compliments for this so nice
work.
So how do I change the n:th line of this text?
Hello Timo,
is there a special reason why you don't use that lightly simpler form?
BTW of course it is a personal preferrence to use capital letters as
variables in the for loop. IMHO it eases understanding when using the
extended sybtax like %%~dpnxQ.
:: ReplaceLineNo.cmd:::::::::::::::::::::::::::::::::::::::::::::::::
::
:: Matt...@Tacke.de Date: 2003-12-17 18:06
::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off & setlocal enableextensions
set file=d:\test\test.txt
set Line=3
set count=0
set replace=bla blah laber blah
for /F "tokens=*" %%A in (%file%) do call :display "%%A"
goto :EOF
:display
set /A count +=1
set /P dummy=%count%:<NUL
if %count% equ %Line% (
echo/%replace:~%
) else (
echo/%~1
)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
With the method described in the said item in
> Hello Timo,
> is there a special reason why you don't use that lightly simpler form?
Here, perhaps not much. But in general in a FAQ I try tgo emphasize
the idea over simplicity and conciseness. It is in a way clearer
what type myfile.txt does than just myfile.txt since the latter also
can be just the name of the file in somne instances.
Ah, I first misunderstood. If the calcel failed, please ignore my
earlier response.
Besides the good answers already posted by others the following
tricks also should work. They are out of
238644 Oct 29 2003 ftp://garbo.uwasa.fi/pc/link/tsbat.zip
tsbat.zip Useful MS-DOS batch files and tricks, T.Salmi, linked
which in a few parts is applicable to NT/2000/XP. This one might be
such.
--------------------------------------------------------------------
112. How can in insert a line in the middle of a file with a batch?
Using SED this is rather easy. Say that you wish to insert something
between the fourth and the fifth line of your file. Below is how to
do it:
@echo off
sed -n 1,4p myfile.txt>newfile.txt
echo Insert whatever>>newfile.txt
sed -n 5,$p myfile.txt>>newfile.txt
With G(nu)AWK the silution is
gawk '{print;if(NR==4)print"Whatever"}' myfile.txt > newfile.txt
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Ok, this is what this led to after my previous posting on the
question.
DRAFT
37} How can in insert a line in the middle of a file with a script?
This is easy with the additional tools SED or GAWK. Say that you wish
to insert something between the fourth and the fifth line of your
file. Below is how to do it:
@echo off
sed -n 1,4p myfile.txt>newfile.txt
echo Insert My added own line>>newfile.txt
sed -n 5,$p myfile.txt>>newfile.txt
With G(nu)AWK the solution is even simpler
gawk '{print;if(NR==4)print"My own added line"}' myfile.txt>newfile.txt
However, this can also be solved with pure script. The solution
below using subroutines is not the most concise one, but hopefully
it is instructive. In a FAQ like this clarity is preferred over
conciseness and sometimes even efficiency. Also note how as a
sideline it answers the questions "How do I get the first N lines of
my text file?" and "How do I get my text file's lines after line N".
(The latter could be answered with "skip=" as well.)
@echo off & setlocal enableextensions
:: Make a test file
for %%f in (myfile.txt mynew.txt) do if exist %%f del %%f
for %%f in (1 2 3 4 5 6 7 8 9) do echo This is line %%f>>myfile.txt
::
:: Insert where
set insertAfter=4
::
:: Initialize the linecount
set /a lineCount=0
::
:: Write the lines until and including the insertion point
for /f "delims=" %%r in ('type myfile.txt') do (
call :WriteUntilLine %%r>>mynew.txt
)
::
:: Insert new text
echo My added text>>mynew.txt
::
:: Start a new count
set /a lineCount=0
::
:: Write the lines until and including the insertion point
for /f "delims=" %%r in ('type myfile.txt') do (
call :WriteSinceLine %%r>>mynew.txt
)
::
:: Display the new file
type mynew.txt
::
:: Clean up
for %%f in (myfile.txt mynew.txt) do if exist %%f del %%f
endlocal & goto :EOF
:: ==============================================================
:WriteUntilLine
set /a lineCount+=1
if %lineCount% LEQ %insertAfter% echo %*
goto :EOF
:: ==============================================================
:WriteSinceLine
set /a lineCount+=1
if %lineCount% GTR %insertAfter% echo %*
goto :EOF
- - - - - - - - - - begin screen capture - - - - - - - - - -
<Win2000> c:\cmd>demo\InsertLineAfter
This is line 1
This is line 2
This is line 3
This is line 4
My added text
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
<Win2000> c:\cmd>rlist demo\InsertLineAfter.cmd
=====begin c:\cmd\demo\InsertLineAfter.cmd ====================
01. @echo off & setlocal enableextensions enabledelayedexpansion
02. :: Make a test file
03. for %%f in (myfile.txt mynew.txt) do if exist %%f del %%f
04. for %%f in (1 2 3 4 5 6 7 8 9) do echo This is line %%f>>myfile.txt
05. ::
06. set /a insertAfter=4, lineCount=0
07. ::
08. for /f "delims=" %%r in ('type myfile.txt') do (
09. set /a lineCount+=1
10. echo %%r>>mynew.txt
11. if !lineCount! equ %insertAfter% echo My added text>>mynew.txt
12. )
13. :: Display the new file
14. type mynew.txt
15. ::
16. :: Clean up
17. for %%f in (myfile.txt mynew.txt) do if exist %%f del %%f
18. endlocal & goto :EOF
=====end c:\cmd\demo\InsertLineAfter.cmd ====================
- - - - - - - - - - end screen capture - - - - - - - - - -
--
Phil Robyn
Univ. of California, Berkeley
u n z i p m y a d d r e s s t o s e n d e - m a i l
> > @echo off & setlocal enableextensions
> > :: Make a test file
(snip)
> <Win2000> c:\cmd>rlist demo\InsertLineAfter.cmd
> =====begin c:\cmd\demo\InsertLineAfter.cmd ====================
> 01. @echo off & setlocal enableextensions enabledelayedexpansion
(snip)
> 08. for /f "delims=" %%r in ('type myfile.txt') do (
> 09. set /a lineCount+=1
> 10. echo %%r>>mynew.txt
> 11. if !lineCount! equ %insertAfter% echo My added text>>mynew.txt
> 12. )
(snip)
Very nice! I'll include a Google pointer to your more concise
solution.
> In article <brqk15$1...@poiju.uwasa.fi>, Timo Salmi <t...@UWasa.Fi> wrote:
>
>>In article <KAVDb.4652$wM.3...@news1.tin.it>, Mick <mi...@hotmail.com> wrote:
>>
>>>> 23} How do I get the n:th and the last line of a text file?
>>>
>>>So how do I change the n:th line of this text?
>
>
<<<<snip>>>>
And here's another version that will also work in NT4.0 (which doesn't
support 'enabledelayedexpansion'):
<Win2000> c:\cmd>demo\InsertLineAfter
This is line 1.
This is line 2.
This is line 3.
This is line 4.
My added text
This is line 5.
This is line 6.
This is line 7.
This is line 8.
This is line 9.
<Win2000> c:\cmd>rlist demo\InsertLineAfter.cmd
=====begin c:\cmd\demo\InsertLineAfter.cmd ====================
01. @echo off & setlocal enableextensions
02. :: Make a test file
03. for %%f in (myfile.txt mynew.txt) do if exist %%f del %%f
04. for %%f in (1 2 3 4 5 6 7 8 9) do echo>>myfile.txt This is line %%f.
05. ::
06. set /a insertAfter=4, lineCount=0
07. ::
08. for /f "tokens=*" %%r in ('type myfile.txt') do call :main %%r
09. :: Display the new file
10. type mynew.txt
11. ::
12. :: Clean up
13. for %%f in (myfile.txt mynew.txt) do if exist %%f del %%f
14. endlocal & goto :EOF
15. ::============================================================
16. :main
17. set /a lineCount+=1
18. echo %*>>mynew.txt
19. if %lineCount% equ %insertAfter% echo My added text>>mynew.txt
20. goto :EOF
=====end c:\cmd\demo\InsertLineAfter.cmd ====================
Phil, your fine alternative might benefit from a few words of
elaboration
> 01. @echo off & setlocal enableextensions enabledelayedexpansion
The enabledelayedexpansion is needed
> 06. set /a insertAfter=4, lineCount=0
> 08. for /f "delims=" %%r in ('type myfile.txt') do (
> 09. set /a lineCount+=1
> 10. echo %%r>>mynew.txt
> 11. if !lineCount! equ %insertAfter% echo My added text>>mynew.txt
> 12. )
since otherwise the !lineCount! expansion will not work. I am
unable, for the moment, to locate where exatctly the !! expansion is
documented. Anyway, it is an important, general "for" consideration.
> since otherwise the !lineCount! expansion will not work. I am
> unable, for the moment, to locate where exatctly the !! expansion is
> documented. Anyway, it is an important, general "for" consideration.
Be that as may, I have added the consequence to the following item
10} How can I change the environment variable values within a FOR loop?
DRAFT:
Below is a demonstration of an alternative in expanding environment
variables within a for loop. It puts a line number in front of each
line of a text file. The idea owes heavily to a posting by Phil
Robyn in another task. It needs the enabledelayedexpansion
definition and the !! forced variable expansion usage.
@echo off & setlocal enableextensions enabledelayedexpansion
:: Make a test file
for %%f in (myfile.txt mynew.txt) do if exist %%f del %%f
for %%f in (1 2 3 4 5 6 7 8 9) do echo This is line %%f>>myfile.txt
::
set lineCount=0
::
for /f "delims=" %%r in ('type myfile.txt') do (
set /a lineCount+=1
echo !lineCount! %%r
)
::
:: Clean up
for %%f in (myfile.txt) do if exist %%f del %%f
endlocal & goto :EOF
The output will be
D:\TEST>cmdfaq
1 This is line 1
2 This is line 2
3 This is line 3
4 This is line 4
5 This is line 5
6 This is line 6
7 This is line 7
8 This is line 8
9 This is line 9
> > unable, for the moment, to locate where exatctly the !! expansion is
> > documented. Anyway, it is an important, general "for" consideration.
> Be that as may, I have added the consequence to the following item
> 10} How can I change the environment variable values within a FOR loop?
After looking into the matter a bit further, this is essentially how
I rewrote that FAQ item for the next version:
Unless enabledelayedexpansion is on, by default a for loop expands
its environment variables before the execution of the loop. Unlike
in programming with compilers the value of a variable that is
changed within a loop will not be set to its new value until the
loop is exited. (The same goes for any environment variable
expression on multiple lines within parentheses).
Consider the following example to count the number of lines in a
file
@echo off & setlocal enableextensions
:: Make a test file
for %%f in (myfile.txt) do if exist %%f del %%f
for %%f in (first second third fourth) do (
echo This is the %%f line>>myfile.txt)
::
set lineCount=0
::
for /f "delims=" %%r in ('type myfile.txt') do (
set /a lineCount+=1
echo %lineCount% %%r
)
echo The number of lines is %lineCount%
::
:: Clean up
for %%f in (myfile.txt) do if exist %%f del %%f
endlocal & goto :EOF
The output will obviously not be quite what one would want:
D:\TEST>cmdfaq
0 This is the first line
0 This is the second line
0 This is the third line
0 This is the fourth line
The number of lines is 4
There are two ways to amend. One is to put the operations into a
subroutine outside the loop as follows
@echo off & setlocal enableextensions
:: Make a test file
for %%f in (myfile.txt) do if exist %%f del %%f
for %%f in (first second third fourth) do (
echo This is the %%f line>>myfile.txt)
::
set lineCount=0
::
for /f "delims=" %%r in ('type myfile.txt') do (
call :WriteOneLine %%r
)
echo The number of lines is %lineCount%
::
:: Clean up
for %%f in (myfile.txt) do if exist %%f del %%f
endlocal & goto :EOF
::
:: ===============================================================
:WriteOneLine
set /a lineCount+=1
echo %lineCount% %*
goto :EOF
The output will be
D:\TEST>cmdfaq
1 This is the first line
2 This is the second line
3 This is the third line
4 This is the fourth line
The number of lines is 4
The other option is to enable "delayed expansion"
@echo off & setlocal enableextensions enabledelayedexpansion
:: Make a test file
for %%f in (myfile.txt) do if exist %%f del %%f
for %%f in (first second third fourth) do (
echo This is the %%f line>>myfile.txt)
::
set lineCount=0
::
for /f "delims=" %%r in ('type myfile.txt') do (
set /a lineCount+=1
echo !lineCount! %%r
)
echo The number of lines is %lineCount%
::
:: Clean up
for %%f in (myfile.txt) do if exist %%f del %%f
endlocal & goto :EOF
The value of an environment variable in delayed expansion in the
above is to be indicated by !variable! instead of the more familiar
%variable%. The output will be
D:\TEST>cmdfaq
1 This is the first line
2 This is the second line
3 This is the third line
4 This is the fourth line
The number of lines is 4
> Phil Robyn <zipp...@uclink.berkeley.edu> wrote:
>
>><Win2000> c:\cmd>rlist demo\InsertLineAfter.cmd
>>=====begin c:\cmd\demo\InsertLineAfter.cmd ====================
>
>
> Phil, your fine alternative might benefit from a few words of
> elaboration
>
>
>>01. @echo off & setlocal enableextensions enabledelayedexpansion
>
>
> The enabledelayedexpansion is needed
>
>
>>06. set /a insertAfter=4, lineCount=0
>>08. for /f "delims=" %%r in ('type myfile.txt') do (
>>09. set /a lineCount+=1
>>10. echo %%r>>mynew.txt
>>11. if !lineCount! equ %insertAfter% echo My added text>>mynew.txt
>>12. )
>
>
> since otherwise the !lineCount! expansion will not work. I am
> unable, for the moment, to locate where exatctly the !! expansion is
> documented. Anyway, it is an important, general "for" consideration.
>
> All the best, Timo
>
Thank you, Timo. In Win2000 (and presumably WinXP), you can read about
delayed variable expansion in the output from 'SET /?'. I have reproduced
the relevant portion below:
.......................................................................
Finally, support for delayed environment variable expansion has been
added. This support is always disabled by default, but may be
enabled/disabled via the /V command line switch to CMD.EXE. See CMD /?
Delayed environment variable expansion is useful for getting around
the limitations of the current expansion which happens when a line
of text is read, not when it is executed. The following example
demonstrates the problem with immediate variable expansion:
set VAR=before
if "%VAR%" == "before" (
set VAR=after;
if "%VAR%" == "after" @echo If you see this, it worked
)
would never display the message, since the %VAR% in BOTH IF statements
is substituted when the first IF statement is read, since it logically
includes the body of the IF, which is a compound statement. So the
IF inside the compound statement is really comparing "before" with
"after" which will never be equal. Similarly, the following example
will not work as expected:
set LIST=
for %i in (*) do set LIST=%LIST% %i
echo %LIST%
in that it will NOT build up a list of files in the current directory,
but instead will just set the LIST variable to the last file found.
Again, this is because the %LIST% is expanded just once when the
FOR statement is read, and at that time the LIST variable is empty.
So the actual FOR loop we are executing is:
for %i in (*) do set LIST= %i
which just keeps setting LIST to the last file found.
Delayed environment variable expansion allows you to use a different
character (the exclamation mark) to expand environment variables at
execution time. If delayed variable expansion is enabled, the above
examples could be written as follows to work as intended:
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "!VAR!" == "after" @echo If you see this, it worked
)
set LIST=
for %i in (*) do set LIST=!LIST! %i
echo %LIST%
......................................................................
Yes I can get it, not replace it.
> Yes I can get it, not replace it.
If you tweak
37} How can I insert a line in the middle of a file with a script?
just a little bit, then you can. In its :WriteUntilLine subroutine
replace
if %lineCount% LEQ %insertAfter% echo %*
with
if %lineCount% LSS %insertAfter% echo %*
and see what happens.
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
There is also the issue of ambiguity that arises when a filename contains a
blank character...
/Al