Batch processing that swaps the first and tail rows

Source: Internet
Author: User

Then, write the last row extracted from the first line of the new text, use the findstr statement to filter out the first and last lines of the original text, and add the first row extracted after the last line. CopyCode The Code is as follows: @ echo off
: Purpose: to swap the first and last rows
: Train of thought:
: Extract the content of the first and last rows (if the first and last rows are empty rows, the extracted content of the first and last non-empty rows is not empty ),
: Write the last row extracted from the first line of the new text, use the findstr statement to filter out the first and last lines of the original text, and then in the last line
: Add the first row to be extracted.
: Because findstr has restrictions on processing the last row, an empty row is generated after the last row to ensure extraction accuracy.
: Note: Make sure that test.txt contains no sensitive characters, no duplicate lines, and the first line cannot be blank.
::
::
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
: it can process empty first behavior and duplicate row content.
: Note: when the number of rows is counted by findstr/n, the last empty row is ignored.
:: therefore, if the last empty row exists, ensure that the number of empty rows 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

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.