I am new to batch script, and I am trying to parse a file that has key value (kind) of pairs delimited by new line. For example:
the value of abc
1234
the value of def
5678
the value of ghi
9876
I would like to read this file using new line as delimited so that I can access the values.
Something like
for /f "tokens=1,2,3 delims='\n'" %%i in ('findstr /C:the value of abc 'filepath') do echo %%j
I just put it here as an option:
for /F delims^=^ eol^= %%i in ('findstr /C:the value of abc 'filepath') do echo %%i
You can't set 'new line' as delimiter. Try this:
#echo off &setlocal
for /f "tokens=1*delims=:" %%i in ('findstr /NC:"the value of abc" "filepath"') do set /a count=%%i
if defined count for /f "skip=%count%tokens=1*delims=:" %%i in ('findstr /N "^" "filepath"') do if not defined value set "value=%%j"
echo.%value%
endlocal
#ECHO OFF
SETLOCAL
SET "prevline="
FOR /f "delims=" %%z IN (tvabc.txt) DO (
IF DEFINED prevline (
FOR /f "tokens=1-6" %%a IN ('echo %%prevline%% %%z') DO (
ECHO %%a %%b %%c %%d %%e %%f
)
SET "prevline="
) ELSE (SET prevline=%%z)
)
should do the job, if you require each token from two successive lines to be output. Would be much easier if we had a listing of the required output rather than having to guess from code that doesn't work.
Essentially, this code saves the contents of one line then detects that the second has been read by the fact that the variable holding the previous line is defined. Under those circumstances, for/f is used to tokenise the concatenation of the two lines.
Not sure what you're going to do with those Name/Value pairs after you get them, but if you're going to do it in Powershell, do it right and start by creating objects:
$file = 'c:\somedir\somefile.txt'
[regex]$regex = #'
(?ms)the value of (\S+)
(\d+)
'#
#Choose one
$text = Get-Content $file -Raw # V3 Only
$text = [IO.File]::ReadAllText($file) # V2 or V3
$regex.matches($text) |
foreach {
New-object PSObject -Property #{
Name = $_.groups[1].value
Value = $_.groups[2].value
}
}|
Select Name,Value |
Format-Table -AutoSize
Name Value
---- -----
abc 1234
def 5678
ghi 9876
What do you think of that? In powershell:
(get-content .\essai.txt)[(Select-String .\essai.txt -Pattern "the value of abc").LineNumber]
It displays the next line after the pattern.
I normally use the given batch command, to read each line in file
FOR /F "delims=" %%G IN (test.txt) DO (
ECHO %%G
)
Here, "delim=" does the trick.
Related
I have multiple text files contains integer on each row. Can loop for /f calculate (add) from each row of each file sequentially?
Lets say the text files are like this:
File1.txt
123
213
321
File2.txt
111
222
333
File3.txt
333
222
111
Is it possible iterate over multiple files content and sum the like rows IE:
file1 row1 + file2 row1 + file3 row1
file1 row2 + file2 row2 + file3 row2
file1 row3 + file2 row3 + file3 row3
:: operation for 1st row of each file should be:
Set /A calc=123+111+333
echo !calc!
After googling around, I could not find any solution similar to my problem.
Appreciate if anyone can provide insight on this.
Thanks
you can use a single for loop over the output of findstr to build counts from each lines value.
#Echo off & CD /D "%~dp0"
cls
Setlocal
For /f "tokens=1 Delims==" %%g in ('set Line[ 2^> nul')Do Set "%%g=" 2> nul
For /f "tokens=2,3 delims=:" %%i in ('%Systemroot%\System32\Findstr.exe /n /r "^[0123456789]*$" "file*.txt"')Do (
Set /A "Line[%%i]+=%%j+0" 2> nul
)
Set Line[
Endlocal
You can use an array this way:
#echo off
setlocal EnableDelayedExpansion
for %%f in (file*.txt) do (
set "i=1"
for /F %%n in (%%f) do set /A "calc[!i!]+=%%n, i+=1"
)
set /A i-=1
for /L %%i in (1,1,%i%) do echo calc[%%i] = !calc[%%i]!
This batch-file that is run by cmd uses a PowerShell script. It creates a hash item for each line and accumulates the sum for each.
#powershell.exe -NoLogo -NoProfile -Command ^
"$h = #{};" ^
"Get-ChildItem -Filter 'filefile*.txt' |" ^
"ForEach-Object {" ^
"$LineNumber = 0;" ^
"Get-Content -Path $_.FullName |" ^
"ForEach-Object { $h[$LineNumber++] += [int]$_ }" ^
"};" ^
"0..($h.Count-1) | ForEach-Object { $h[$_] }"
This is a lot easier and clearer if written in a .ps1 file.
$h = #{}
Get-ChildItem -Filter 'filefile*.txt' |
ForEach-Object {
$LineNumber = 0
Get-Content -Path $_.FullName |
ForEach-Object { $h[$LineNumber++] += [int]$_ }
}
0..($h.Count-1) | ForEach-Object { $h[$_] }
You may see this get downvoted by someone who thinks PowerShell is not part of cmd. PowerShell is just as much a part of cmd as are find.exe, ipconfig.exe, and setx.exe. PowerShell is available on all supported Windows systems.
You can use findstr in a single for loop and set results sequentially per file:
#echo off & setlocal enabledelayedexpansion
for /f "tokens=1*delims=:" %%i in ('findstr /R "[0-9]" file*.txt') do (
set /a %%~ni+=1 & set /a _Result[!%%~ni!]+=%%j
)
set _Result
Each line per file's lines will be added together, result (based of your current examples:
For a drop single sum files on me approach:-
Summation.bat
#echo off & SETLOCAL ENABLEDELAYEDEXPANSION & Title Summation
for /f "tokens=1* delims=[]" %%A in ('find /n /v "%Title%" "%~1"') do (set "L%%A=%%B" & set "count=%%A")
set calc=0 & for /L %%i in (1,1,!count!) do (set /a calc = !calc! + !L%%i!)
echo/ & echo Line Count = !count! ^& Sum = !calc! & echo/ & pause
Command Line Usage >Summation File1.txt
NOTE as per comment by #T3RROR below
I misread your question as "Provide summation for a file at a time."
HOWEVER have kept it here for others wishing to sum each file.
Good batching, and may the cmd be with you :-)
I have a txt file which contains some animal and fruit names. * contain fruits and # contain animal. Just like below:-
* apple
# cat
* banana
# dog
* mango
# lion
* graps
I want to sort down list and write like a table which given below:-
* apple # cat
* banana # dog
* mango # lion
I'm using a for loop for this:
for /f "useback eol=* tokens=1-5 delims= " %%a in ("my text.txt") do (
for /f "useback eol=# tokens=1-5 delims= " %%c in ("my text.txt") do (
Echo %%a %%b
))
But it echoes duplicates.
Please note that tokens are parts of a single line. For the first line, Token1=*, token2=apple, token3 to 5 are not defined. Same for each other line. For your example, tokeins=1,2 would be fine, but I decided to use 1,* to take account of animals or fruits whose names are two (or more) words (like dragon fly)
for /f "usebackq tokens=1,*" %%a in ("my text.txt") do (
if "%%a" == "*" (
<nul set /p "=%%a %%b "
) else (
echo %%a %%b
)
)
echo\
This uses a special trick to write a string without a linefeed in case the line starts with *
(<nul set /p "=string")
Of course, this depends on alternating * and # lines in the source textfile.
This can be done in cmd or a batch-file using the following. If you are on a supported Windows system, PowerShell is available.
This code get the entire file contents into memory, the splits it into an array on the * character. The first, empty, array element is skipped. Then, the NewLine between the * and # lines is replaced with a few SPACE characters.
powershell.exe -NoLogo -NoProfile -Command ^
"(Get-Content -Path '.\fruit-animal.txt' -Raw) -split '\*' | Select-Object -Skip 1 | %{ '* ' + $_ -replace [Environment]::NewLine, ' '}"
Here is the data file and example execution.
C:>TYPE .\fruit-animal.txt
* apple
# cat
* banana
# dog
* mango
# lion
* graps
C:>powershell.exe -NoLogo -NoProfile -Command ^
More? "(Get-Content -Path '.\fruit-animal.txt' -Raw) -split '\*' | Select-Object -Skip 1 | %{ $_ -replace [Environment]::NewLine, ' '}"
apple # cat
banana # dog
mango # lion
graps
This will likely work well for you unless you have a -large- file.
If PowerShell Core http://github.com/PowerShell/Powershell is used (recommended), change it to:
pwsh.exe -NoLogo -NoProfile -Command ^
"(Get-Content -Path '.\fruit-animal.txt' -Raw) -split '\*' | Select-Object -Skip 1 | %{ '* ' + $_ -replace [Environment]::NewLine, ' '}"
The task description, the example input and output data and the posted code do not match at all. So it is really difficult to understand what the task really is.
The first batch file code below is for reading all non-empty lines from a data file and merge each non-empty odd line with each non-empty even line together and finally replace the source data file with the temporary file containing the merged lines.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "DataFile=Data.txt"
if not exist "%DataFile%" goto MissingFile
set "TempFile=%DataFile%.tmp"
set "LineOdd="
set "LineEven="
(
for /F usebackq^ delims^=^ eol^= %%I in ("%DataFile%") do (
if defined LineOdd (
set "LineEven=%%I"
setlocal EnableDelayedExpansion
echo(!LineOdd! !LineEven!
endlocal
set "LineOdd="
set "LineEven="
) else set "LineOdd=%%I"
)
if defined LineOdd (
setlocal EnableDelayedExpansion
echo(!LineOdd!
endlocal
set "LineOdd="
)
)>"%TempFile%"
if /I not "%~1" == "test" (move /Y "%TempFile%" "%DataFile%") else type %TempFile%"
del "%TempFile%"
exit /B
:MissingFile
echo ERROR: There is no file "%DataFile%" in "%CD%".
echo/
pause
endlocal
It is possible to run this batch file with the string test as parameter to just output the final result instead of overwriting the source data file.
The file Data.txt contains perhaps the following lines:
* apple
# cat
# dog
* banana
* mango
# lion
* grapes
* pear
* plum
# mouse
# canary
# hamster
* cherry
The result of the batch file above is:
* apple # cat
# dog * banana
* mango # lion
* grapes * pear
* plum # mouse
# canary # hamster
* cherry
The second batch file code below really looks on the identifiers * and # at beginning of the lines to determine if the rest of the line should be on left or on right side in the output file. All lines not starting with * and # are ignored by this code.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "DataFile=Data.txt"
if not exist "%DataFile%" goto MissingFile
set "TempFile=%DataFile%.tmp"
set "DataLeft="
set "DataRight="
(
for /F "usebackq tokens=1*" %%I in ("%DataFile%") do (
if "%%I" == "*" (
if not defined DataLeft (
set "DataLeft=%%J"
) else if defined DataRight (
setlocal EnableDelayedExpansion
echo(!DataLeft! !DataRight!
endlocal
set "DataLeft=%%J"
set "DataRight="
) else (
setlocal EnableDelayedExpansion
echo(!DataLeft!
endlocal
set "DataLeft=%%J"
)
) else if "%%I" == "#" (
if not defined DataRight (
set "DataRight=%%J"
) else if defined DataLeft (
setlocal EnableDelayedExpansion
echo(!DataLeft! !DataRight!
endlocal
set "DataRight=%%J"
set "DataLeft="
) else (
setlocal EnableDelayedExpansion
echo( !DataRight!
endlocal
set "DataRIGHT=%%J"
)
)
)
if defined DataLeft (
if defined DataRight (
setlocal EnableDelayedExpansion
echo(!DataLeft! !DataRight!
endlocal
set "DataLeft="
set "DataRight="
) else (
setlocal EnableDelayedExpansion
echo(!DataLeft!
endlocal
set "DataLeft=%%J"
)
)
if defined DataRight (
setlocal EnableDelayedExpansion
echo( !DataRight!
endlocal
set "DataRight="
)
)>"%TempFile%"
if /I not "%~1" == "test" (move /Y "%TempFile%" "%DataFile%") else type "%TempFile%"
del "%TempFile%"
exit /B
:MissingFile
echo ERROR: There is no file "%DataFile%" in "%CD%".
echo/
pause
endlocal
This batch file can be also executed with argument test to just output the final result instead of overwriting the source data file.
The output for the same data file example above is:
apple cat
banana dog
mango lion
grapes
pear
plum mouse
canary
cherry hamster
It can be seen that on left side are the fruits marked with * and on right side the animals marked with # in the data file.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... for an explanation of %~1
del /?
echo /?
endlocal /?
exit /?
for /?
goto /?
if /?
move /?
pause /?
set /?
setlocal /?
type /?
It is hard to tell from your specifics what exactly may be the content and layout of your text file, or regarding the output, what exactly you mean by table, and whether empty cells in those tables would be acceptable.
Whilst trying to keep the idea you were using, as much as possible, and based upon the example text file you've submitted, assuming that there will not be two consecutive lines beginning with *, and using a horzontal tab, as a cell separator, line 3 Echo([TAB]%%G, here's a quick example of something which may suit your purposes:
#For /F Delims^=^ EOL^= %%G In ('%SystemRoot%\System32\findstr.exe /BIL "* #"
"S:\ome\Directory\my text.txt" 2^>NUL') Do #(
For /F "EOL=* Delims=" %%H In ("%%G") Do #Echo( %%G
For /F "EOL=# Delims=" %%I In ("%%G") Do #Set /P "=%%G" 0<NUL)
#Echo(& Pause
Example output (with the mis-spelling graps corrected):
* apple # cat
* banana # dog
* mango # lion
* grape
If you didn't want the * and # characters in your resulting output file, i.e.
apple cat
banana dog
mango lion
grape
Then the code would be modified slightly, where line 3 would have Echo([TAB]%%I:
#For /F Delims^=^ EOL^= %%G In ('%SystemRoot%\System32\findstr.exe /BIL "* #"
"S:\ome\Directory\my text.txt" 2^>NUL') Do #(
For /F "EOL=* Tokens=1,*" %%H In ("%%G") Do #Echo( %%I
For /F "EOL=# Tokens=1,*" %%J In ("%%G") Do #Set /P "=%%K" 0<NUL)
#Echo(& Pause
I have a list file
MFC_530_18MM_007_F0
MFC_520_18MM_008_F0
MFC_430_18MM_001_F1
MFC_270_18MM_002_F1
MFC_270_18MM_003_F1
MFC_720_18MM_004_F1
MFC_130_18MM_005_F1
MFC_540_18MM_006_F1
MFC_BT580_18MM_007_F1
MFC_530_18MM_008_F1
MFC_MP110_18MM_009_F1
MFC_AAC1_18MM_010_F1
I want to get the same name
ex:
MFC_530_18MM
MFC_520_18MM
MFC_430_18MM
MFC_270_18MM
MFC_270_18MM
MFC_720_18MM
MFC_130_18MM
MFC_540_18MM
MFC_BT580_18MM
MFC_530_18MM
MFC_MP110_18MM
MFC_AAC1_18MM
I have this code:
#echo off
set "R_Str_f=_0.*"
set "R_Str_fed="
setlocal enableDelayedExpansion
(
for /f "tokens=1* delims=:" %%a in ('findstr /n "^" source.txt') do (
set "line=%%b"
if defined line set "line=!line:%R_Str_f%=%R_Str_fed%!"
echo(!line!
)
)> output.txt
I want this code to remove the last character and then remove duplicate keywords but it does not work
not sure which OS you're using.
on Unix/Linux and SHELL you can try to use the "_" as a field separator (FS)
cat yourfile | awk 'FS="_" { print $1"_"$2"_"$3}'
If the requirement is to result in the first three (3) items separated by LOW LINE (underscore) characters, this might work.
#powershell -NoLogo -NoProfile -Command ^
"Get-Content -Path '.\in.txt' | ForEach-Object {($_ -split '_')[0..2] -join '_'}"
Result:
PS C:\src\t\so68080140> .\doit.bat
MFC_530_18MM
MFC_520_18MM
MFC_430_18MM
MFC_270_18MM
MFC_270_18MM
MFC_720_18MM
MFC_130_18MM
MFC_540_18MM
MFC_BT580_18MM
MFC_530_18MM
MFC_MP110_18MM
MFC_AAC1_18MM
Given that your question intent appears to be to replace everything from and including _0 with nothing, does this help?
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
( For /F UseBackQ^ Delims^=^ EOL^= %%G In ("source.txt") Do (
Set "LineContent=%%G"
SetLocal EnableDelayedExpansion
For /F UseBackQ^ EOL^=^|^ Delims^=^| %%H In (
'!LineContent:_0^=^|!'
) Do EndLocal & Echo(%%H
)
) 1>"output.txt"
For the above example code to work, I have assumed, and therefore it is a requirement, that your initial strings do not include any | characters.
If you are using Windows 10, and do not mind what order your output file list is in, then you could probably perform the entire task, i.e. with duplicates removed, like this:
#Echo Off
SetLocal EnableExtensions
(
For /F UseBackQ^^^ Delims^^^=^^^ EOL^^^= %%G In ("source.txt") Do #(
Set "LineContent=%%G"
%SystemRoot%\System32\cmd.exe /D /C "Echo(%%LineContent:_0=&:%%"
)
) | %SystemRoot%\System32\sort.exe /Unique 1>"output.txt"
I have a text file that contains lots of values on multiple lines with a different amount of spaces between values. Some spacing is 4, 6, 7, 9, etc. I have this code written but it only works for removing odd numbers of spaces (and leaving one space) which means that if I have 4 spaces between values (an even amount) I have no required space left.
Edit: max spacing is 13 and values per line are not the same.
Example text file:
123.000 345.555 # 777.4560000 1.55555 66.878444
333.444 555.4848 999.758584 | 34.3737373
This is what I want after the fix:
123.000 345.555 777.4560000 1.55555 66.878444
333.444 555.4848 999.758584 34.3737373
This is what I am getting with my code below:
123.000345.555 777.45600001.55555 66.878444
333.444555.4848 999.758584 34.3737373
How do I set spacing to one space between every value regardless of the amount of spaces? I am also removing the # and | symbols as well.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
> "conv_output_clean.txt" (
for /F "usebackq delims=" %%L in ("conv_output.txt") do (
set "LINE=%%L"
setlocal EnableDelayedExpansion
set "LINE= !LINE: =!"
set "LINE=!LINE:#=!"
set "LINE=!LINE:|=!"
echo(!LINE!
endlocal
)
)
endlocal
exit /B
Based upon your provided text content, and ignoring your | and # characters being removed, the following example should replace all concurrent space characters with a single one:
#SetLocal EnableDelayedExpansion
#(For /F UseBackQ^ Delims^=^ EOL^= %%G In ("conv_output.txt") Do #(Set "_="
For %%H In (%%G) Do #If Not Defined _ (Set "_=%%H ") Else Set "_=!_!%%H "
Echo !_!)) 1> "conv_output_clean.txt"
If you want to also remove the | and # characters, then the following modification should work with your provided content:
#SetLocal EnableDelayedExpansion
#(For /F UseBackQ^ Delims^=^ EOL^= %%G In ("conv_output.txt") Do #(
Set "_="&Set "#=%%G"&Set "#=!#:|=!"
For %%H In (!#:#^=!) Do #If Not Defined _ (Set "_=%%H ") Else Set "_=!_!%%H "
Echo !_!)) 1> "conv_output_clean.txt"
Yet another way, output redirection left for the reader to add.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
for /F "usebackq delims=" %%L in ("conv_output.txt") do (
set "line="
for %%M in (%%L) do (
if "%%M" neq "#" if "%%M" neq "|" (
if defined line set "line=!line! "
set "line=!line!%%M"
)
)
echo(!line!
)
[ EDIT ] Sample run output.
C:\etc>type conv_output.txt
123.000 345.555 # 777.4560000 1.55555 66.878444
333.444 555.4848 999.758584 | 34.3737373
C:\etc>conv_output.cmd
123.000 345.555 777.4560000 1.55555 66.878444
333.444 555.4848 999.758584 34.3737373
C:\etc>
You can wind down number of spaces step by step using a while-like loop (or until-like loop). Unfortunately, we cannot use :label inside a command block enclosed in () parentheses so call a subroutine as follows:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
>"conv_output_clean.txt" (
for /F "usebackq delims=" %%L in ("conv_output.txt") do (
set "LINE=%%L"
call :removeAddChars
setlocal EnableDelayedExpansion
echo(!LINE!
endlocal
)
)
endlocal
exit /B
:removeAddChars
set "LINE=%LINE:#=%"
set "LINE=%LINE:|=%"
:remove2Spaces
set "LINE=%LINE: = %"
set "_LINE=%LINE: = %"
if "%_LINE%"=="%LINE%" goto :eof
goto :remove2Spaces
Output:
type conv_output.txt
123.000 345.555 # 777.4560000 1.55555 66.878444
333.444 555.4848 999.758584 | 34.3737373
.\SO\63063411.bat
type conv_output_clean.txt
123.000 345.555 777.4560000 1.55555 66.878444
333.444 555.4848 999.758584 34.3737373
This will work with up to 16 spaces:
#echo off
setlocal enabledelayedexpansion
for /f "usebackq delims=" %%a in ("conv_output.txt") do (
set "LINE=%%~a"
set "LINE=!LINE:#=!"
set "LINE=!LINE:|=!"
set "LINE=!LINE: = !"
set "LINE=!LINE: = !"
set "LINE=!LINE: = !"
set "LINE=!LINE: = !"
echo !line!
)
It replaces 9 spaces with a single space, then 5, 3, and 2. If you add 17 you can (I believe) replace up to 32 spaces. Note it is imperative to remove the # and | first.
Another idea is to use call and shift:
#echo off
setlocal enabledelayedexpansion
for /f "usebackq delims=" %%a in ("conv_output.txt") do (
set "lineinput=%%~a"
set "lineinput=!lineinput:#=!"
set "lineinput=!lineinput:|=!"
call :process !lineinput!
)
goto :eof
:process
set lineoutput=%~1
shift
:loop
if "%~1"=="" echo %lineoutput%&&goto :eof
set lineoutput=%lineoutput% %~1
shift
goto :loop
I'll leave it up to you to put this into your code.
Here is one more algorithm as designed by Jeb on DosTips.com
#echo off
setlocal EnableExtensions DisableDelayedExpansion
> "conv_output_clean.txt" (
for /F "usebackq delims=" %%L in ("conv_output.txt") do (
set "str=%%L"
setlocal EnableDelayedExpansion
set "str=!str:#=!"
set "str=!str:|=!"
set ^"str=!str: =a !"
set ^"str=!str: a =!"
set ^"str=!str:a = !"
echo(!str!
endlocal
)
)
endlocal
exit /B
Here is Jeb's explanation from that post. In Jeb's example it was replacing commas. So wherever he mentions a comma, it equates to the space in this users desired output
I want to use the replacing of a search pattern with nothing to remove all but one comma.
Obviously this can't work with simply replacing ","->"".
So I create first a better pattern
"," -> "a,,"
abc,,,,cba,,,,end
will be expanded to
abca,,a,,a,,a,,cbaa,,a,,a,,a,,end
Then each ",a," will be replaced with nothing
abca,,a,,a,,a,,cbaa,,a,,a,,a,,end
->
abca,,cbaa,,end
Now, exactly one pattern remains and will be replaced with the rule "a,," -> ","
And just to show the efficiency of some of the code answers, I did a test with a 2,000 line file.
JosezF: 0 Days 0 Hours 0 Minutes And 5 Seconds
Jeb: 0 Days 0 Hours 0 Minutes And 0 Seconds
Compo: 0 Days 0 Hours 0 Minutes And 0 Seconds
The code I used from Jeb and Compo's code are quite efficient. Less than 1 second for each of them.
Here is an alternative approach that is foolproof against empty lines/ lines that contain !
#echo off
SETLOCAL DisableDelayedExpansion EnableExtensions
>"process.txt" (FOR /F "delims=" %%L in ('
""%__APPDIR__%FINDSTR.EXE" /N "^^" example.txt"
') do (
set "lnCont=%%L"
SETLOCAL EnableDelayedExpansion
set "lnCont=!lnCont:*:=!"
FOR %%C in (^| #) do if DEFINED lnCont set "lnCont=!lnCont:%%~C=!"
if DEFINED lnCont FOR /F delims^=^ eol^= %%T in (^"!lnCont:^ ^=^
%=DO NOT REMOVE ME=%
!^") do if DEFINED proc (set "proc=!proc! %%T") ELSE set "proc=%%T"
echo(!proc!
ENDLOCAL
))
The loop:
parses the output of FINDSTR /N, line by line
replace all SPACEs with linefeeds
FOR /F ignore empty lines, therefore consecutive spaces are treated as one
One more way...
Get-Content .\conv_output.txt |
ForEach-Object { ($_ -replace '\|','') -replace '\s+',' ' }
Running from a cmd.exe shell or .bat file script...
powershell -NoLogo -NoProfile -Command ^
"Get-Content .\conv_output.txt |" ^
"ForEach-Object { ($_ -replace '\|','') -replace '\s+',' ' }"
I would like to use as the variable for my code below, instead of what comes after ClassName= in 1.txt, I would like what comes in-between this:
EntryText=Ship sunk!|Grid AO 77|Variable,
(notice the comma after the variable and the | before it )
So grab after the text line ending with the second | and before the comma.
The text line before the variable will be the same and constant EXCEPT after "Grid" there could be any mixture of letters and numbers up until the second |
So I am trying to use as a variable, what is in between:
EntryText=Ship sunk!|Grid (Any combination of letters or numbers) | (variable) , (comma)
So grab in between the second | and the comma. Than you.
I would like to replace the grabbing of the variable after ClassName= to what is in between the second | and the comma.
Please keep in mind that there are other | and commas in the file that I don't want to grab, I just want to grab the variable after | and before comma if its after the "EntryText=Ship sunk!|Grid ....
Again, I don't want the Grid part, I'd like what comes after the second | and before the comma. There will be many EntryText lines as well, so I'd like to grab that part of all of them and put in my code.
So instead of what is after ClassName=, I'd like to copy what is where the variable listed above.
Thank you for your time!!
#echo off
copy 2.txt 2.txt-backup
setlocal enableDelayedExpansion
>2.txt (
for /f "tokens=1* delims=:" %%A in ('findstr /n "^" 2.txt-backup') do (
( echo !ln!| findstr "^Type=206$" >NUL && set ln=ln ) || (
set "ln=%%B"
if "!ln:~0,6!"=="Class=" (
findstr /c:"ClassName=!ln:~6!" "E:\Dropbox\New folder\Log_*.txt" >"E:\Dropbox\New folder\null" && (
echo Class=ShipDummy
set "ln=Type=206"
)
)
if #!ln!==# (echo;) else echo !ln!
)
)
)
I was given this bottom code by someone, but I don't know if its what I want or how to apply it to the above:
for /f "tokens=3 delims=|" %%C in ("%%B") do for /f "tokens=1 delims=," %%D in ("%%C") do echo %%D
Thank you!!!!!
I think you want something like this...
...
set "ln=%%B"
for /f "tokens=3 delims=|" %%C in ("%%B") do for /f "tokens=1 delims=," %%D in ("%%C") do set "MyVar=%%D"
findstr /c:"ClassName=!MyVar!" "E:\Dropbox\New folder\Log_*.txt" >"E:\Dropbox\New folder\null" && (
echo Class=ShipDummy
...