Unable to edit text files with shared delimiter - batch-file

I am trying to write a script that takes a one lined text file containing the output from another script and removes the commas\spaces and replaces them with a carriage return.
Sample
ENTRY_1331_TFS273350_03, ENTRY_1331_TFS282928, ENTRY_1331_TFS292719,
Desired Output
ENTRY_1331_TFS273350_03
ENTRY_1331_TFS282928
ENTRY_1331_TFS292719
I have tried using variations of the below script (doesn't do anything)
for /f "tokens=* delims=, " %%a in (input.txt) do (
echo %%a>> output.txt
echo( >>output.txt
)
or (only echos the first object)
setLocal EnableDelayedExpansion
for /f "delims==, " %%A in (input.txt) do (
set string=%%A
echo !string!>>output.txt
echo( >>output.txt
)
Any help or advice would be appreciated,
Thanks!

Try this.
#ECHO OFF
(FOR /F "USEBACKQ DELIMS=" %%A IN ("input.txt"
) DO FOR %%B IN (%%A) DO ECHO=%%B)>"output.txt"

Related

I've made a batch file that's meant to display a list and its sublist but it doesn't work

Every entry in the main list is written in a new line, while every entry in a sublist is separated with a comma in the file. The first entry of a sublist is its title. Something like: title,entry,entry each line, with a dynamic number of entries. Sorry for newbie mistakes, I'm pretty new to this. Also sorry if it was asked before (couldn't find anything like that myself).
What i've written looks like this and I have no idea why doesn't it work:
SETLOCAL EnableDelayedExpansion
for /F "tokens=*" %%G in (list.txt) do (
set chos=%%G
for /F "tokens=1 delims=," %%H in ("!chos!") echo %%H:
set chos=!chos:,=^
!
for /F "tokens=* skip=1" %%I in ("!chos!") echo - %%I
)
pause >nul
you miss "do" before echo
for /F "tokens=1 delims=," %%H in ("!chos!") do echo %%H:
A black window will flash away when the batchfile has some errors. Calling batchfile in the cmd.exe will keep the error message.
edit: You can also do it in this way:
#echo off&SETLOCAL EnableDelayedExpansion
for /F "tokens=*" %%G in (list.txt) do (
for /F "tokens=1,* delims=," %%H in ("%%G") do (
echo %%H
for %%a in (%%I) do (
echo -%%a
)
)
)
pause

Script to convert fixed width flat file to csv

I want to make a generic script which will convert a fixed width flat file into csv. Below is my approach:
`echo off
setlocal EnableDelayedExpansion
echo a,b,c>final.txt
for /f "tokens=1 delims=;" %%i in (source.txt) do (
set x=%%i
for /f "tokens=1,2 delims=," %%a in (config.txt) do (
call SET VAR=!x:~%%a,%%b!
for %%p in (!VAR!) do (echo/|set /p ="%%p,"
) >>final.txt
)
)
`
Config file which I am using contains the parameters for substring which states from where to do the substring and how many characters.
Config file contains:
0,9
9,3
12,11
23,7
30,1
31,1
32,5
37,9
46,9
55,3
58,9
67,9
76,9
85,9
94,1
Source file contains the actual fixed with source.
Now with my code I am getting the result but for the fields which has no value/empty in the source is not reflecting in the final output.
Example:
Source:
1234<space>678 [col1=678,col2=space,col3=678]
Output (Current):
1234,678
Output(Expected):
1234,,678
Please help
Your for %%p in (!VAR!) do (…) loop executes defined (…) command for all strings found in !VAR! delimited with white space(s). So if !VAR! results to all white spaces, then consequential for %%p in ( ) do (…) loop executes nothing. Next code snippet could help:
echo off
setlocal EnableDelayedExpansion
echo a,b,c>final.txt
rem replace the €€€ string with any unused one
set "fooString=€€€"
for /f "tokens=1 delims=;" %%i in (source.txt) do (
set "x=%%i"
for /f "tokens=1,2 delims=," %%a in (config.txt) do (
call SET "VARraw=!x:~%%a,%%b!%fooString%"
rem replaced with respect to the OP's comment: for %%p in (!VARraw!) do (
for /F "tokens=*" %%p in ("!VARraw!") do (
set "rav=%%p"
set "var=!rav:%fooString%=!"
echo/|set /p "=!var!,"
) >>final.txt
)
)
Edit: note that for /F "tokens=*" %%p in ("!VARraw!") do ( would remove leading spaces from the !VARraw! string.

is there any command in cmd prompt similar to cut -d in unix?

I have a file foo which contains the below content.
Exception/BatchUpdateException/SQLException 755BJX/5268501774913
Exception/BatchUpdateException/SQLException 5DL9E4/5268501583930
Exception/BatchUpdateException/SQLException 5NHBDO/5268501765579
I need only the below output from the content.
755BJX,5268501774913
5DL9E4,5268501583930
5NHBDO,5268501765579
Could you please help on this.
Another one:
for /F "tokens=4,5 delims=/ " %%a in (test.txt) do echo %%a,%%b
#echo off
for /f "tokens=2" %%i in (c:\temp\log.txt) do call :parse %%i
:parse
set text=%1
echo %text:/=,%
explaination :
We read each line of the logfile, take the second part ( right side of the space, as is a default delimiter.
Then we assign this to a variable and we use in place substitution to convert /to ,
FOR /F "tokens=2" %%I IN (test.txt) DO FOR /F "tokens=1,2 delims=/" %%J IN ("%%I") DO #echo %%J,%%K

Find And Replace in a TXT From CSV File

I'm trying to find words on a first column of a CSV or XLS file, and replace them with words in the second column of the CSV of XLS. I have made something like that but it doesn't work.
Can you help me? For each line, the first column in a variable called ita and the second column in a variable called eng, and then find Ita and replace Eng. As you can imagine I need to translate a web page, starting from the csv with a language for each column. My csv file structure is:
ita1;eng1
ita2;eng2
etc...
This is my wrong script:
#echo off
setlocal enableextensions enabledelayedexpansion
set host=%COMPUTERNAME%
echo Host: %host%
pause
for /f "tokens=1 delims=;" %%Ita in (index.csv) do (
SET ita=%%Ita
echo %ita%
pause
for /f "tokens=2 delims=;" %%eng in (index.csv) do (
set eng=%%eng
echo %eng
pause
(for /f "delims=" %%i in ('findstr /n "^" "index.txt"') do (
set "transl=%%i"
set "transl=!line:%ita%=%eng%!"
echo(!line!
endlocal
))>"index2.txt"
type "index2.txt"
)
)
)
(for /f "tokens=1,2 delims=;" %%a in (index.csv) do echo(%%b;%%a)>index2.txt
type index2.txt
#echo off
setlocal enableextensions enabledelayedexpansion
(for /f "tokens=* usebackq" %%l in ("index.txt") do (
set "line=%%l"
for /f "tokens=1,2 delims=; usebackq" %%a in ("index.csv") do (
set "line=!line:%%a=%%b!"
)
echo !line!
)) > index2.txt
But this approach doesn't care of substring match.
Try using awk
awk -F';' 'NR==FNR {a[$1]=$2;next} { for(x in a) gsub(x,a[x]) } 1' index.csv index.txt

How do I only assign FOR /F token from one line only in a test file?

How do I make this grab the token from the first line ONLY in .txt file instead of looping through every line. I want %%m to be assigned to the 3rd token on line one only then stop.
#echo off
FOR %%A IN (.\xdrive\*.txt) DO (
FOR /F "usebackq tokens=3 delims=," %%m IN ("%%A") DO (
IF "%%m" == "F01" (xcopy /Y "%%A" .\Outbound)
pause
)
)
pause
set /p can be used to read the first line, and then you can use a FOR /F loop to get the third token
setlocal EnableDelayedExpansion
FOR %%A IN (%1) DO (
<%%A set /p firstline=
FOR /F "tokens=3 delims=," %%m IN ("!firstline!") DO (
echo %%m
)
)
Without see the eg files and knowing exactly what you're trying to do I can't test this, but here's the listing of firstline.bat which should do what you're asking for :) At first I thought this needed to be more complicated than it is... after your first if simply use a goto to exit the for structure after it's first call - problem solved?
#echo off
::: firstline.bat - Retrieve the first line from a series of files
::: usage: firstline $filespec
::: filespace - files to process (eg .\xdrive\*.txt)
if "%~1"=="" findstr "^:::" "%~f0"&GOTO:EOF
FOR %%A IN (%1) DO (
call :testfirst "%%A"
)
goto :eof
:testfirst
FOR /F "usebackq tokens=3 delims=," %%m IN (%1) DO (
IF "%%m" == "F01" (xcopy /Y %1 .\Outbound)
goto:eof
)
See this post, which shows how to mimic the gnu head utility using a dos batch file:
Windows batch command(s) to read first line from text file
untested
read first line tokens3
for /f "tokens=3 delims=," %%a in ('"findstr /n . %1|findstr /b 1:"') do set fltok3=%%a
echo(%fltok3%
Hackish =
#echo off
FOR %%A IN (.\xdrive\*.txt) DO (
FOR /F "usebackq tokens=3 delims=," %%m IN ("%%A") DO (
IF "%%m" == "F01" (xcopy /Y "%%A" .\Outbound)
GOTO:EOF
)
)
So all you're doing is escaping the loop after the first pass instead of continuing onto the next line.

Resources