Insert a string in a fixed position in a text file - windows

I need to insert 4 strings in a text file in a fixed position:
first string: the product, at the beginning of the line, column 1
second one: %%b, column 20
third one: %%c, column 33
fourth one: doesn't exist, column 42.
I always want the strings written at the exact same position no matter how longer are the other strings before. so it should looks like the example below:
the product ergerzgtrg 65ggrth784rjhnjgbkljn doesn't exist
the product reggbrtbhtrergzthrjhlyoiul rtjntjrez doesn't exist
the product zef rt doesn't exist
Here's my code:
FOR /F "delims=; tokens=1-7*" %%a in (mytextfile.txt) do (
if "%%e"=="Unkown" (
echo the product %%b %%c doesn't exist>>Unkown_product.txt
)
)
mytextfile.txt looks like this:
K5134908-Blabla_4;K5134908;Blabla_4;01-69423;Unkown;K5134908-Blabla_4-516245;K5134908-Blabla_4-516245;
K2602207-Blabla_2;K2602207;Blabla_2;01-81111;Unkown;K2602207-Blabla_2-516245;K2602207-Blabla_2-516245;
K2602006-Blabla_3;K2602006;Blabla_3;01-82789;Unkown;K2602006-Blabla_3-516245;K2602006-Blabla_3-516245;
K2601507-Blabla_4;K2601507;Blabla_4;01-75135;Unkown;K2601507-Blabla_4-516245;K2601507-Blabla_4-516245;
Is there any way to do that within a batch file (.bat)?

Add enough spaces to the values (20 in my example), then cut the first [whatever you need] characters (15 in my example):
#echo off
setlocal
FOR /F "delims=; tokens=1-7*" %%a in (mytextfile.txt) do (
if "%%e"=="Unkown" call :format "%%b" "%%c"
)
goto :eof
:format
set "b=%~1 "
set "c=%~2 "
set "b=%b:~0,15%"
set "c=%c:~0,15%"
echo the product %b% %c% doesn't exist>>Unkown_product.txt
Note: the search string is "Unkown" according to your file example (probably a typo, I guess it should be "Unknown")

Related

Windows Batch - Find word in one string matching word in another string and capture output

While this may seem easy to some, I've struggled for hours on it.
I have a file:
MYFOLDER,JobE,JobD_ENDED_OK,
MYFOLDER,JobD,JobC_ENDED_OK,JobD_ENDED_OK
MYFOLDER,JobD,JobB_ENDED_OK,
MYFOLDER,JobC,JobA_ENDED_OK,JobC_ENDED_OK
MYFOLDER,JobB,JobA_ENDED_OK,JobB_ENDED_OK
MYFOLDER,JobA,,JobA_ENDED_OK
I need to loop through and find where token 4 in one line matches token 3 in another line and then echo a statement to a file. I am looking for an output file that shows this:
MYFOLDER_JobA_MYFOLDER_JobB matches JobA_ENDED_OK
MYFOLDER_JobA_MYFOLDER_JobC matches JobA_ENDED_OK
MYFOLDER_JobB_MYFOLDER_JobD matches JobB_ENDED_OK
MYFOLDER_JobC_MYFOLDER_JobD matches JobC_ENDED_OK
MYFOLDER_JobD_MYFOLDER_JobE matches JobD_ENDED_OK
I know it's a FOR loop with a DO, I am just not getting the rest of it.
Any assistance is greatly appreciated.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q46510665.txt"
SET "outfile=%destdir%\outfile.txt"
(
FOR /f "usebackqdelims=" %%h IN ("%filename1%") DO (
SET "col4line=%%h"
SET "col4line=!col4line:,=|,|!"
FOR /f "tokens=1-4delims=," %%a IN ("!col4line!") DO IF "%%d" neq "|" (
FOR /f "usebackqdelims=" %%H IN ("%filename1%") DO (
SET "col3line=%%H"
SET "col3line=!col3line:,=|,|!"
FOR /f "tokens=1-4delims=," %%A IN ("!col3line!") DO (
IF "%%d|"=="%%C" (
SET "reportline=%%a_%%b_%%A_%%B matches %%C"
ECHO !reportline:^|=!
)
)
)
)
)
)>"%outfile%"
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
I used a file named q46510665.txt containing your data for my testing.
Produces the file defined as %outfile%
For each line in the file, set a variable col4line to the entire line via %%h, then replace each , with |,| so that successive , will be separated. Tokenise on , and ignore any line which has simply | as its 4th token (ie last-column-empty).
Repeat the process for every line in the file this time through %%H into col3line (note case differential to use different actual metavariables) and if the third column matches the fourth column+| from the outer loop, assemble the report line from the tokens and output, removing the |s.

