Bat replace new line char to "|" - batch-file

I want to replace new line char to "|" use Window bat.
eg file1:
1
2
3
output:
1|2|3
I try this bat:
echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (123.txt) do (
set a=%%a
set a=!a:"\r\n"=^|!
for %%b in ("!a!") do (
echo.%%~b>>1245.txt
))
pause
But, new line char is not "\r\n". How can I get the new line char expression ?

#echo off
setlocal EnableDelayedExpansion
rem Initialize the output line
set "line="
rem Catenate all file lines in the same variable separated by "|"
for /F "delims=" %%a in (123.txt) do set "line=!line!|%%a"
rem Show final line, removing the leading "|"
echo !line:~1!>>1245.txt

You can play with this a little bit:
FOR /F "Usebackq Tokens=*" %%# IN ("File.txt") DO (
<NUL Set /P "=%%#"
)
or this, the /p removes the newLines:
C:\> echo Hello World
Hello World
C:\> echo|set /p=Hello World
Hello World

with a some edits of Krekkon's script:
#echo off
FOR /F "Usebackq Tokens=* delims=" %%# IN ("123.txt") DO (
echo|set /p=%%#^^^|
)>>temp.file
move /y "temp.file" "123.txt"

Related

Batch: split values into separate line with specific pipe delimiter

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!
)

Extract substring in batch script

