Extract the first quoted string of each line of a text file - windows

I want to read through a text file with a lot of lines.
in the beginning of each line, i have a string between quotes, then a coma and then the rest of the lines, Ex.:
"CBL003","C3/C5 // <>SdcdUB","",0,1,"PfcdDT_gerergv","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",
"CBL004","C3<.<C7 // <>SqsxUB","",0,1,"PDzesdxT_esfdczec","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,""
What I want is to read through the file, and extract the first line put it in a different text file and name it with the first strings of the line that it contains. Ex.:
In the above example, I should have the text file CBL003.txt that contains:
"CBL003","C3/C5 // <>SdcdUB","",0,1,"PfcdDT_gerergv","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",
and a second file text CBL004.txt that contains :
"CBL004","C3<.<C7 // <>SqsxUB","",0,1,"PDzesdxT_esfdczec","",0,"","",0,"","",0,"","",0,"","",0,"","",0,"","",0,""
I already have a code that read through each line :
FOR /F "tokens=*" %%a IN (C:\SourceFile.txt) DO (
ECHO %%a
)
But I don't know how to extract the first part of each line

You need tokens=1 because you want %%a to be set to only the first token, and you need delims=, to specify the comma as the delimiter. You can remove the quotes, if you want, by using %%~a. Type for /? for help.
FOR /F "tokens=1 delims=," %%a IN (C:\SourceFile.txt) DO (
ECHO %%~a
)

Related

Batch file to insert string in exact position in file txt

I have a test.txt file like this:
sylvester, stallone, 35,20, florida;
brad, pitt, 40,25, california;
sean, connery, 15,80, london;
I have to create a new one in which the surname begins with the 15th column and the name in the 30th.
I would like to do it with a batch file.
What I have managed to do is this:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=1,2 delims=," %%G IN (test.txt) DO (
SET "line=%%H"
SET "spaces= "
ECHO (!spaces!!line!!spaces!%%G
)
) >> output.txt
pause
But in this way %%G does not begin always from the same position, it depends on how many characters has %%H. And more, does not write on output.txt but it makes me see the results on the batch window.
I know it's probably a trivial question, but I'm new to programming.
#echo off
setlocal enabledelayedexpansion
del output.txt
set "spaces= " :: 15 spaces
(FOR /F "tokens=1,2 delims=, " %%G IN (test.txt) DO (
SET "line=%spaces%%%H %spaces%"
set "line=!line:~0,29!"
echo !line:~0,29!%%G
)) >output.txt
add 14 spaces in front of the surname (to let surename start at 15) and append another 15 spaces (to get a cumulated length of at least 29). Then trim it to the first 29 characters and append the name (at Pos 30).
(added a space to the delims for proper handling)

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.

Windows command line - filter lines with non-zero values

I have a file.data whose content is as below:
140919071513,10,0,1,0,0
140919071513,11,0,1,0,0
140919071513,12,67,1,0,0
140919071513,13,89,1,0,0
140919071513,14,0,1,0,0
140919071513,15,32,1,0,0
140919071513,16,0,1,0,0
140919071513,17,0,1,0,0
140919071513,18,78,1,0,0
140919071513,19,0,1,0,0
140919071513,20,34,1,0,0
I need to run a one-line command in Windows-DOS to get the below output:
(non-zero values in 3rd column)
140919071513,12,67,1,0,0
140919071513,13,89,1,0,0
140919071513,15,32,1,0,0
140919071513,18,78,1,0,0
140919071513,20,34,1,0,0
I used this command to try to get something, but couldn't get the desired result.
for /f "tokens=* delims=," %i in file.data do echo %i
Thanks
In cmd.exe you can do the following:
for /f "tokens=1-3,* delims=," %i in (tst.txt) do #if %k GTR 0 echo %i,%j,%k,%l
Almost, what you tried, but a few changes:
... do #if ..// The # will disable echo of the command itself ...
tokens=1-3,* // the first three tokens will be translated into variables
delims=, // %i, %j and %k. The rest of each line ends up in %l
%k GTR 0 // GTR="greater", is a comparison of the third column against 0
This will get you
140919071513,12,67,1,0,0
140919071513,13,89,1,0,0
140919071513,15,32,1,0,0
140919071513,18,78,1,0,0
140919071513,20,34,1,0,0
Just for the case that you have Powershell at hand:
Import-Csv .\file.data -Header "A","B","C","D","E","F"|where {$_.C -ne 0}|foreach-object {Write-host("$($_.A),$($_.B),$($_.C),$($_.D),$($_.E),$($_.F)")}
The last part with the "foreach-object {Write-Host..." might be optimized, but it works in this case.
findstr /v /r /c:"^[^,]*,[^,]*,0," file.data
This will list all the lines that does not match (/v) the regular expression (/r) : from the start of the line (^) any sequence of zero or more characters that does not contain a comma ([^,]*), followed by a comma, any sequence of zero or more characters that does not contain a comma, a comma, a zero and a comma.

How to rename file with pattern using batch script

For example I have a .txt file named pair.txt.
Example:
AAA_BBB_CCC_DDD_EEE_FFF_GGG_HHH.idoc.xml AAAA
AAA_BBB_CCC_DDD_EEE_FFF_111_222.idoc.xml BBBB
AAA_BBB_CCC_DDD_EEE_FFF_333_444.idoc.xml CCCC
Now this file contains 2 columns of filenames. First column will be the pattern to rename the second column. Now I want to use the right side of 6th and 7th "_" as pattern. The final filenames of the files in the second column must be:
AAAA.GGG_HHH
BBBB.111_222
CCCC.333_444
As you noticed, I didn't include the .idoc.xml part. Now I want to put the code within this for statement:
for /f "tokens=1,2" %%a in ('type c:\user\pair.txt') do (
echo Renaming file : %%b
)
How will I able to do this?
for /f "usebackq tokens=1,2" %%a in ("c:\user\pair.txt") do (
for /f "tokens=7,8 delims=_." %%c in ("%%a") do (
echo Renaming file : %%b = %%b.%%c_%%d
)
)
Use a second for command to split the first column and then use the adecuated tokens

Batch File Loops with delims

I am getting only 1 item: Adding - RegistrationPool for the following for loop below. What have I done wrong? How do I iterate to the next token/item?
for /F "delims=!" %%i in ("RegistrationPool"!"SubSystemService") DO (
echo Adding - %%i
echo.
)
if you use a comma or a space instead of an exclamation mark, you can simply use:
for %%i in (RegistrationPool,SubSystemService) DO (
echo testing %%i
echo.
)
To split a string you need a string to split
for /F "delims=!" %%i in ("RegistrationPool!SubSystemService") DO (
echo Adding - %%i
)
But this will still only print one "Adding" line. Why?. for /f will iterate over the files/lines, executing the content of the do body once per line. You have only one line (with two tokens, but one line) so only one iteration.
You can access the second element as a second token
for /F "tokens=1,2 delims=!" %%i in ("RegistrationPool!SubSystemService") DO (
echo Adding - %%i
echo Adding - %%j
)
First token is stored in the indicated replaceable parameter, and the following tokens in alphabetical order.
But, if iterating over a list is what is needed, then the correct construct is
for %%i in ("RegistrationPool" "SubSystemService") DO (
echo Adding - %%i
)
Now, there is a for command iterating over a list of elements. Two elements, two iterations, two "Adding" lines.

Resources