Copy codeThe Code is as follows: @ echo off
: Purpose:
: In SearchNet. TXT, each row has only one number. The number of repetitions of each number is counted and sorted from high to low by the number of repetitions.
: Train of thought:
: First sort all the numbers by sort, then count the number of repetitions, and write the number + number of repetitions into the local file tmp2.txt;
: Success;
: Extract the content in tmp3.txtby row. Then, find the matching record in tmp2.txtand write the result in tmp3.txt;
: This solution will generate a large number of temporary files, but the efficiency is relatively high.
::
::
Set num =-1
Sort <SearchNet. TXT> tmp1.txt
Cd.> tmp2.txt
Cd.> tmp3.txt
Cd.> result.txt
: Count repeated 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 %
: Sort repeated times
Md tmp & pushd tmp
For/f "tokens = 2" % I in (.. \ tmp2.txt) do (
Cd.> % I
For/l % j in (, % I) do echo. >>%% I
)
> .. \ Tmp3.txt dir/o-s/B
: Extract records by repeated times
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
Other methods for counting the number of characters (no temporary files are generated)
@ Echo off
: Count the number of occurrences of each character and find the most frequently occurring character
: Train of thought:
: By extracting characters in each bit, some dynamic variables starting with: are assigned,
: If the variable name is the same, it is automatically added once, and then extracted at a time using the set character: Command
: All dynamic variables starting with ":" are handed over to the for statement for processing. Set is very clever.
: No need to generate temporary files and sort them 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. % 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
: Count the number of occurrences of Characters
: Train of thought:
: Split the string into a single character string separated by spaces,
: Then, use the for statement to detect the number of times each character appears in the string.
: This method does not need to generate a temporary file and follow
: Display in sequence
::
::
::
Setlocal EnableDelayedExpansion
Set str = adadfdfseffserfefsefseetsdg
Rem disassembling 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 Characters
: Train of thought:
: Disassemble the string and separate it with spaces to form a new string.
: Shift is used to call different parameters, and
: Set to name variables. The variable names start with a uniform one.
: Finally, use set 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.
% Max % = % maxN %
Pause
Exit
: Start
If [% 1] = [] (goto: eof) else (set/a. % 1 + = 1)
If !. % 1! GTR! MaxN! Set maxN = !. % 1! & Set max =. % 1
Shift
Goto: start
@ Echo off
: The simplest 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.
% Max % = % maxN %
Pause
Exit
@ Echo off & setlocal
: After sort, count the number of duplicates by comparing the obtained content with the previous one.
: It takes a lot of skill to save the current and previous content at the same time.
: Note that the initial value of the number of times must be set to 1. The for statement cannot be followed by the parentheses
::
::
Set/a n = 1
For/f % a in ('Type 1.txt ^ | sort ') do (
Call: pp %
)
: Pp
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 character length of each findstr,
: Then sort the same length by sort, thus breaking through
: Sort can only be sorted by characters.
::
::
Set a = [0-9]
For/l % a in (1, 1, 3) do (
Call: pp! A!
Set a =! A! [0-9]
)
Goto c
: Pp
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: pp %
)
: Pp
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