Code for extracting content from different lines through batch processing

Source: Internet
Author: User

For instance :-

For/F "delims =" % A in (input.txt) Do...

For/F "delims =" % A in ('Type input.txt ') Do...

For/F "delims =" % A in ('more ^ <input.txt ') Do...

However, only the last method (using the more command) will give consistent results into SS Windows NT, 2000, XP and 2003. the first method does not recognize Unicode files. also, the usebackq switch must be used if the input filename contains spaces. the second method, using the type command, also fails to recognize Unicode files on Windows 2000, XP and 2003 if the input file does not begin with a bit order mark (BOM ).

In all the examples, assume the contents of the file numbers.txt to be :-

One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten

Displaying the first line

This example prints one.

@ Echo off & setlocal enableextensions
Set "first ="
For/F "delims =" % A in ('more ^ <numbers.txt ') Do (
If not defined first set first = %
)
Echo/% first %

Displaying the first x lines

This example prints one, two and three.

@ Echo off & setlocal enableextensions
Set "lines = 3"
Set I =-1
Set "OK ="
For/F "delims =" % A in ('more ^ <numbers.txt ') Do (
Set/a I + = 1 & for/F % Z in ('echo/% I % ') Do (
If "% Z" = "% lines %" set OK = 1
)
If not defined OK echo/%
)

Displaying the last line

This example prints ten.

@ Echo off & setlocal enableextensions
For/F "delims =" % A in ('more ^ <numbers.txt ') do set "last = %"
Echo/% last %

Displaying the last x lines

This example prints nine and ten.

@ Echo off & setlocal enableextensions
Set "lines = 2"
For/F % A in ('Find/C/V "" ^ <numbers.txt ') do set/a skip = % A-Lines
For/F "delims =" % A in ('more/e + % skip % ^ <numbers.txt ') Do (
Echo/%
)

Displaying the nth line

This example prints three. note that instead of using the more Command's/e switch, the Skip option cocould have been used with the for/F command, however, this fails is it is set to any number less than one.

@ Echo off & setlocal enableextensions
Set lineno = 3
Set "line ="
Set/A lineno-= 1
For/F "delims =" % A in ('more/e + % lineno % ^ <numbers.txt ') Do (
If not defined line set "line = %"
)
Echo/% line %

Displaying the nth line plus X number of lines

This example prints five and six.

@ echo off & setlocal enableextensions
set start = 5
set "lines = 2"
set/A I =-1, start-= 1
set "OK ="
for/F "delims =" % A in ('more/e + % start % ^ set/a I + = 1 & for/F % Z in ('echo/% I % ') do (
If "% Z" = "% lines %" set OK = 1
)
if not defined OK echo/% A
)

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.