How to get every 3rd line of text file and put it on a separate text file using batch script

I need to get only the every 3rd line of a text file(Movie.txt) and put it on a separate text file(Title.txt) using batch script(for /f)
Movie.txt
Movie
Title
Anime1
Movie
Title
Anime2
Movie
Title
Anime3
Movie
Title
Anime4
Title.txt
Anime1
Anime2
Anime3
Anime4
currently I have no idea how to get the required data...
I would appreciate any help you can provide. thank you.
#echo off
setlocal enabledelayedexpansion
set input=yourinput.txt
set output=youroutput.txt
set i=0
for /f "tokens=*" %%l in (%input%) do (
set /a i=!i!+1
set /a r=!i!%%3
if !r!==0 echo %%l>>%output%
)
We can use the modulo operator and a line counter. !i! contains the line number and !r! is the line number modulo 3. So if !r!==0 we write the line (%%l) into the output file.
#ECHO OFF
SETLOCAL
SET "line1="
SET "line2="
FOR /f "tokens=1*delims=" %%a IN (
'findstr /r "." "100lines.txt"'
) DO (
IF DEFINED line1 (
IF DEFINED line2 (
ECHO %%a
SET "line1="
SET "line2="
) ELSE SET line2=y
) ELSE SET line1=y
)
GOTO :EOF
All you need is to replace the input filename - I have a file named 100lines.txt that contains 100 different lines of text.
Since a variable's current defined status acts on the run-time value of the variable, ensure that the two flags are clear to start, then for each line of the file,
if line1 is not defined, set it
otherwise, if line2 is not defined, set it
otherwise we are on a *3 line, so regurgitate it and clear the two flags.

Batch for insert/replace in a txt file in a fixed position

