How do I apply this? - batch-file

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
...

Related

How to get list from a delayed variable in for loop in bat file?

This is my code:
for /f "usebackq tokens=1-3 delims=," %%a in ("%my_path%") do (
set z=%%a
for %%A in (!z:.= !) do (
echo %%A
)
)
In the second loop I need to get list from !z:.= !. But it doesn't work. However, if I do this way:
for /f "usebackq tokens=1-3 delims=," %%a in ("%my_path%") do (
set z=%%a
for %%A in ("!z:.= !") do (
echo %%A
)
)
Then it works, but I have two extra " in output which I don't need. Could anyone say how to get list from !z:.= ! in a correct way (without getting extra quotes)?
EDIT: In pseudo code in the second loop I need:
string[] splits = z.split(".");
for (var split : splits) {
echo split
}

how get the same keyword in file

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"

Removing carriage returns from a text file using a batch file based on a value

I have a text file that I would like to edit and therefore would like to remove the last line. I have the following code for this:
for /f "delims=" %%a in (input.txt) do (
echo/|set /p ="%%a%"
)>>output.txt
input:
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
output:
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
Now I would like to edit the data in groups for example by the first value, so that I have the following output:
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
If I replace the FOR /F "delims=" %%a in (input.txt) do … loop with an equivalent FOR %%a in … loop:
#ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
set "_gruppeName="
(
for %%a in (
"GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
"GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;"
) do (
for /f "tokens=1 delims=;" %%A in ("%%~a") do (
if /I NOT "%%~A"=="!_gruppeName!" (
if defined _gruppeName echo(
set "_gruppeName=%%~A"
)
)
echo/|set /p ="%%~a"
)
echo(
)>>output.txt
REM debugging output follows
type output.txt
Output:
1st run: 2>NUL del output.txt & D:\bat\CR\61816520.bat
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
Next run: D:\bat\CR\61816520.bat
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEA;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEB;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;GRUPPEC;123;12345;sdfdsfds;sdfdsfsdfs;sdfsdfafsf;
Your question is not clear
based on ... the first value (GRUPPEA)
is it SORTed? or just write duplicates on the same line?
#echo off
SETLOCAL EnableExtensions EnableDelayedExpansion
::VARIABLES
set "in=input.txt" IN file
set "out=output.txt" OUT file
set/a"#lines=0"
set "gruppe="
set "prevContent="
::Count lines
FOR /F %%L in ('
"findstr /N "^^" "%in%" %= do not skip empty lines =%"
') do set/a"#lines+=1" %= Get the # of lines =%
::Read IN via SET /P #LINES number of times
::Bangs (!) will be lost
<"%in%" >"%out%" (FOR /L %%# in (1 1 %#lines%) do (
set "data=" ::clear DATA
set/p"data=" ::read from IN
FOR /F tokens^=1^ delims^=^;^ eol^= %%T in ("!data!") do set "gruppe=%%T"
if NOT "!prevContent!" == "!gruppe!" (
set "prevContent=!gruppe!"
echo(
)
<nul set/p"=!data!" ::does not work with leading space, tabs, or equal signs
)) %= read file by lines via SET /P =%
exit /b
The script counts the number of lines using FINDSTR /N and a FOR /F loop to count the # of lines, which is required to execute SET /P that many times.
Tips:
Use ECHO( instead of the problematic ECHO.
Using a pipe | is very slow, as described by #jeb here. Use <nul instead

Search filename with variable and replace different variable text in another file with multiple lines

I had an answer to my question previously, but now I've realised that i need a bit more help. I have code that does almost everything I want it to:
#echo off
copy 2.txt 2.txt-backup
setlocal enableDelayedExpansion
>2.txt (
for /f "delims=" %%A in (2.txt-backup) do (
set "ln=%%A"
if "!ln:~0,6!" == "Class=" findstr /c:"ClassName=!ln:~6!" 1.txt >null && set "ln=Class=ShipDummy"
echo !ln!
)
)
Please refer to this:
Batch - I would like to search with a variable in one file and replace that variable result in another file
But now I've realized I need 1.txt to be a file name with a variable in its name, a series of files, with no exact limit like:
Log_0.txt
Log_1.txt
Log_2.txt
Log_3.txt
and so on....
There may be just Log_0.txt, or there may be Log_0.txt through Log_23.txt, or even more...
Also, I'd like to add another line to the replacement text of Class=ShipDummy to be:
Class-ShipDummy
Type=206
And that would replace a line of text just below Class=ShipDummy replacement text with Type=206.\
Does this do what you want?
#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!" Log_*.txt >null && (
echo Class=ShipDummy
set "ln=Type=206"
)
)
if #!ln!==# (echo;) else echo !ln!
)
)
)

Grab variable wihin a line of text that contains constants and other variables

I would like to use as the variable for my code below, instead of what came after ClassName=in 1.txt, I would like what came in-between this:
EntryText=Ship sunk!|Grid AO 77| Grab variable HERE, (notice the comma at the end)
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 a word or a different two pair of letters and numbers, or any mixture of letters and numbers up until the |
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.
#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!
)
)
)
for /f "tokens=3 delims=|" %%C in ("%%B") do for /f "tokens=1 delims=," %%D in ("%%C") do echo %%D

Resources