How batches display lines with only a specified number of characters
This article mainly introduces how to display lines with only a specified number of characters in batch processing, which is very detailed and has a certain reference value. Friends who are interested must finish reading it!
The code is as follows:
@ echo off
Goto begin
Parameter 1: text file name
Parameter 2: the number of characters of the lines to be displayed (save)
For example, to print a line with 4 characters in a.txt:
Print.bat a.txt 4
Or drag and drop the file you want to process directly onto the script file and follow the prompts.
Very efficient
: begin
Setlocal ENABLEDELAYEDEXPANSION
Set var=%2
If "% var%" = "" set / p var= enter the number of characters of the line to display:
For / l%% I in (1JI% var%) do set stringers.
Findstr / x str% 1
Pause > nul
Goto: eof
@ echo off
:: displays a line of text with only 4 characters
:: test.txt has only pure letters, no spaces, no blank lines
:: because it is a full-text analysis, it is inefficient
:: code by jm 2006-8-14
For / f% I in (test.txt) do (
Set str=%%i
Set var=%%i
Call: loop
Set num=0
)
Pause
Goto: eof
: loop
Set var=%var:~0,-1%&& set / a num+=1
If not "% var%" = "" goto loop
If num% equ 4 echo str%
Goto: eof
@ echo off
:: it is more efficient to judge by inserting characters at the beginning and end.
:: suitable for situations with spaces in the line
:: if you do not insert characters, but only judge the situation of the fifth character, you will make an error
:: code by 3742668
Setlocal ENABLEDELAYEDEXPANSION
For / f "delims="% I in (test.txt) do (
Set str=b%%ie
If not "! str:~5,1!" = "" if "! str:~6,1!" = "" echo. % I
)
Pause
Goto: eof
The above is all the content of the article "how to display lines with only a specified number of characters in batches". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!