Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the batch processing of counting repetition times and sorting

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to count the number of repetitions and sort the batch processing. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

The code is as follows:

@ echo off

: objective:

:: there is only one number per row in SearchNet.TXT. Count the number of repetitions of each number and sort them from high to low.

:: train of thought

:: first sort all the numbers with sort, then count the number of repetitions, and write to the temporary file tmp2.txt in the format of number + repetition.

: extract the number of repetitions, use this number as the length to establish a file named by this number, use dir to sort the file name (that is, the number of repetitions), and write it to tmp3.txt

:: extract the contents of tmp3.txt by line, then find the records in tmp2.txt that match those in tmp3.txt, and write the result

This scheme will produce a large number of temporary files, but it is more efficient

::

::

Set num=-1

Sorttmp1.txt

Cd. > tmp2.txt

Cd. > tmp3.txt

Cd. > result.txt

:: count the number of repetitions

Setlocal enabledelayedexpansion

For / f% I in (tmp1.txt) do (

Set / a num+=1

Set secondhands paid first!

Set first=%%i

If not "! second!" = "" if! second! Neq! first! (> > tmp2.txt echo! second!! numb flowers set num=0)

)

> > tmp2.txt echo first% num%

:: sort the number of repeats

Md tmp & & pushd tmp

For / f "tokens=2"% I in (..\ tmp2.txt) do (

Cd. >% I

For / l% j in (1JI% I) do echo. >% I

)

>.\ tmp3.txt dir / Omurs / b

:: extract records by number of repeats

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 schemes for counting the number of characters (no temporary files are generated)

@ echo off

Count the number of occurrences of each character and find out the characters with the most occurrences

:: train of thought

:: by extracting the characters on each bit, give uniform some dynamic variables that begin with the character:

:: if the variable name is the same, it is self-incremented once, and then extracted once through the set character: command

:: all dynamic variables that begin with the character: are handed over to the for statement to handle. Set is used cleverly.

Do not need to generate temporary files and arrange them in ascending alphabetical order

::

::

::

Setlocal ENABLEDELAYEDEXPANSION

Set str=adadfdfseffserfefsefseetsdmg

Set / a massif 0rem nympho recorder lumb0

Call: loop

:: the following are the characters that appear the most

For / f "tokens=1,2 delims=="% I in ('set character:') do (

Echo%% iTunes% j

If j GTR! l! Set lags% j & set masks% I

)

Echo. % m% percentage% 1% with the most occurrences.

Pause

Goto: EOF

: loop

Call set masked%% strug percent% n% jiao 1%%

If not defined m goto: EOF

Set / a "character:% m% room1"

Set / a naughty 1

Goto loop

@ echo off

:: count the number of character occurrences

:: train of thought

:: first disassemble the string into a string of single characters 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 follows the

:: display in sequence

::

::

::

Setlocal EnableDelayedExpansion

Set str=adadfdfseffserfefsefseetsdg

Rem disassembles the 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% 1room1

Echo 1! 1!

Goto: eof

@ echo off

:: count the number of characters that appear

:: train of thought

:: disassemble the string and separate it with spaces to form a new string

:: call different parameters through shift, and use the

:: set to name the variable, the variable name has a uniform beginning

Finally, these variables are displayed through set

::

::

::

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.

The most frequent occurrence of echo:% max%=%maxN%

Pause

Exit

: start

If [% 1] = [] (goto: eof) else (set / a.% 1)

If!. 1! GTR! maxN! Set MaxNpermission.% 1 percent & set max=.%1

Shift

Goto: start

@ echo off

To sum up the above schemes, 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.% nasty 1

If!. N! GTR! maxN! Set MaxNaturals.%% nasty setting max=%%n)

Set.

The most frequent occurrence of echo:% max%=%maxN%

Pause

Exit

@ echo off&setlocal

After sort, count the number of repetitions by comparing whether the content fetched this time is equal to that of the previous one

:: it takes a lot of skill to save this and last content at the same time

:: note that the initial value of the number of times should be set to the parenthesis after the parenthesis of the statement, which cannot be immediately followed by the statement.

::

::

Set / a nasty 1

For / f% an in ('type 1.txt^ | sort') do (

Call: pp% a

)

: pp

If not defined bb goto b

If "% bb%" = = "% 1" (set / a nude 1) else (> > ko.txt echo% bb%% n% times & set / a n% times)

: 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 ones of the same length with sort, thus breaking through

The restriction that sort can only sort by character bit size

::

::

Set a = [0-9]

For / l% an in (1pm 1m 3) do (

Call: pp! a!

Set astata! [0-9]

)

Goto c

: pp

For / f% x in ('findstr "^ 1 $" aa.txt^ | sort') do @ echo% x > > dd.txt

Goto: eof

: c

Set / a nasty 1

For / f% an in ('type dd.txt') do (

Call: pp% a

)

: pp

If not defined bb goto b

If "% bb%" = = "% 1" (set / a nude 1) else (> > ko.txt echo% bb%% n% times & set / a n% times)

: b

Set bb=%1

Goto: eof

This is the end of this article on "how to count the number of repetitions and sort batches". I hope the above content can be helpful to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report