批处理提取不同行上的内容的代码

所属分类: 脚本专栏 / DOS/BAT 阅读数: 712
收藏 0 赞 0 分享
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 across Windows NT, 2000, XP and 2003. The first method does not recognise 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 recognise 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 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=%%a
)
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/%%a
)

Displaying the last line

This example prints ten.

@echo off & setlocal ENABLEEXTENSIONS
for /f "delims=" %%a in ('more ^< numbers.txt') do set "last=%%a"
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/%%a
)

Displaying the Nth line

This example prints three. Note that instead of using the more command's /e switch, the skip option could 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=%%a"
)
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% ^< 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/%%a
)
更多精彩内容其他人还在看

Windows cmd命令行输入输出重定向问题

这篇文章主要介绍了Windows cmd命令行输入输出重定向问题,需要的朋友可以参考下
收藏 0 赞 0 分享

cmd下过滤文件名称的两种方法

这篇文章主要介绍了cmd下过滤文件名称的两种方法,需要的朋友可以参考下
收藏 0 赞 0 分享

DOS命令行下使用HaoZip进行文件压缩的方法

这篇文章主要介绍了DOS命令行下使用HaoZip进行文件压缩的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

经常用的DOS命令大全(经典收藏)

DOS(Disk Operating System)是一个使用得十分广泛的磁盘操作系统,就连眼下流行的Windows9x/ME系统都是以它为基础。这篇文章主要介绍了经常用的DOS命令大全(经典收藏),需要的朋友可以参考下
收藏 0 赞 0 分享

一键配置jdk环境变量的批处理代码

在重装系统后,可能需要配置计算机的环境变量,在这里以配置Java环境变量为例讲解一种比较简单的设置方法,需要的朋友可以参考下
收藏 0 赞 0 分享

批处理版chm文件反编译器 v1.3

你手头是否有几本chm格式的电子书?你是否有过满世界搜索反编译chm软件的痛苦经历?你是否抱怨过这些软件有这样或那样的限制?然而,你是否意识到,就在你的系统里,微软已经为你准备好了小巧而强悍的反编译程序
收藏 0 赞 0 分享

纯批处理获取硬件信息的代码

此程序纯批处理,且纯净绿色,也不会在生成文件中包含我的个人信息,大家不要随意修改里面的代码结构,很可能自己电脑没问题,到了别的电脑就会获取不准
收藏 0 赞 0 分享

批处理万年历实现代码(包括农历日期)

这篇文章主要介绍了批处理万年历实现代码(包括农历日期),月历查询工具 最初发表于CN-DOS,输出数字排序有问题大家可以自行修复一下
收藏 0 赞 0 分享

批处理文件bat脚本实现代码自动提交和项目部署

这篇文章主要介绍了批处理文件bat脚本实现代码自动提交和项目部署,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

cmd环境变量命令set 设置永久环境变量命令setx

这篇文章主要介绍了cmd环境变量命令set 设置永久环境变量命令setx,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多