I'm trying to create a report that will split into next row for each line. My data:
I,have,a,report,to,split,|,into,next,row
my,second,line,should,also,|,split,like,before
then it should be like this:
I,have,a,report,to,split
into,next,row
my,second,line,should,also
split,like,before
I have tried the script:
#echo off
setLocal
for /f "tokens=1 delims=.|" %%a in (input.csv) do echo %%a
for /f "tokens=2 delims=.|" %%a in (input.csv) do echo %%a
but the result is:
I,have,a,report,to,split
my,second,line,should,also
into,next,row
split,like,before
Anyone can help me with this? thanks in advanceā¦
Your result example is wrong. This is the output from your code:
I,have,a,report,to,split,
my,second,line,should,also,
,into,next,row
,split,like,before
Note the comma at end of line 1 and 2, and at beginning of line 3 and 4
This method works:
#echo off
setlocal EnableDelayedExpansion
rem Define a variable with CR+LF ASCII chars:
for /F %%a in ('copy /Z "%~F0" NUL') do set CRLF=%%a^
%empty line 1/2%
%empty line 2/2%
rem Change each ",|," string by CR+LF characters
for /F "delims=" %%a in (input.csv) do (
set "line=%%a"
for %%N in ("!CRLF!") do echo !line:,^|,=%%~N!
)
Of course, you may also do it in the "traditional" :/ way:
#echo off
setlocal EnableDelayedExpansion
for /F "tokens=1,2 delims=|" %%a in (input.csv) do (
set "left=%%a"
set "right=%%b"
echo !left:~0,-1!
echo !right:~1!
)
Related
for /f " tokens=%i%-%j% delims=," %%a in (%input%) DO (
(ECHO %%a %%b %%c %%d %%e %%f %%g %%h %%i %%j)>>%output%
)
"delims=" will force no delimiter and use full string making each line %%a instead of each word, delimited by ,.
for /f "delims=" %%a in (%input%) DO ECHO %%a >>%output%
If you would like to get rid of the , simply replace it.
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (%input%) DO ( set string=%%a
set string=!string: =,!
ECHO !string! >> %output%
)
endlocal
According to your comments, the following should do exactly, what you want: multiple spaces are reduced to one delimiter, no delimiters at the end of a line.
#echo off
setlocal enabledelayedexpansion
(for /f "delims=" %%a in (input.txt) do (
set "new="
for %%b in (%%a) do (
set "new=!new!,%%b"
)
echo !new:~1!
))>output.txt
Because your ? characters may be problematic with some solutions, the following may do what you require:
#Echo Off
Set "input=input.txt"
Set "output=output.txt"
Set "_="
(For /F "UseBackQ Delims=" %%A In ("%input%"
) Do Set "_="&Call :Sub %%A&Call Echo(%%_:~1%%)>"%output%"
If Not Defined _ Del "%output%"
Exit /B
:Sub
If "%1"=="" GoTo :EOF
Set "_=%_%,%1"
Shift&GoTo :Sub
I am struggling to edit the below command line to only give the name of the file name instead of the full path name and file. I would also want a comma straight after that and another comma straight after the second column (which is the created date done in do, as its a delimited notepad file.
#echo off
setlocal enabledelayedexpansion
set _SRCDIR=C:\Users\hyea\Desktop\Testing\Source
set _DSTDIR=C:\Users\hyea\Desktop\Testing\Target
for /f "delims=" %%f in ('dir /b /a-d "!_SRCDIR!\*.cos"') do (
type "!_SRCDIR!\header.txt" > "!_DSTDIR!\%%f"
call :handle_file "!_SRCDIR!\%%f" "!_DSTDIR!\%%f"
)
goto :eof
:handle_file
set _TMPVAR0=%~t1
setlocal disabledelayedexpansion
for /f "usebackq delims=" %%g in (`"findstr /n ^^ %1"`) do (
echo %%g
set "_TMPVAR1=%%g"
setlocal enabledelayedexpansion
set "_TMPVAR1=!_TMPVAR1:*:=!"
echo.%~1 !_TMPVAR0: =-! !_TMPVAR1!>> %2
endlocal
)
goto :eof
[untested, since you give no indication of the data involved and present a confused list of the outputs you need]
#echo off
setlocal enabledelayedexpansion
set _SRCDIR=C:\Users\hyea\Desktop\Testing\Source
set _DSTDIR=C:\Users\hyea\Desktop\Testing\Target
for /f "delims=" %%f in ('dir /b /a-d "!_SRCDIR!\*.cos"') do (
type "!_SRCDIR!\header.txt" > "!_DSTDIR!\%%f"
set "_TMPVAR0=%~tf"
set "_tmpvar0=!_tmpvar0: =-!"
for /f "tokens=1*delims=:" %%g in (`findstr /n ^^ ""`) do (
echo(%~nf,!_TMPVAR0!,%%h>> "!_DSTDIR!\%%f"
)
)
goto :eof
which should insert the header then copy each line, including blank lines, prefixing each line with
filenameonly,the filedate/time (with spaces replaced by -),linebody
I need a batch script which splits a file with 4 lines and creates from those 4 lines 4 txt files.
Looks like this: test.txt
line 1
line 2
line 3
line 4
--> every line should be outputted in an other txt file.
Thanks guys, Anita
#echo off
setlocal enableextensions disabledelayedexpansion
set "inputFile=test.txt"
for %%z in ("%inputFile%") do if %%~zz gtr 0 (
set "lineNumber=1000000000"
for /f "delims=" %%a in ('
findstr /n "^" "%inputFile%"
') do (
set "line=%%a" & set /a "lineNumber+=1"
setlocal enabledelayedexpansion
>"%%~fz.!lineNumber:~-9!" (echo(!line:*:=!)
endlocal
)
)
EDIT: I simplified the code a little...
#echo off
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" test.txt') do (
> file%%a.txt echo(%%b
)
If a line in the file start with colon, this solution will eliminate the colon. This problem may be fixed, if needed.
#echo off
set "filen=c:\file.txt"
for /f "usebackq tokens=* delims=" %%a in ("%filen%") do (
(echo(%%a>%%a)
)
I am trying to find a list of machines in files in folders, and print out the last line of the output only.
#echo off
for /f %%a in (computers.txt) do findstr /xs "%%a" unhealthy.txt
pause
The computers.txt file has a list of 300 machines.
I want that to output only the last line of each instance it finds.
Right now the command displays and outputs all instances of the computer name, not just the tail end. I've tried to use "tail for Windows" but am getting errors as well.
Current output:
2013\10-Oct\28\unhealthy.txt:WIN57505
2013\10-Oct\29\unhealthy.txt:WIN57505
2013\10-Oct\30\unhealthy.txt:WIN57505
2013\10-Oct\31\unhealthy.txt:WIN57505
2013\11-Nov\1\unhealthy.txt:WIN57505
2013\11-Nov\4\unhealthy.txt:WIN57505
2013\11-Nov\5\unhealthy.txt:WIN57505
2013\11-Nov\6\unhealthy.txt:WIN57505
I only want:
2013\11-Nov\6\unhealthy.txt:WIN57505
#echo off
setlocal enableextensions disabledelayedexpansion
for /f %%a in (computers.txt) do (
set "line="
for /f "tokens=*" %%b in ('findstr /xs "%%a" *') do set "line=%%b"
setlocal enabledelayedexpansion
echo(!line!
endlocal
)
pause
endlocal
setLocal enableDelayedExpansion
for /f %%a in (computers.txt) do for /f "tokens=*" %%A in ('findstr /xs "%%a"') do set lastFound=%%A
echo !lastFound!
I'm trying to find words on a first column of a CSV or XLS file, and replace them with words in the second column of the CSV of XLS. I have made something like that but it doesn't work.
Can you help me? For each line, the first column in a variable called ita and the second column in a variable called eng, and then find Ita and replace Eng. As you can imagine I need to translate a web page, starting from the csv with a language for each column. My csv file structure is:
ita1;eng1
ita2;eng2
etc...
This is my wrong script:
#echo off
setlocal enableextensions enabledelayedexpansion
set host=%COMPUTERNAME%
echo Host: %host%
pause
for /f "tokens=1 delims=;" %%Ita in (index.csv) do (
SET ita=%%Ita
echo %ita%
pause
for /f "tokens=2 delims=;" %%eng in (index.csv) do (
set eng=%%eng
echo %eng
pause
(for /f "delims=" %%i in ('findstr /n "^" "index.txt"') do (
set "transl=%%i"
set "transl=!line:%ita%=%eng%!"
echo(!line!
endlocal
))>"index2.txt"
type "index2.txt"
)
)
)
(for /f "tokens=1,2 delims=;" %%a in (index.csv) do echo(%%b;%%a)>index2.txt
type index2.txt
#echo off
setlocal enableextensions enabledelayedexpansion
(for /f "tokens=* usebackq" %%l in ("index.txt") do (
set "line=%%l"
for /f "tokens=1,2 delims=; usebackq" %%a in ("index.csv") do (
set "line=!line:%%a=%%b!"
)
echo !line!
)) > index2.txt
But this approach doesn't care of substring match.
Try using awk
awk -F';' 'NR==FNR {a[$1]=$2;next} { for(x in a) gsub(x,a[x]) } 1' index.csv index.txt