批处理脚本:搜索文件,提取编号,然后复制到新文件中 [英] Batch Scripting:search file, extract numbers, and copy into new file

查看:117
本文介绍了批处理脚本:搜索文件,提取编号,然后复制到新文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是批处理脚本的新手,并且有一个问题.我想知道是否有可能按要求搜索.txt文件并获取指定的数据并复制到新的.txt文件中?

I'm new to batch scripting and have a question. I was wondering if it's possible to search a .txt file by requirements and take data specified and copy into a new .txt file?

就像我有50行9位数字和一堆我不需要的废话,我可以说, 对于以1,2,3,4,5,6,7,8或9开头的任何行...请使用前9位数字并将其复制到新文件中,以用于文件中的所有行??? "

Like if I have 50 lines with 9 digit numbers and a bunch of other crap I don't need after them can I say, "For any line beginning with a 1,2,3,4,5,6,7,8,or 9...take the first 9 digits and copy them into a new file, for all lines in the file???"

我认为这比尝试删除所有其他内容要容易.让我知道您是否知道如何执行此操作!谢谢.

I thought this would be easier than trying to delete all the other stuff. Let me know if you know anything about how to do this! Thanks.

以下是一行内容的示例:
123456789 @ example
我只需要从大约50行中提取9位数字即可.

Here's an example of what one line looks like:
123456789@example
and I just need to extract the 9 digit numbers from about 50 lines of this.

推荐答案

您可以使用FINDSTR过滤掉所有不以9位数字开头的行.然后,FOR/F可以逐行读取结果.设置了一个变量,子字符串操作仅保留前9位数字.

You can use FINDSTR to filter out all lines that do not start with 9 digits. Then FOR /F can read the result, line by line. A variable is set, and a substring operation preserves just the first 9 digits.

@echo off
setlocal enableDelayedExpansion
(
  for /f %%A in (
    'findstr "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" yourFile.txt'
  ) do (
    set "ln=%%A"
    echo !ln:~0,9!
  )
)>newFile.txt

这篇关于批处理脚本:搜索文件,提取编号,然后复制到新文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