Batch _dos/bat with statistics of repeat times and sorted

Source: Internet
Author: User
Tags goto
Copy Code code as follows:

@echo off
:: Objective:
:: SearchNet.TXT only one number per line, counting the number of repetitions per count, and according to the number of repetitions from high to low sort
:: Ideas:
:: Sort all the numbers first, then count the number of repetitions, and write the temporary files in the format of number + repeat times tmp2.txt;
:: To extract the number of repetitions, the number for the length of the establishment of the file name, with dir to the file name (that is, repeat the number of times) to order, written to Tmp3.txt;
:: Extracts the content in the Tmp3.txt by row, then looks for the record which matches in the Tmp3.txt in the Tmp2.txt, writes the result;
:: This scenario produces a large number of temporary files, but is more efficient
::
::
Set Num=-1
Sort<searchnet.txt>tmp1.txt
Cd.>tmp2.txt
Cd.>tmp3.txt
Cd.>result.txt
:: Statistics repeat times
Setlocal enabledelayedexpansion
for/f%%i in (tmp1.txt) do (
set/a num+=1
Set second=!first!
Set first=%%i
If not "!second!" = = "" If!second! NEQ!first! (>>tmp2.txt echo!second!!num!&set num=0)
)
>>tmp2.txt Echo%first%%num%
:: Sorting by repeat times
MD tmp && pushd TMP
for/f "tokens=2"%%i in (.. \tmp2.txt) Do (
Cd.>%%i
FOR/L%%j in (1,1,%%i) do echo.>>%%i
)
";" \tmp3.txt dir/o-s/b
:: Extract records by repeat number
For/f%%i in (.. \tmp3.txt) Do (
>> \result.txt findstr "%%i$". \tmp2.txt
)
POPD && rd/q/s TMP
Del tmp1.txt tmp2.txt Tmp3.txt
Start Result.txt
Goto:eof
about other scenarios in which the number of statistics characters appears (no temporary files are generated)
@echo off
:: Statistics The number of occurrences of each character, and find the most frequently occurring characters
:: Ideas:
:: By extracting the characters on each bit, give the uniform character: the beginning of some dynamic variables,
:: If the variable name is the same, then add it again, and then, through the set character: command-time Fetch
:: All dynamic variables that begin with the character: are handed to the for statement. Set is cleverly used.
:: Do not need to generate temporary files, and in ascending alphabetical order
::
::
::
Setlocal enabledelayedexpansion
Set STR=ADADFDFSEFFSERFEFSEFSEETSDMG
set/a m=0,n=0,l=0
Call:loop
:: The following are the most frequently occurring characters
for/f "tokens=1,2 delims=="%%i in (' Set character: ') do (
Echo%%i=%%j
If%%j GTR!l! Set l=%%j& set m=%%i
)
Echo. The most frequent%m%=%l%
Pause
Goto:eof
: Loop
Call Set m=%%str:~%n%,1%%
If not defined M goto:eof
set/a "Character:%m%+=1"
set/a n+=1
Goto Loop
@echo off
:: Statistics character occurrences
:: Ideas:
:: First splitting the string into a space-delimited string of single characters,
:: Then use the For statement to probe the number of occurrences of each character in the string
:: This method does not need to generate a temporary file and follows the
:: Sequential Display
::
::
::
Setlocal enabledelayedexpansion
Set STR=ADADFDFSEFFSERFEFSEFSEETSDG
REM uncoupling string
: Analyze
Set str_tmp=%str_tmp%%str:~0,1%
Set str=%str:~1%
If not "%str%" = "" goto analyze
Rem
For%%i in (%str_tmp%) do call:exclude%%i
Pause
Exit
: Exclude
For%%i in (%counted%) do if "%1" = "%%i" goto:eof
Set counted=%counted%%1
Call:count%1
Goto:eof
: Count
For%%i in (%str_tmp%) do if "%1" = "%%i" set/a%1+=1
Echo%1!%1!
Goto:eof
@echo off
:: Count the number of occurrences of a character
:: Ideas:
:: Splitting a string, separating the new string with a space
:: Call different parameters by shift, and use
:: Set to name variables, variable names with a uniform start
:: Finally, the set is used to display these variables
::
::
::
Setlocal enabledelayedexpansion
Set STR=ADADFDFSEFFSERFEFSEFSEETSDG
: Loop
Set str_tmp=%str_tmp%%str:~0,1% && set str=%str:~1%
If not "%str%" = "" Goto loop
Call:start%str_tmp%
Set.
Echo appears most frequently:%max%=%maxn%
Pause
Exit
: Start
If [%1]==[] (goto:eof) Else (set/a.%1+=1)
if!. %! GTR!maxn! Set maxn=!. %1!&& Set max=.%1
Shift
Goto:start
@echo off
:: Comprehensive above plan, the most concise code is as follows
::
::
Setlocal enabledelayedexpansion
Set STR=ADADFDFSEFFSERFEFSEFSEETSDGADADFDFSEFFSERFEFSEFSEETSDGA
: Loop
Set str$=%str$%%str:~0,1%&set str=%str:~1%
If not "%str%" = "" Goto loop
For%%n in (%str$%) do (
set/a.%%n+=1
if!. %%n! GTR!maxn! Set maxn=!. %%n!&&set max=%%n)
Set.
Echo appears most frequently:%max%=%maxn%
Pause
Exit
@echo off&setlocal
:: After sort, counts the number of repetitions by comparing the contents of this fetch to the previous one.
:: How to save this and last content at the same time requires a lot of skill
:: Note To set the initial value of the number of times to the 1,for statement after the parentheses can not immediately jump out of the statement
::
::
set/a N=1
for/f%%a in (' Type 1.txt^|sort ') do (
Call:p P%%a
)
:p P
If not defined BB goto b
If "%bb%" = = "%1" (set/a n+=1) Else (>>ko.txt echo%bb%%n% times &set/a n=1)
: b
Set bb=%1
Goto:eof
@echo off&setlocal enabledelayedexpansion
:: Code with sorting function
:: Use FOR/L to control the length of each findstr character,
:: And then the same length of the use of sort to break through the
:: Sort can only be sorted by character bit size this limit
::
::
Set A=[0-9]
FOR/L%%a in (1,1,3) do (
Call:p P!a!
Set a=!a! [0-9]
)
Goto C
:p P
for/f%%x in (' findstr ' ^%1$ ' aa.txt^|sort ') do @echo%%x >>dd.txt
Goto:eof
: C
set/a N=1
for/f%%a in (' Type dd.txt ') do (
Call:p P%%a
)
:p P
If not defined BB goto b
If "%bb%" = = "%1" (set/a n+=1) Else (>>ko.txt echo%bb%%n% times &set/a n=1)
: b
Set bb=%1
Goto:eof

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.