How to get text from a specific line in Batch - batch-file

I'd like to write a batch script that reads a specific word between delimiters:
Eg my text file contains the below
!DATA
Scen|2022|m|YTD|a|b|c|d|e|f|g|h|101
Scen|2022|m|YTD|a1|b1|c1|d1|e1|f1|g1|h1|102
The text file will have around 1000 lines. I will always want to read the 3rd line.
I want to:
1. Get the first word before the pipe delimiter (eg Scen) and store to some variable
2. I then want the second word between 1st & 2nd pipe marks (eg 2022)
Any help in this would be great.

Here's an example for you to really get your teeth into:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "targetLine="
( Set /P "="
Set /P "="
Set /P "targetLine="
) 0< "textFile.ext"
If Not Defined targetLine GoTo :EOF
For /F "Delims==" %%G In ('"(Set Field) 2>NUL"') Do Set "%%G="
Set "i=1"
SetLocal EnableDelayedExpansion
Set "Field!i!=%targetLine:|=" & Set /A i +=1 & Set "Field!i!=%"
Echo %%Field1%% = %Field1%; %%Field2%% = %Field2%
Pause
The above technique has actually defined a variable for each of the pipe delimited fields. To see those just change line 13 to (Set Field) 2>NUL
(Set /P "var=") 0< "stdInput" defines var with the value of the first non empty input line, In this case you would change stdInput (textfile.ext) to your actual source file. I have therefore extended the idea to retrieve the third line. I will not explain the creation of the variables for each of the fields, simply link you to an already existing explanation.

If these are your intended executions:
!DATA // Your Line Skipped: 1
Scen|2022|m|YTD|a|b|c|d|e|f|g|h|101 // Your Line Skipped: 2
Scen|2022|m|YTD|a1|b1|c1|d1|e1|f1|g1|h1|102 // Your Line Strings To Save: Scen & 2022
... // Your Lines Remaining Skipped: Goto %:^)
A simpler way to do it would be:
#echo off & cd /d "%~dp0"
for /f "usebackq skip=2 delims=| tokens=1-2*" %%i in (`
type InputFile.txt`)do set "_str_1=%%i" && set "_str_2=%%j" & goto %:^)
%:^)
echo\ %%_str_1%% == %_str_1% && echo\ %%_str_2%% == %_str_2%

Another, perhaps simpler, method:
#echo off
setlocal EnableDelayedExpansion
rem Read the 3rd line
( for /L %%i in (1,1,3) do set /P "line3=" ) < test.txt
rem Get 1st and 2nd words from line 3
set "goto="
set "word1=%line3:|=" & !goto! & set "goto=goto continue" & set "word2=%"
:continue
echo word1="%word1%", word2="%word2%"

Related

How to read a comma delimited file and output results by 3 in a single row

