my text file is as shown below:
H;SEQUENCENUMMER;TIMESTAMP;VERSION;VALIDITY_FLAG;SID
D;1077;1383519656;"20.0.8-2";1;;
D;1079;1383519657;"20.0.8-2";2;;
i want to parse this using windows batch program and output file will look like as beloW:
H SEQUENCENUMMER TIMESTAMP VERSION VALIDITY_FLAG SID
D 1077 1383519656 "20.0.8-2" 1
D 1078 1383519657 "20.0.8-3" 2
something like a tab delimited
pls suggest
#echo off
setlocal enableextensions enabledelayedexpansion
rem Get a tab character
for /f tokens^=^*^ delims^= %%t in ('forfiles /p "%~dp0." /m "%~nx0" /c "cmd /c echo(0x09"') do set "tab=%%t"
rem For each line in text file, replace ; with a tab
(for /f "tokens=*" %%l in (plaintext.txt) do (
set "line=%%l"
echo !line:;=%tab%!
)) > output.txt
endlocal
#echo off
setlocal enabledelayedexpansion
if exist output.txt del output.txt
for /f "delims=" %%a in ('type "Your Text File.txt"') do (set $line=%%a
echo !$line:;= !>>output.txt)
You can use nested for-loops as follows:
#echo off
echo.|set a=>outfile.txt
for /f %%A in (infile.txt) do (
for %%B in (%%A) do (
echo.|set /P ="%%B ">>outfile.txt
)
echo.>>outfile.txt
)
You don't really seem to be parsing anything, you are just replacing semicolons with tabs. You can use VBScript to do this. Save the following as "process.vbs"
Option Explicit
Dim Line,Regex
Set RegEx = new RegExp
Regex.Pattern = ";"
Regex.Global=True
Do While Not WScript.StdIn.AtEndOfStream
Line = WScript.StdIn.ReadLine()
Line = Regex.Replace(Line,VBTab)
WScript.Echo Line
Loop
Then you can run it like this:
vbscript /nologo process.vbs < yourfile > newfile
Related
windows cmd batch
I have a text file that looks like this:
Dog
Cat
Frog
Bat
Bird
Mouse
I want to attribute a number to each string, each line.
So that it becomes
1 Dog
2 Cat
3 Frog
4 Bat
5 Bird
6 Mouse
Then I want to ask the user to input a number, and then have the corresponding string stored in the variable.
So if the user inputs 1, then the variable is set to the string Dog
So when the user inputs 1 the program stores/outputs Dog
set /p var1="number? " so var1 becomes the string, not the number.
This does the first part of the task kinda, but now I need the second part, storing the strings in avariable.
#echo off
set TEXT_T="list.txt"
set /a c=0
setlocal ENABLEDELAYEDEXPANSION
FOR /F "tokens=1 usebackq" %%i in (%TEXT_T%) do (
set /a c=c+1
echo !c! %%i
)
endlocal
pause
Here below is an updated answer, thanks to LotPings.
With a small tweak to ask for the folder by string
This provides an easier way to use Megatools from CMD
https://megatools.megous.com/man/megals.html
https://github.com/megous/megatools
#echo off
:start:
megals /Root/
set /p var1="dir? " & megals /Root/%%var1%%
for /f "tokens=1,* delims=:" %%A in ('megals -n /Root/%%var1%% ^|findstr
/n "." ') do (
set Link[%%A]=%%B
Echo %%A %%B
)
for /f "tokens=1,* delims=:" %%A in ('megals -n -e /Root/%%var1%% ^|findstr
/n "." ') do (
set Link[%%A]=%%B
)
set /p choice=Select #:
Call Set Link=%%Link[%choice%]%%
set "trimmedlink="
for %%h in (%Link%) do if not defined trimmedlink set "trimmedlink=%%h"
Megadl %trimmedlink% && goto :start:
pause
Edit: Had to trim %Link% to just the first word, i.e just the link
The output of Megals -e /Root/misc looks like this:
The asterisk are the unique link ids for the files
/Root/misc
https://mega.nz/#!********!********************* /Root/misc/File1
https://mega.nz/#!********!********************* /Root/misc/File2
https://mega.nz/#!********!********************* /Root/misc/File3
With the batch script above it looks like:
1 File1
2 File2
3 File3
Select #: <------input file number
Edit2 number listing is fixed
Edit3 Parentheses in filenames crash the program
i.e
1 File(1)
The chosen file number then gets passed to Magadl as the corresponding link
Megadl Link
The batch script allows you to download the link by just entering the corresponding file number so you don't have to type out the long link id in a standard cmd window.
To use megals output directly and avoid delayedexpansion (which removes the !)
Findstr /n will do the numbering.
#echo off
for /f "tokens=1,* delims=:" %%A in ('megals -e /Root/ ^|findstr /n "." ') do (
set Item[%%A]=%%B
Echo %%A %%B
)
set /p choice=Select #:
Call Echo Choice:%%Item[%choice%]%%
Using a (pseudo-)call with doubled percent signs is an old fashioned method of realizing delayed expansion without the problem with the !.
In programming/scripting you need to adapt techniques to fit your needs.
Without knowing the exact output of your megatools,
this could do the job :
#echo off
for /f "tokens=1,* delims=:" %%A in ('megals -e /Root/ ^|findstr /n "." ') do (
set Folder[%%A]=%%B
Echo %%A %%B
)
set /p choice=Select #:
Call Set Folder=%%Folder[%choice%]%%
for /f "tokens=1,* delims=:" %%A in ('megals -e %Folder% ^|findstr /n "." ') do (
set Link[%%A]=%%B
Echo %%A %%B
)
set /p choice=Select #:
Call Set Link=%%Link[%choice%]%%
megadl %Link%
As compo advised, please edit your question to contain all necessary information - don't force others to gather it from unnecessary answer and comments.
Just use an array:
#echo off
setlocal ENABLEDELAYEDEXPANSION
set TEXT_T="list.txt"
set /a c=0
FOR /F "tokens=1 usebackq" %%i in (%TEXT_T%) do (
set /a c=c+1
echo !c! %%i
set string[!c!]=%%i
)
set /P number=Enter number:
echo !string[%number%]!
pause
For further details, see this answer.
What about making the output of a command into a list, instead of a text file into a list?
so the line with var1 here would be turned into a numbered list (the files listed in that directory), and Var2 is the number the users enters to create the returned string that gets passed to a command. i.e
https://megatools.megous.com/man/megals.html
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
(megals -e /Root/) <--------list all folders in root
(set /p var1="dir? " && megals -e /Root/!var1!) <-- select folder + list files
(set /p var2="Megalink? " && Megadl !var2!) <--- enter the file name for download
ENDLOCAL
pause
I want to be able to enter a number for the download, instead of the long file name. So this way the user can enter the number that corresponds to the link that they want to download. So if they enter 1 then the program does Megadl Dog
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
megals -e /Root/
set /p var1="dir? "
megals -e /Root/!var1! > rlist.txt
set TEXT_T="rlist.txt"
set /a c=0
FOR /F "tokens=1 usebackq" %%i in (%TEXT_T%) do (
set /a c=c+1
echo !c! %%i
set string[!c!]=%%i
)
set /P number=Enter number:
Megadl !string[%number%]!
Endlocal
pause
This kills the first part of the link because it removes everything in between and including exclamations. !dasasdasd!
all megalinks start with #!something!
This didn't work
I need output of Call Echo %%Item[%choice%]%% passed to Megadl
#echo off
for /f "tokens=1,* delims=:" %%A in ('megals -e /Root/anime ^|findstr /n "." ') do (
set Item[%%A]=%%B
Echo %%A %%B
)
set /p choice=Select #:
Call Echo %%Item[%choice%]%%
for /F "tokens=*" (`%%Item[%choice%]%%`) do (
set "var1=%%A"
Megadl !Var1!
)
pause
I have a text file which containts stuff like
M test123
S test
M abc
and so on...
I'm trying to write a batch script that will do the following:
Read this text file, search every line for "M " (with spaces!) and then save the found line in a variable, delete the "M " and store the output in a seperate output.txt
So the output.txt should containt then following:
test123
S test
abc
Here's what I have so far:
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (output_whole_check.txt) DO (
SET var!count!=%%F
findstr /lic:"M " > nul && (set var!count!=var!count!:~8%) || (echo not found)
SET /a count=!count!+1
)
ENDLOCAL
Or is there some easier way to solve that without any additional stuff installed on windows?
Try this one. It echoes all lines to output.txt with "M " replaced with nothing.
#echo off & setlocal
>output.txt (
FOR /F "usebackq delims=" %%I IN ("output_whole_check.txt") DO (
set "line=%%I"
setlocal enabledelayedexpansion
echo(!line:M =!
endlocal
)
)
Result:
test123
S test
abc
Or if your output_whole_check.txt is very large, it might be faster to loop over the lines using a for /L loop. for /L is more efficient than for /F. You just have to get a count of the lines to determine how many iterations to loop.
#echo off & setlocal
rem // get number of lines in the text file
for /f "tokens=2 delims=:" %%I in ('find /v /c "" "output_whole_check.txt"') do set /a "count=%%I"
<"output_whole_check.txt" >"output.txt" (
for /L %%I in (1,1,%count%) do (
set /P "line="
setlocal enabledelayedexpansion
echo(!line:M =!
endlocal
)
)
The result is the same output.
I wanna make script, which in document example.txt sort all lines by length (some line has spaces). The longest line will be first line, the shortest line will be at end of the document. Script can rewrite original document. Thank you :-)
It is surprisingly easy and fast to accomplish this by writing each line as its own file within a temporary folder. Then use DIR /B /O-S to sort the files (lines) by size, capturing the result with FOR /F and then printing each file (line) with TYPE.
#echo off
setlocal disableDelayedExpansion
set "file=example.txt"
set "tempLoc=sortLinesTemp"
md "%tempLoc%"
set "cnt=0"
for /f usebackq^ delims^=^ eol^= %%A in ("%file%") do (
set /a cnt+=1
set "ln=%%A"
setlocal enableDelayedExpansion
echo(!ln!>"%tempLoc%\f!cnt!"
endlocal
)
(for /f %%F in ('dir /b /o-s "%tempLoc%"') do type "%tempLoc%\%%F")>"%file%.new"
move /y "%file%.new" "%file%" >nul
rd /s /q "%tempLoc%"
type "%file%"
This solution will strip empty lines. Empty lines can be preserved with a bit more code.
Also, line lengths are limited to a bit less than 8191 characters. This limitation is inherent to any pure native batch solution.
This batch file uses a VBS script to help get the line lengths, and sorts, then rewrites the input.txt file into input.new.txt
Use the batch like this: sortline.bat "filename.txt"
Leading | characters in a line will vanish.
#echo off
set "file=%temp%\sortline.vbs"
(
echo. Const ForReading = 1, ForWriting = 2
echo. infile = "%~1"
echo. Set fso = CreateObject("Scripting.FileSystemObject"^)
echo. Set f1 = fso.OpenTextFile(infile, ForReading^)
echo. Do While not f1.AtEndOfStream
echo. f = f1.readline
echo. Wscript.echo right(10000+len(f^),4^) ^& "|" ^& f
echo. loop
echo. f1.close
)>"%file%"
(for /f "tokens=1,* delims=|" %%a in (' cscript //nologo "%file%" ^|sort /r ') do echo(%%b)>"%~n1.new.txt"
del "%file%"
I think this is the simplest and fastest way to do that:
#echo off
setlocal EnableDelayedExpansion
set /A seqNum=10000, accumLen=0
set "lastLine="
for /F "tokens=1* delims=:" %%a in ('findstr /O "^" example.txt') do (
if not defined lastLine (
set "lastLine=%%b"
) else (
set /A "seqNum+=1, thisLen=10000-(%%a-accumLen), accumLen=%%a"
set "line[!thisLen!!seqNum:~-4!]=!lastLine!"
set "lastLine=%%b"
)
)
for %%a in (example.txt) do (
set /A "seqNum+=1, thisLen=10000-(%%~Za-accumLen)"
set "line[!thisLen!!seqNum:~-4!]=!lastLine!"
)
(for /F "tokens=1* delims==" %%a in ('set line[') do echo %%b) > sorted.txt
This solution strip empty lines and exclamation marks from the file. Both limitations may be fixed, if needed.
You need the strLen function from this page. Also, here is a download link to download sortn.bat , which will give you the hints to get started. Also, you need to be familiar with the basic compare function:
public int compare(String o1, String o2) {
if (o1.length()!=o2.length()) {
return o1.length()-o2.length();
}
return o1.compareTo(o2);
}
Good luck.
All commands must be performed in Windows Command Prompt
I have a file data.txt which has several strings in it:
gargonxx**stringX**moregargon
gargonargongargongargon
gargon**stringZ**xxgargonxxxx
and for this data file, I want to create a "library" file:
stringX = informationx
stringY = informationy
stringZ = informationz
then create variables in CMD out of the "information" shown in the "library" file,
ONLY of the instances in the data.txt file that match with the library file.
varx = string's informationx
and then relay this information in the command window.
echo varx
How do I go about
Having CMD recognize the instances that match
Relaying the instance's information in the command window
Here are my LIBRARY and DATA files
#ECHO OFF
SETLOCAL
FOR /f "delims==" %%a IN ('set $ 2^>nul') DO "SET %%a="
FOR /f "delims=" %%a IN (data.txt) DO (
FOR /f "tokens=1*delims== " %%L IN (library.txt) DO (
IF /i "%%a"=="%%L" SET $%%a=%%M&ECHO(%%M
)
)
ECHO ==== OR =====
SET $
GOTO :EOF
use a text editor to create the library file.
This batch matches the instances that match.
Showing the information depends on what you want to do.
With 'data.txt`
lineX
lineZ
and library.txt
lineX = informationx
lineY = informationy
lineZ = informationz
the above batch showed
informationx
informationz
==== OR =====
$lineX=informationx
$lineZ=informationz
For revised specification
#ECHO OFF
SETLOCAL
FOR /f "delims==" %%a IN ('set $ 2^>nul') DO "SET %%a="
FOR /f "tokens=1*delims== " %%L IN (library.txt) DO (
FINDSTR /i /L "%%L" data.txt >nul
IF NOT ERRORLEVEL 1 SET $%%L=%%M&ECHO(%%M
)
ECHO ==== OR =====
SET $
GOTO :EOF
(same results, except line replaced by string)
is there a way to transfer characters in the a plain .txt file.
I have a lot of entries in the .txt file and they are all in this format:
1 = Example,
2 = Example2,
3 = Example3...
what I need is a batch file to transfer "# =" to the right side of the string. So it looks like this:
Example = 1,
Example2 = 2...
Is it possible to that with a .bat file?
This will handle more than one word where you have Example...
#echo off
for /f "tokens=1,* delims== " %%a in (input.txt) do >>output.txt echo %%b = %%a
If you can use GNU sed:
sed "s/\(\S\+\)\s=\s\(\S\+\)/\2 = \1/" file
Something like this should work:
#echo off
pushd "C:\some\where"
for /f "tokens=1,2 delims==" %%a in (input.txt) do echo %%b = %%a
popd
Or use this to get rid of whitespace:
#echo off
setlocal EnableDelayedExpansion
pushd "C:\some\where"
for /f "tokens=1,2 delims==" %%a in (input.txt) do (
set "key=%%a"
set "val=%%b"
echo !val: =! = !key: =!
)
popd
To save the output to a file, call either script like this:
switch.cmd >output.txt