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 swapping the first line and the last line

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to realize the batch processing of swapping the first line and the tail line, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to know about it.

Then the last line of the extracted text is written in the first line of the new text, the first and last lines of the original text are filtered out with the findstr statement, and the first line extracted is added after the last line.

The code is as follows:

@ echo off

Objective: to swap the first line and the last line

:: train of thought

:: extract the first and last lines (if the first and last lines are blank, the non-empty first and last lines are extracted)

:: then write the extracted last line on the first line of the new text, filter out the first and last lines of the original text with the findstr statement, on the last line

:: and then add the first row extracted.

Because the last line processed by findstr is conditionally limited, a blank line is generated after the last line to ensure the accuracy of the extraction

Note: please make sure that there are no sensitive characters in test.txt, no duplicate lines, and the first line cannot be empty.

::

::

Copy test.txt test.bak > nul

Echo. > > test.txt

Set num=0

Setlocal enabledelayedexpansion

For / f "delims="% I in (test.txt) do (

Set str=%%i

Set / a num+=1

If! num! Equ 1 set var=%%i

)

> test_.txt echo.%str%

Findstr / b / e / v / c: "% str%" / c: "% var%" test.txt > > test_.txt

> > test_.txt echo.%var%

Move test.bak test.txt

Start test_.txt

Goto: eof

@ echo off

:: you can handle situations where the first behavior is empty and the line content is duplicated

Note: because findstr / n ignores the last blank line when counting rows

Therefore, if there are blank lines at the end, please make sure that the number of blank lines is greater than 1

Findstr / n. * test.txt > tmp1.txt

For / f "tokens=1* delims=:"% I in (tmp1.txt) do (

If I equ 1 set str=%%j

Set line=%%i&set var=%%j

)

> tmp2.txt echo line%:%var%

Findstr / b / v / c: "1:" / c: "% line%:" tmp1.txt > > tmp2.txt

Echo 1% str% > > tmp2.txt

Echo. > > tmp2.txt

Cd. > result.txt

For / f "tokens=1* delims=:" I in (tmp2.txt) do > > result.txt echo.%%j

Del / q tmp1.txt tmp2.txt

Start result.txt

Thank you for reading this article carefully. I hope the article "how to interchange the first line and the last line" shared by the editor will be helpful to everyone. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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