I need to split one text file into multiple files using windows batch script, could anybody light me up?
sample text file:
abc1-10
abc1-11
abc1-12
xyz2-01
xyz2-02
xyz3-01
xyz3-02
in this case, it has to split into 3 files, first one consists the lines abc1-xx, second one consists xyz2-xx and xyz3-xx go to the last one
You could use a batch file, but why not just use FINDSTR command?
findstr /R "^abc1-" sample.txt > file1.txt
findstr /R "^xyz2-" sample.txt > file2.txt
findstr /R "^xyz3-" sample.txt > file3.txt
Use the cgwin command SPLIT.
Samples:
-split a file every 500 lines counts:
split -l 500 [filename.ext]
For more: split --help
This may help - it will split the text into separate files of
abc1.txt
xyz2.txt
xyz3.txt
#echo off
for /f "tokens=1,* delims=-" %%a in ('type "file.txt"') do (
>>"%%a.txt" echo(%%a-%%b
)
pause
Related
On a windows 2008 Server, the below more and sort command is being used to sort a large csv file (20MB) by the first column. But the command is still running after 20 minutes! What is the best way to sort large csv files in Windows?
more input.csv +1 | sort > sortedInput.csv
If I have to bet, your file has more than 65535 lines and the more command is waiting for you to press a key (more command makes a pause after each 65535 lines)
Without more information on the .csv file characteristics, this can be used as a starting point
#echo off
setlocal enableextensions disabledelayedexpansion
< input.csv (
set /p header=
setlocal enabledelayedexpansion
echo(!header!
endlocal
findstr "^" | sort
) > output.csv
This will
open the input file for reading
read the first line (max 1021 characters)
output the firstline (delayed expansion is needed)
read the rest of the file (findstr) and pipe the data to sort command
send anything to the output file
Please, note that both set /p and findstr have several limitations that could make this approach fail.
sort input.csv > sorted.csv
I use windows 7.
I have two csv files file1.csv and file2.csv
file1.csv
emp_id;salary
1;1000
2;2000
3;3000
file.csv
emp_id;salary
1;1000
2;2000
3;3000
4;4000
5;5000
I'm confused how to write a batch file.
The batch file should output the should be a csv file showing the changes.
Sample output:
emp_id;salary
4;4000
5;5000
You can use findstr to look for differences, and the /v parameter to display differences. Like so:
findstr /v /g:"file1.csv" "file2.csv"
Also:
for /f "delims=" %%a in (file1.csv) do (
findstr "^%%a$" "file2.csv" >nul ||echo %%a
)
And using the fc command:
fc "file1.csv" "file2.csv"
For fc im sure you can use an if not errorlevel 1 echo No difference
The following code displays file names in a directory and sub-directories and puts the results into results.txt. I am having trouble sorting the list. Where do I put the sort option?
for /r %i in (*) do #echo %~ni >> results.txt
Use the dir command:
dir /ON /B >> results.txt
The /ON sorts by name.
The /B returns in "Bare" (name only) format.
If you add /S it will recurse all sub-directories, but will include the file path.
You'll need to do the sort later, once the results are written to the text file. Add a second line to your batch file reading:
sort results.txt
The results will be written back to results.txt. Alternatively you can get it to go faster by using
sort /O:NewResults.txt results.txt
And getting it to write to a different file.
This can be done with a single line:
(for /r %i in (*) do #echo %~ni)|sort /o results.txt
writing the output to a file with sort switch /o is faster than redirecting sort > file.txt
Only one file gets written, so it should be even faster.
(Note: this is command line syntax. For use in a batchfile, replace each %i with %%i)
I'm trying to run a bat file that will compare 1 file to another and output the differences
I've tried using gnu diff utilites, fc, and endless googleing to find a solution but I cant seem to figure it out
File 1
C:\Books\Tolkien, J.R.R. - The Adventures Of Tom Bombadil.pdf
C:\Books\test.rtf.epub
C:\Books\w_E_20130215.epub
File 2
C:\Books\test.rtf.epub
C:\Books\w_E_20130215.epub
I want file 3 to be
C:\Books\Tolkien, J.R.R. - The Adventures Of Tom Bombadil.pdf
Any one have any ideas?
You could use diff from the DiffUtils and something like this:
diff file1.txt file2.txt | findstr /r /c:"^<" /c:"^>" >file3.txt
The output lines will be preceded by < or >, depending on which file the respective line was missing in. If you want to remove those indicators as well, use something like this:
for /f "tokens=1*" %a in (
'diff file1.txt file2.txt ^| findstr /r /c:"^<" /c:"^>"'
) do #echo %b >>file3.txt
Change %a and %b into %%a and %%b if you want to run this in a batch file.
FINDSTR /v /b /e /l /g:file2. file1. >file3.
should produce the required results - lines in file1 missing from file2.
/v says 'not found', /b /e forces exact match - not part-of-line-matches /l literal.
I am very new to DOS Comments.
I have a text file which has so many entries but it s separated by a delimeter
(Ex: Hi; conf.txt; 161; new team)
I want to print each and every separation in a separate line.
Ex:
The output should be
Hi
conf.txt
161
new team
Can you guide me?
Try:
#echo off
setlocal enabledelayedexpansion
(for /f "tokens=*" %%a in (file.txt) do (
set str=%%a
echo !str:;=!
))>file.tmp
move file.tmp file.txt >nul
Where 'file.txt' is the name of your text file (you need to change it in both places in the batch file).