I have a text file that contains more than 25k records which looks like this in a single line:
cr:121,cr:122,cr:123,cr:124,cr:221,cr:222,cr:223,cr:224,cr:225,cr:321, and so on..
I would like to create a batch file to read the comma separated values and output them in a new file by batch of 3.
Expected Result: newfile.txt
cr:121,cr:122,cr:123
cr:124,cr:221,cr:222
cr:223,cr:224,cr:225
cr:321
I searched the site but all I can find is batch commands for FOR statements that tackles files with multiple rows, not a single line.
I am currently trying
for /f "usebackq tokens=1-4 delims=," %%a in ("algtest_extract.txt") do (
echo %%a %%b %%c %%d )
but this only works for files with multiple lines. It gives me the first 4 values then exits.
In notepad++ press Ctrl+H, set Search Mode in "Regular expression", then use search pattern
([^,]+,[^,]+,[^,]+),
and repace with
\1\n
UPD
use search pattern ((?:[^,]+,){500}) if you want to use 500 elements instead of 3.
The commas in the end of each line could be replaced using search pattern ,$
I could not resist to write a batch script for accomplishing your task. Given script is called reshape.bat, provide the input text file as a command line argument, like this:
reshape.bat "algtest_extract.txt"
To store the output data into another file, say algtest_reshaped.txt, do this:
reshape.bat "algtest_extract.txt" > "algtest_reshaped.txt"
Here is the code (see all the explanatory remarks):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~1" & rem // (file to process; `%~1` means first argument)
set "_COL=3" & rem // (number of columns in the output data)
set "_SEP=," & rem // (separator character for input and output data)
set "_PAD=" & rem // (if defined, fill up last row with empty cells)
rem // Read from file:
< "%_FILE%" call :PROCESS
endlocal
exit /B
:PROCESS
rem // Initialise variables:
set "REST=" & rem // (remaining text string after the first separator)
set "LINE=" & rem // (row collector for line/row to output)
set "IDX=0" & rem // (column counter for output data)
setlocal EnableDelayedExpansion
:READ
rem // Read some text, 1023 characters/bytes at most:
set "STR=" & set /P STR=""
rem // Terminate loop if no more data are available:
if not defined STR goto :END
rem // Precede with potential remainder from previous loop:
set "STR=!REST!!STR!"
:PARSE
rem // Extract everything behind the first separator:
if defined STR set "REST=!STR:*%_SEP%=!"
rem // No separator had been found, so read more text:
if "!REST!"=="!STR!" goto :READ
rem // Extract part in front of first separator:
for /F "delims=%_SEP%" %%I in ("ITEM=!STR!") do set "%%I"
rem // Increment column counter:
set /A "IDX+=1" & if !IDX! lss %_COL% (
rem // Output line/row not yet complete, so go on assembling:
set "LINE=!LINE!%_SEP%!ITEM!"
) else (
rem // Output line/row complete, hence return it:
echo(!LINE:*%_SEP%=!%_SEP%!ITEM!
rem // Reset row collector and column counter:
set "LINE=" & set /A "IDX=0"
)
rem // Keep on parsing using the remainder:
set "STR=!REST!" & goto :PARSE
:END
rem // Return potential remaining data:
if defined _PAD (set /A "IDX=_COL-IDX-1") else (set /A "IDX=0")
set "LINE=!LINE!%_SEP%!REST!"
for /L %%I in (1,1,%IDX%) do set "LINE=!LINE!%_SEP%"
if defined LINE echo(!LINE:*%_SEP%=!
endlocal

adding a new column to csv and populate the values alternatives

I am trying to write a batch file in windows which copies / appends a new column at the starting of CSV file . and then populates with values 0 and 1 alternately
For Example:
F1,F2,F3
1,2,3
1,2,3
2,3,4
3,4,5
Now I wish to add a new column at first and add values to them
ex
F0,F1,F2,F3
0,1,2,3
1,1,2,3
0,2,3,4
1,3,4,5
Just append 0 for all even row numbers and 1 for all odd rows
Below is the code that I have written, but that just adds 0 to all rows, but I want 0 and 1 alternately
#echo off > newfile.csv & setLocal enableDELAYedeXpansion
for /f "tokens=* delims= " %%a in (J_CAFE27032018_090325.csv) do (
>> newfile.csv echo a,%%a
)
A c equivalent would be having a for loop for all even and odd columns
for(i=0;i<n;i+2)
{
add 0
}
for(i=0;i<n;i++)
{
add 0
}
Would you please help me with the batch file equivalent to traverse each odd and even rows.
This method is the same as Aacini's however it prepends the header line with #,, (can be modified).
#Echo Off & SetLocal EnableDelayedExpansion
Set "i=" & (For /F "UseBackQ Delims=" %%A In ("J_CAFE27032018_090325.csv") Do (
If Defined i (Echo !i!,%%A) Else Set "i=1" & Echo #,%%A
Set /A "i=(i+1)%%2"))>newfile.csv & Exit /B
This is one of several ways to do it:
#echo off & setLocal enableDELAYedeXpansion
set "i=0"
(for /f "delims=" %%a in (J_CAFE27032018_090325.csv) do (
echo !i!,%%a
set /A "i=(i+1)%%2"
)) > newfile.csv
However your original logic dos not correctly process the header (first) line...
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q50041056.txt"
SET "outfile=%destdir%\outfile.txt"
SET "firstline=Y"
SET "zerostart=Y"
(
FOR /f "usebackqdelims=" %%a IN ("%filename1%") DO (
IF DEFINED firstline (
ECHO F0,%%a
SET "firstline="
) ELSE (
IF DEFINED zerostart (
ECHO 0,%%a
SET "zerostart="
) ELSE (
ECHO 1,%%a
SET "zerostart=Y"
)
)
)
)>"%outfile%"
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
I used a file named q50041056.txt containing your data for my testing.
Produces the file defined as %outfile%
The usebackq option is only required because I chose to add quotes around the source filename.
This solution uses the fact that if defined interprets the current status of the variablename, so the variable in question is simply toggled between a value and nothing.
I would do it the following way -- given that no line of the input CSV file (data.csv) exceeds an overall length of 1021 characters/bytes:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~1" & rem // (CSV file to process; use first argument)
set "_SEP=," & rem // (separator character)
set "_HEAD=F0" & rem // (header text for new column)
set /A "_MOD=2" & rem // (divisor for modulo operation)
set /A "_OFF=0" & rem // (offset for modulo operation)
setlocal EnableDelayedExpansion
rem // Determine number of lines contained in CSV file:
for /F %%C in ('^< "!_FILE!" find /C /V ""') do set /A "COUNT=%%C"
rem // Read from CSV file:
< "!_FILE!" (
rem // Check whether header text is defined:
if defined _HEAD (
rem // Header text defined, so read current header:
set "LINE=" & set /P LINE=""
rem // Prepend header text for new column to current line:
echo(!_HEAD!!_SEP!!LINE!
rem // Decrement number of lines:
set /A "COUNT-=1"
)
rem // Process remaining lines in a loop:
for /L %%I in (1,1,!COUNT!) do (
rem // Read current line:
set "LINE=" & set /P LINE=""
rem // Perform modulo operation:
set /A "NUM=(%%I+_OFF-1)%%!_MOD!"
rem // Prepend remainder of division to current line:
echo(!NUM!!_SEP!!LINE!
)
)
endlocal
endlocal
exit /B
This approach uses input redirection to read from the input CSV file.
To write the output to another CSV file, say data-mod.csv, rather than to the console, use the following command line, assuming the script is called prepend-modulo.bat and the input CSV file is named data.csv, and both reside in the current directory:
prepend-modulo.bat "data.csv" > "data_mod.csv"

New CSV by combining 2 csv files

I have 2 CSV files that has
File1:
Column1,column2
data1, data2
File2:
Column3,column4, column5,column6
data3,data4,data5,data6
I have to create a new CSV file that combines both columns from file 1 with the 1st and 3rd columns from file 2 (4 total columns).
Column1,column2,column3,column5
data1,data2,data3,data5
I am looking to do this using batch file commands. Any suggestions?
Code i am using helps me copy one file.
#ECHO OFF
SETLOCAL
(
FOR /f "tokens=1-3delims=," %%a IN (file1.csv) DO (
ECHO(%%a,%%c
)
)>new.csv
GOTO :EOF
How about following script?
File1.csv :
column1,column2
data1,data2
data3,data4
data5,data6
File2.csv :
column3,column4,column5,column6
data3,data4,data5,data6
data7,data8,data9,data10
Script :
#echo off
setlocal enabledelayedexpansion
set ct1=0
for /f "tokens=*" %%i in (File1.csv) do (
set /a ct1+=1
set ar1[!ct1!]=%%i
)
set ct2=0
for /f "tokens=*" %%i in (File2.csv) do (
set /a ct2+=1
set ar2[!ct2!]=%%i
)
if !ct1! lss !ct2! (
set ct=!ct2!
) else (
set ct=!ct1!
)
for /l %%i in (1,1,!ct!) do (
echo !ar1[%%i]!,!ar2[%%i]!>> new.csv
)
new.csv :
column1,column2,column3,column4,column5,column6
data1,data2,data3,data4,data5,data6
data3,data4,data7,data8,data9,data10
data5,data6,
Here is a pure batch solution that works, but with the following limitations and or assumptions:
File 1 lines are terminated by carriage return and linefeed (Windows style)
File 1 lines are no longer than 1021 bytes
File 2 must have a value for each column (no consecutive commas)
File 2 line lengths never exceed ~8191 bytes
File 2 does not have any quoted column values that include commas.
Files 1 and 2 have the same number of lines
Neither file has quoted data values that include new lines (rare, but possible within a CSV).
#echo off
setlocal disableDelayedExpansion
<"file1.csv" >"merged.csv" (
for /f "usebackq eol=, delims=, tokens=1,3" %%A in ("file2.csv") do (
set /p "part1="
set "part2=%%A,%%B"
setlocal enableDelayedExpansion
echo !part1!,!part2!
endlocal
)
)
A much more robust and faster solution is possible if you use PowerShell, JScript, or VBS.
You can also implement an efficient and robust solution using JREPL.BAT - a regular expression text processing utility. JREPL.BAT is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward. Full documentation is available from the command line via jrepl /?, or jrepl /?? for paged help.
This JREPL solution has only the following reasonable limitations:
Files 1 and 2 must have the same number of lines
Neither file has quoted data values that include new lines
#echo off
setlocal
set "col=\q(?:\q\q|[^,\q])*\q|[^,]*"
call jrepl "^(%col%),(?:%col%),(%col%)(?:,.*|$)" ^
"$txt=stdin.ReadLine()+','+$1+','+$2" ^
/jq /x /f "file2.csv" /o "merged.csv" <"file1.csv"
This flexible script does what you want, given that the following restrictions are not violated:
both files must contain the same number of lines;
the number of columns per line/row must be equal per each file;
lines are no longer than 1023 bytes, including the terminating line-break;
field/cell values must not contain line-breaks;
each line/row must be terminated by Windows-style line-breaks (CR+LF);
the given column numbers to copy must be sorted in ascending order;
So here is the code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE1=%~dp0File1.csv" & rem // (1st input CSV file; state `%~1` to use 1st arg.)
set "_FILE2=%~dp0File2.csv" & rem // (2nd input CSV file; state `%~2` to use 2nd arg.)
set "_COLS1=1,2" & rem // (ascending list of columns to copy from 1st file)
set "_COLS2=1,3" & rem // (ascending list of columns to copy from 2nd file)
set "_SEPAR=," & rem // (separator character, usually `,`)
rem // Main routine:
4< "%_FILE1%" 3< "%_FILE2%" (
call :READLINE
)
endlocal
exit /B
:READLINE
rem // Read a line of both files:
set "LINE1=" & set "LINE2="
<&4 set /P LINE1=""
<&3 set /P LINE2=""
rem // Terminate sub-routine in case both lines are empty:
if not defined LINE1 if not defined LINE2 exit /B
rem // Process lines:
call :PROCESS LINE1 LINE2
rem // Repeat reading:
goto :READLINE
exit /B
:PROCESS ref_string1 ref_string2
setlocal DisableDelayedExpansion
set "BUF=%_SEPAR%"
setlocal EnableDelayedExpansion
rem // Test both strings against global wild-card characters:
set "STR1=!%~1!" & set "STR2=!%~2!"
if "!STR1:**=!!STR2:**=!"=="!STR1!!STR2!" goto :PROCESS_CONT
if "!STR1:*?=!!STR2:*?=!"=="!STR1!!STR2!" goto :PROCESS_CONT
if "!STR1:*<=!!STR2:*<=!"=="!STR1!!STR2!" goto :PROCESS_CONT
if "!STR1:*>=!!STR2:*>=!"=="!STR1!!STR2!" goto :PROCESS_CONT
>&2 echo(ERROR: Illegal character encountered^^!
exit /B 1
:PROCESS_CONT
rem // Prepare line strings for being processed by a standard `for` loop:
set "STR1=!STR1:"=""!^"
set "STR2=!STR2:"=""!^"
set "STR1="!STR1:%_SEPAR%=","!""
set "STR2="!STR2:%_SEPAR%=","!""
rem // `for /F` loops to transport prepared line strings beyond `endlocal`:
for /F "delims=" %%E in (^""!STR1!"^") do (
for /F "delims=" %%F in (^""!STR2!"^") do (
endlocal
rem // Process 1st line string:
set /A "IDX=0"
for %%I in (%%~E) do (
rem // Compare column index of current item with given column list:
set /A "IDX+=1" & set "FND="
for %%J in (%_COLS1%) do (
setlocal EnableDelayedExpansion
if !IDX! EQU %%J (
endlocal & set "FND=#"
) else endlocal
)
rem // Matching column index encountered, so assemble output line:
if defined FND (
set "NEW=%%~I%_SEPAR%"
setlocal EnableDelayedExpansion
for /F "delims=" %%K in (^""!BUF!!NEW!"^") do (
endlocal
set "BUF=%%~K"
)
)
)
rem // Process 1st line string:
set /A "IDX=0"
for %%I in (%%~F) do (
rem // Compare column index of current item with given column list:
set /A "IDX+=1" & set "FND="
for %%J in (%_COLS2%) do (
setlocal EnableDelayedExpansion
if !IDX! EQU %%J (
endlocal & set "FND=#"
) else endlocal
)
rem // Matching column index encountered, so assemble output line:
if defined FND (
set "NEW=%%~I%_SEPAR%"
setlocal EnableDelayedExpansion
for /F "delims=" %%K in (^""!BUF!!NEW!"^") do (
endlocal
set "BUF=%%~K"
)
)
)
setlocal EnableDelayedExpansion
)
)
rem // Return output line buffer:
echo(!BUF:~1,-1!
endlocal
endlocal
exit /B

use batch file to read and write version number into a file

I'm trying to write a batch file to increment version number every time i run it, but I'm confuse about "for /f" and the behaviour of the batch file when I test it by using command prompt. Please help me with this.
here's my batch file
for /f "tokens=2,3 " %%i in (version.h) do (
set /a number=%%j+1
echo %%i
echo %%j
echo %number%
if %%i==REVISION (
echo line1
echo #define %%i "%number%" >> temp.file
) else (
echo line2
echo #define %%i %%j >> temp.file
)
)
del /q version.h
ren temp.file version.h
and here's my version.h
#define MAJOR "1"
#define MINOR "0"
#define REVISION "242"
The batch file can only produce correct result at the first run(#define REVISION "243"), and has a weird result at the second run(#define REVISION "0"). The third run's result is correct("#define REVISION "244"), but the forth run it goes weird again(#define REVISION "1"), and so on.
It seems that I didn't parse the correct string so I cannot have correct result every time.
I typed "for /?" in the command prompt and read the help message, but still cannot understand it, please help me with this. Any reply would be appreciate!
The following script does what you want and preserves empty lines and special characters present in the target file. There is no temporary file involved, the file modification is accomplished in-place.
So here is the code -- reference the explanatory remarks for how it works:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "FILE=%~1" & rem // (provide the target file as command line argument)
set "DIRECTIVE=#define" & rem // (name of the directive to search)
set "DEFINITION=REVISION" & rem // (name of the definition to search)
set "CASESENS=" & rem // (set to non-empty for case-sensitive searches)
set "QUOTED="^" & rem // (set to non-empty for quoting returned number)
rem // Resolve arguments and options:
if not defined FILE ((>&2 echo ERROR: no file specified!) & exit /B 1)
if defined CASESENS (set "CASESENS=") else (set "CASESENS=/I")
if defined QUOTED (set "QUOTED="^") else (set "QUOTED=")
rem // Loop through all lines in the target file:
setlocal EnableDelayedExpansion
for /F delims^=^ eol^= %%L in ('
rem/ /* Prefix lines with line numbers to not lose empty ones; ^& ^
rem/ after having read file, deplete its entire content: */ ^& ^
findstr /N /R "^^" "!FILE!" ^& ^> "!FILE!" break
') do (
endlocal
set "FLAG="
set "LINE=%%L"
rem // Split line into three tokens:
for /F "tokens=1-3 eol= " %%I in ("%%L") do (
set "FIELD1=%%I"
set "FIELD2=%%J"
set "NUMBER=%%~K"
setlocal EnableDelayedExpansion
rem // Check first token for matching directive name:
if %CASESENS% "!FIELD1:*:=!"=="!DIRECTIVE!" (
rem // Check second token for matching definition name:
if %CASESENS% "!FIELD2!"=="!DEFINITION!" (
endlocal
rem // Increment number of third token:
set /A "NUMBER+=1"
set "FLAG=#"
setlocal EnableDelayedExpansion
)
)
endlocal
)
setlocal EnableDelayedExpansion
rem // Write output line into target file:
>> "!FILE!" (
rem // Check whether dirctive and definition matched:
if defined FLAG (
rem // Match found, so write new line with incremented number:
echo(!DIRECTIVE! !DEFINITION! %QUOTED%!NUMBER!%QUOTED%
) else (
rem // No match found, so write original line:
echo(!LINE:*:=!
)
)
)
endlocal
endlocal
exit /B

Batch For loop array

Okay so here is what I have.
#echo off
setLocal EnableDelayedExpansion
:begin
set /a M=0
set /a number=0
set /p Input=You:
echo %Input% >> UIS
for /F "tokens=1 delims= " %%i in ("%Input%") do (
set /a M+=1
set i!M!=%%i
)
del UIS 1>nul 2>nul
:loop
set /a number+=1
set invar=!i%number%!
echo %invar%
pause > nul
goto loop
Say, for example, the Input string was "Lol this is my input string"
I want the for loop to set i!M! where M = 1 to "Lol", where M = 2 i!M! is "this" and where M = 3 i!M! is "is" and so on. Now, of course, this can't go on forever, so even if I have to stop when M = 25 or something, and say the string was only 23 words long. Then when M = 24 and 25 then i!M! is simply null or undefined.
Any help is appreciated, thank you.
for /f reads line by line, not word by word.
Here's an answer proposed at How to split a string in a Windows batch file? and modified for your situation:
#echo off
setlocal ENABLEDELAYEDEXPANSION
REM Set a string with an arbitrary number of substrings separated by semi colons
set teststring=Lol this is my input string
set M=0
REM Do something with each substring
:stringLOOP
REM Stop when the string is empty
if "!teststring!" EQU "" goto displayloop
for /f "delims= " %%a in ("!teststring!") do set substring=%%a
set /a M+=1
set i!M!=!substring!
REM Now strip off the leading substring
:striploop
set stripchar=!teststring:~0,1!
set teststring=!teststring:~1!
if "!teststring!" EQU "" goto stringloop
if "!stripchar!" NEQ " " goto striploop
goto stringloop
:displayloop
set /a number+=1
set invar=!i%number%!
echo %invar%
pause > nul
goto displayloop
endlocal
for /F command divide a line in a definite number of tokens that must be processed at once via different replaceable parameters (%%i, %%j, etc). Plain for command divide a line in an undefined number of words (separated by space, comma, semicolon or equal-sign) that are processed one by one in an iterative loop. This way, you just need to change this for:
for /F "tokens=1 delims= " %%i in ("%Input%") do (
by this one:
for %%i in (%Input%) do (
PS - I suggest you to write the array in the standard form, enclosing the subscript in square brackets; it is clearer this way:
set i[!M!]=%%i
or
set invar=!i[%number%]!

Resources