I need to replace, using a Batch file (.bat), a blank character, placed always in a fixed position (column 13) of each line in a .txt file, with another fixed character.
What kind of function can I use?
Example of my file:
1000588141025 00LEOTOURING SRL VIA FILADELFO CASTRO,1 LENTINI
I'd like to have:
1000588141025A00LEOTOURING SRL VIA FILADELFO CASTRO,1 LENTINI
#ECHO OFF
:: version 1 - "replace regardless"
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /f "delims=" %%a IN (q26733113.txt) DO (
SET "line=%%a"
ECHO(!line:~0,13!A!line:~14!
)
)>newfile.txt
:: version2 - "replace only if space found in position"
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /f "delims=" %%a IN (q26733113.txt) DO (
SET "line=%%a"
IF "!line:~13,1!"==" " (ECHO(!line:~0,13!A!line:~14!) ELSE (ECHO(%%a)
)
)>newfile2.txt
GOTO :EOF
I used a file named q26733113.txt containing your data for my testing.
Produces newfile.txt and newfile2.txt - the first changing column - well 14 in normal counting, as indicated in your test data - regardless of the content of column 14. The second only replaces column 14 with the A if it was a space, otherwise leaves it as-is.
Don't worry about the seeming-unbalanced parentheses. echo( has special properties - te parenthesis isn't counted...
Using REPL.BAT:
type "file.txt"|repl "^(.{12}) " "$1A" >"file.txt.new"
move /y "file.txt.new" "file.txt" >nul

Batch Scripting:search file, extract numbers, and copy into new file

I'm new to batch scripting and have a question. I was wondering if it's possible to search a .txt file by requirements and take data specified and copy into a new .txt file?
Like if I have 50 lines with 9 digit numbers and a bunch of other crap I don't need after them can I say,
"For any line beginning with a 1,2,3,4,5,6,7,8,or 9...take the first 9 digits and copy them into a new file, for all lines in the file???"
I thought this would be easier than trying to delete all the other stuff. Let me know if you know anything about how to do this! Thanks.
Here's an example of what one line looks like:
123456789#example
and I just need to extract the 9 digit numbers from about 50 lines of this.
You can use FINDSTR to filter out all lines that do not start with 9 digits. Then FOR /F can read the result, line by line. A variable is set, and a substring operation preserves just the first 9 digits.
#echo off
setlocal enableDelayedExpansion
(
for /f %%A in (
'findstr "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" yourFile.txt'
) do (
set "ln=%%A"
echo !ln:~0,9!
)
)>newFile.txt
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /f "delims=" %%a IN (q24824079.txt) DO (
SET "line=%%a"
REM set DIGITS to the first 9 characters of LINE
SET "digits=9!line:~0,9!"
FOR /L %%z IN (0,1,9) DO SET "digits=!digits:%%z=!"
IF NOT DEFINED digits ECHO(!line:~0,9!
)
)>newfile.txt
GOTO :EOF
I used a file named q24824079.txt containing data for my testing.
Produces newfile.txt
You did not specfy what to do if the line was all-digits but has fewer than 9 characters. I chose to report that line.
Hopefully this helps getting the job done:
#echo off
setlocal enabledelayedexpansion
for /f %%e in (emails.txt) do (
echo Email: %%e
for /f "delims=# tokens=1" %%b in ("%%e") do (
set BEGIN=%%b
echo Name: !BEGIN!
set FIRST=!BEGIN:~0,1!
echo First char: !FIRST!
set /a NUMERIC=!FIRST!+0
echo Converted to number: !NUMERIC!
if !FIRST!==!NUMERIC! echo Yippieh!
echo.
)
)
Instead of echo Yippieh! append the email (%%e) to a file, e.g. like
echo %%e >> output.txt

Windows Batch Script parsing multiline file with variable line formats?

I need to parse a file that has the following format:
A + tags/RWSTestConsole_tag1/
(from trunk/RWSTestConsole/:r776)
So I'm using a FOR /F loop with a counter and inspecting the tokens based on whether I'm looking at line 1 or line 2. Everything works fine except the first token for line 2 includes the leading spaces (" from") and a) I thought the delims on my FOR would take care of spaces and b) theoretically I could just compare to a constant string that is set to " from" but that's kind of hokey.
This is my FOR command:
for /F "tokens=1,2,3,4,5 delims=():/ " %%a in (svn.txt) DO (
Is there a change I can make to the FOR command to ignore the spaces? If not is there a way to trim the token inside the FOR loop's DO clause so I only get the word without the leading spaces?
Edit:
This is output from a Subversion SVNLOOK command in a script that is managing whether or not to allow a "tag" to be created. The output can be 1 line that must be formatted as:
D /tags/tagfoldername/
If it's one line but it's not a delete for the actual tag folder then it's an error. This case is handled.
If it's more than 2 lines it's a list of files and that's an error. I have that handled.
The case I'm having problems with is if it is 2 lines it needs to be in the format shown above:
A + tags/RWSTestConsole_tag1/
(from trunk/RWSTestConsole/:r776)
Where col 1 = "A". col 3 = "+", col 5 = "tags" and the remainder of line one is the tag folder name. The second line is the source of the create request so it has to start with "from", followed by "trunk", "branches" or "tags" followed by a single-level folder name and a revision number.
I used the FOR command as described above. In the DO clause I look at a counter to tell if I'm parsing line 1 or line 2. Line 1 is simple, I have all the logic to handle it.
Line 2 is parsed by the same FOR command and the first token (%%a) removes the "{" from delims, but leaves behind all the leading blanks so I get back %%a=" from".
I need to know if there's a way I can modify the FOR command to remove the blanks or a way to trim %%a within the FOR DO clause to remove the blanks.
Edit 2: FOR Loop Code
set c=1
for /F "tokens=1,2,3,4,5 delims=():/ " %%a in (svn.txt) DO (
echo op=%%a, %%b, %%c, %%d, %%e
if !c!==1 (
set rc1=0
if /I %%a EQU A (
if "%%b" EQU "+" (
if [%%e] EQU [] (
echo Tag from a Copy Op
set rc1=0
) else (
echo Found a subfolder: %%e - not a tag delete
set rc1=1
)
) else (
echo Tag not a Copy
set rc1=1
)
)
)
if !c!==2 (
set rc2=0
set str1=%%a
echo String replace 1 = !str!
set str2=!str1:~-4!
echo String Replace 2 *!str2!*
if /I !str2! EQU FROM (
set isvalid=false
if %%b EQU trunk set isvalid=true
if %%b EQU branches set isvalid=true
if %%b EQU tags set isvalid=true
if !isvalid! EQU true (
set rc2=0
) else (
set rc2=1
echo Invalid source for Tag Creation
)
) else (
set rc2=1
echo Tag not FROM
)
)
set /a c+=1
)
echo RC1=!rc1!
echo RC2=!rc2!
set /a rc=!rc1!+!rc2!
echo final RC = !rc!

Resources