set line = xxx/yyy/xxx/xxxxxxxx.h:32:#include "../../xxxx/xxxxx.h"
if this is the string, how can i extract just xxxxxxxx.h, between / and : in batch script?
i have included the bin path of cygwin to my windows environment variables so it can run grep commands.
Current, this is my script:
#echo off
SETLOCAL enabledelayedexpansion
set /p search_parameter="Type your search: "
rem run grep seach commmand
grep -nri %search_parameter% --colour --include=*.{c,h} > text.txt
rem filter required lines into new text file
type text.txt | findstr /I "#include" | findstr /V "examples DELIVERY_REL" > text2.txt
rem read text file line-by-line
for /F "tokens=* delims=" %%G in (text2.txt) do (
set "line=%%G"
rem echo line: !line!
for /f "delims=:" %%i in ("!line!") do set "line=%%i"
for %%i in (!line:/= !) do set "line=%%i"
echo line: !line!
)
pause
echo on
Currently, this is my output:
line: line:/
line: line:/
line: line:/
line: line:/
line: line:/
line: line:/
line: line:/
line: line:/
line: line:/
line: line:/
The problem is this line:
for %%i in (!line:/= !) do set "line=%%i"
Use a for /f loop to get the part before the first :.
Then use a plain for to get the last token of this part (tokenize by replacing / with spaces):
#echo off
setlocal
set "line=xxx/yyy/xxx/xxxxxxxx.h:32:#include "../../xxxx/xxxxx.h""
set line
for /f "delims=:" %%a in ("%line%") do set "line=%%a"
set line
for %%a in (%line:/= %) do set "line=%%a"
set line
Output of this code:
line=xxx/yyy/xxx/xxxxxxxx.h:32:#include "../../xxxx/xxxxx.h"
line=xxx/yyy/xxx/xxxxxxxx.h
line=xxxxxxxx.h
due to the exact format of your string, you can just:
set "line=xxx/yyy/xxx/xxxxxxxx.h:32:#include "../../xxxx/xxxxx.h""
for /f "delims=:" %%a in ("%line%") do set "line=%%~nxa"
(Thanks, #dbenham). This works because the parser "translates" the "wrong" slash (not valid for a filename) to the "correct" backslash and %%~nxa just extracts the "filename and extension" from the "full file name". (for treats the string as a filename but doesn't even care, if it's valid)
Edit
...
rem read text file line-by-line
for /F "tokens=* delims=" %%G in (text2.txt) do (
set "line=%%G"
for /f "delims=:" %%i in ("!line!") do set "line=%%~nxi"
echo line: !line!
)
#echo off
SETLOCAL enabledelayedexpansion
set /p search_parameter="Type your search: "
rem run grep seach commmand
grep -nri %search_parameter% --colour --include=*.{c,h} > text.txt
rem filter required lines into new text file
type text.txt | findstr /I "#include" | findstr /V "examples DELIVERY_REL" > text2.txt
rem read text file line-by-line
for /F "tokens=* delims=" %%G in (text2.txt) do (
set "line=%%G"
rem echo line: !line!
for /f "delims=:" %%i in ("!line!") do set "line=%%~nxi"
echo line: !line!
)
pause
echo on

How to extract a word after specific character in batch program?

For example I have a text file test.txt that contains like this:
Item1 $23 Item2 $24
Item3 $25
When I run this script:
#echo off
for /f "delims=$ tokens=1*" %%A in (test.txt) do echo %%B >> result.txt
The result is:
23 Item2 $24
25
I want the result is:
23
24
25
How should I repair the script?
Try the following script (see the explanatory rem remarks in the code):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_IFILE=test.txt"
set "_OFILE=result.txt"
rem // Store line-break in variable:
(set ^"LF=^
%= blank line =%
^")
rem // Write to resulting file:
> "%_OFILE%" (
rem // Loop through lines of text file:
for /F "usebackq delims=" %%L in ("%_IFILE%") do (
rem // Store current line in variable:
set "LINE=%%L"
rem // Toggle delayed expansion to be able to write and read variable in same code block:
setlocal EnableDelayedExpansion
rem // Replace each `$` by line-break and loop over resulting lines skipping the first one:
for /F "skip=1" %%K in (^"!LINE:$^=^%LF%%LF%!^") do (
rem // Return first token of each line:
echo(%%K
)
endlocal
)
)
endlocal
exit /B
At first, this replaces every $ by a line-break, so the first line of test.txt becomes:
Item1
23 Item2
24
And the second one becomes:
Item3
25
Then it extracts the first SPACE- or TAB-separated token from each of the newly formed multi-line strings, skipping the first line, so only the numbers are returned.
I have decided to post this example batch-file to show a method of parsing each line and returning every other string, (doesn't work with poison characters).
#(For /F "UseBackQDelims=" %%A In ("test.txt")Do #(Set "i=0"
For %%B In (%%A)Do #(Set /A "i=(i+1)%%2"&Cmd /V:On /Q /C Exit !i!
If Not ErrorLevel 1 Echo %%B)))>"results.txt"
In your posted example code and 'wanted' result, you could probably use that idea to output the strings with their first character removed if it is a $.
#(For /F "UseBackQDelims=" %%A In ("test.txt")Do #(Set "i=0"
For %%B In (%%A)Do #(Set /A "i=(i+1)%%2"&SetLocal EnableDelayedExpansion
If !i! Equ 0 (Set "Val=%%B"&If "!Val:~,1!"=="$" (Echo !Val:~1!
)Else Echo %%B)&EndLocal)))>"results.txt"
This should do exactly what you want.
#echo off
setlocal enabledelayedexpansion
(for /f "usebackq tokens=* delims= " %%i in ("test.txt") do (
set "_br=%%i"
echo(!_br: =^
!
)
)>_tmp
(for /f "delims=$ tokens=*" %%a in ('type _tmp ^| find "$"') do echo %%a)>result.txt
Note that you must copy the script as is. The extract of the script as show below does the replacement of whitespace with new line and therefore cannot change at all, the position and the extra line must remain as is for this to work as intended.
echo(!_br: =^
!
This script just splits each word separated by a space and redirects it to a new file, we then specifically search for the words containing $ and redirect those hits only.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%A in (test.txt) do (
for %%T in (%%A) do (
set "term=%%T"
if "!term:~0,1!"=="$" echo !term:~1!
)
)>> result.txt
endlocal
Read each line to %%A. Tokenise to %%T. Assign each token in turn to term, examine the first character and if $, echo the term, except the first character.

Batch Files: How to concatenate each line in a text file?

I have a text file with text on each line. I would like to be able to put each line in one long line along with a space. So, if the text file has:
Bob
Jack
Sam
I want the result to be
Bob Jack Sam
Below are two methods that I am working on but I am stuck. Anything in brackets [] means that I know the syntax is completely wrong; I only put it there to show my thought process. The commented sections are just me experimenting and I have left them in case anyone wants to comment on what they would do / why they do what they do.
Method 1:
#echo off
SETLOCAL EnableDelayedExpansion
SET count=1
FOR /F "tokens=* delims= usebackq" %%x IN ("afile.txt") DO (
SET var!count!=%%x
for %%a in (!count!) do (
!var%%a! = !var%%a! & " "
echo !var%%a!
)
SET /a count=!count!+1
echo !count!
)
::echo !var1! !var2! !var3!
start "" firefox.exe !var%%a!-1
ENDLOCAL
::echo "endlocal" %var1% %var2% %var3%
Method 2:
#echo off
SETLOCAL EnableDelayedExpansion
SET count=1
FOR /F "tokens=* delims= usebackq" %%x IN ("afile.txt") DO (
SET var!count!=%%x
call echo %%var!count!%%
SET /a count=!count!+1
echo !count!
)
::echo !var1! !var2! !var3!
start "" firefox.exe ^
[i = 1]
[for i to !count! do (]
call echo %%var!count!%% & " " & " "^
ENDLOCAL
::echo "endlocal" %var1% %var2% %var3%
If you only have three values, you can directly retrieve them from the file
< input.txt (
set /p "line1="
set /p "line2="
set /p "line3="
)
set "var=%line1% %line2% %line3%"
echo("%var%"
If you don't know the number of values, use a for /f command to read the lines and concatenate the contents into a variable.
#echo off
setlocal enableextensions enabledelayedexpansion
set "var="
for /f "usebackq delims=" %%a in ("input.txt") do set "var=!var!%%a "
echo("%var%"
But if the data can contain exclamation signs, the delayed expansion state will remove them (and the text surounded by them). To avoid it, this can be used
#echo off
setlocal enableextensions disabledelayedexpansion
set "var="
for /f "usebackq delims=" %%a in ("input.txt") do (
setlocal enabledelayedexpansion
for /f "tokens=* delims=¬" %%b in ("¬!var!") do endlocal & set "var=%%b%%a "
)
echo("%var%"
where the setlocal/endlocal and the inner for are used to avoid problems with ! character
Or you can try something like this
#echo off
setlocal enableextensions disabledelayedexpansion
for /f "delims=" %%a in ('
"<nul cmd /q /c "for /f "usebackq delims=" %%z in ("input.txt"^) do (set /p ".=%%z "^)""
') do set "var=%%a"
echo("%var%"
It runs a cmd instance to output the input lines as only one output line (<nul set /p is used to ouput the data without line feeds, so all data is in the same line). This is wrapped in a for to retrieve the output inside a variable
Would this work for you?
#echo off
SET var=
SETLOCAL EnableDelayedExpansion
FOR /f %%i in (afile.txt) DO (
SET var=!var!%%i
)
echo !var!
ENDLOCAL
Notice there is a space after the SET var=!var!%%i[Space Here] to separate each word in each row
EDIT:
If you want to display the current value in the loop just print %%i with NO concatenation. After the FOR loop finishes it will print the last value assigned to the variable
#echo off
SET var=
SETLOCAL EnableDelayedExpansion
for /f %%i in (input.txt) do (
SET var=%%i
echo !var!
)
echo %var%
Will print:
Bob
Jack
Sam
Sam
Try this: Post Edited.
#Echo off
Set "File=Recent_Log.txt"
Set "Output=My_Log.txt"
(
For /F %%F in (%File%) Do (
Set /p ".=%%~F "
)
) > "%Output%" < Nul
Goto :Eof
Because this is the first link after a Google search, I post my solution(Windows 7/10 .bat):
for %%f in (h:\blaba\*.*) do (type "%%f" & echo.) >> h:\sfsdf\dd.txt
NOTE: When your directory/filename contains spaces use double quotes.

SubString replacement in batch files batch script

Hi i am trying to replace the contents of a text file using batchscript.
setlocal enableextensions enabledelayedexpansion
set line=0
set position=0
set replacetoken=POSITION
set newfile = new.txt
for /f "tokens=* delims=" %%x in (resproprty.txt) do (
#echo off
set /a line+=1
if not !line!==1 (
set /a position+=1
)
set newcount=0
set newcount=!position!
set content=%%x
echo !content!
set content=!content:%replacetoken%=POSITION#!position!!
echo !newcount!
echo !position!
#echo on
echo !content! >> NEW.TXT
)
set test=Hello!
echo %test%
endlocal
here i am trying to replace my token POSITION with POSITION#1 i.e POSITION#number which is being incremented. however its not working !position! is not giving any output, i even tried %position% but that just gives output as 0. please help
Try like this :
#echo off
if exist output.txt del output.txt
setlocal EnableDelayedExpansion
set /a $count=1
for /f "delims=" %%a in ('type "resproprty.txt"') do (
set line=%%a
call set line=!line:POSITION=POSITION#%%$count%%!
echo !line!>>output.txt
set /a $count+=1
)
Echo ok
That will create the file output.txt with the change done
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /f "tokens=1*delims=:" %%a IN ('findstr /n /r "." q22988413.txt') DO (
SET "line=%%b"
SET "line=!line:position=position#%%a!"
ECHO(!line!
)
)>newfile.txt
GOTO :EOF
I used a file named q22988413.txt containing
Hello position one
Hello position two
Hello position three
Hello position four
Hello position five
which produced this output in newfile.txt
Hello position#1 one
Hello position#2 two
Hello position#3 three
Hello position#4 four
Hello position#5 five

Resources