使用批处理从文本文件中提取特定文本 [英] Extract specific text from text file using batch

查看:198
本文介绍了使用批处理从文本文件中提取特定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用批处理代码从文本文件中提取特定文本.我需要从中提取数据的文件将包含多行文本,并且行数会有所不同,这意味着指标的位置也会发生变化.这是文本文件的示例:

文件1:

<File>
<General>
   <Primary_1>1.2.3.4.5</Primary_1>
   <Secondary_2>9.8.7.6.5</Secondary_2>
</General>
<Main_List>
   <Details="Title" One="C:\Folder1\Folder2\Folder3\Folder4\Folder5" Two="I" Three="4"/>
</Main_List>
</File>

我已经进行了一些操作,并从文本文件中提取了包含所需数据的行,并将其保存到两个单独的文本文件中,所以我最终得到了这一点:

文件2:

   <Primary_1>1.2.3.4.5</Primary_1>

文件3:

<Details="Title" One="C:\folder1\folder2\folder3\folder4" Two="A" Three="5"/>

因此,从上面的两个文件(文件2和文件3)中,我需要能够提取两个值.第一个在| Primary_1 |之间和|/Primary_1 |指标...在这种情况下,我需要拉"1.2.3.4.5"值.第二个是| Details ="|之后和|"之前的值一个= |指标...在这种情况下,我需要提取标题"值.

我四处搜寻,找不到符合要求的任何内容.我发现最接近的是"...在同一行..."代码(解决方案

当两行都在file.txt中时尝试

如果TAB不在文件中,则它适用于给定的txt.

 @echo off
for /f "tokens=2 delims=<> " %%a in ('find "<Primary_1>" ^< "file.txt" ') do echo "%%a"
for /f "tokens=2 delims==" %%a in ('find "<Details =" ^< "file.txt" ') do SET "xtitle=%%a"
SET ntitle=%xtitle:~1%
SET xtitle="%xtitle%"
ECHO +%ntitle%+ or +%xtitle%+ - your choice...
 

如果您想要的文本包含空格,那么有一种使用助手批处理文件的更健壮的方法.

(Magoo的小挠痒痒–允许在带引号的标题"字符串中留空格-但我不知道该要求是带引号还是不带引号的变量内容,所以您会两者兼有.(不收取额外费用)

I'm trying to extract specific text from a text file using batch code. The file from which I need to extract data will have multiple lines of text and the number of lines will vary, which means the position of the indicators will change as well. Here's a sample of the text file:

File 1:

<File>
<General>
   <Primary_1>1.2.3.4.5</Primary_1>
   <Secondary_2>9.8.7.6.5</Secondary_2>
</General>
<Main_List>
   <Details="Title" One="C:\Folder1\Folder2\Folder3\Folder4\Folder5" Two="I" Three="4"/>
</Main_List>
</File>

I've gone through some manipulation already and extracted the lines that contain the data I need from the text file and saved it to two separate text files so I end up with this:

File 2:

   <Primary_1>1.2.3.4.5</Primary_1>

File 3:

<Details="Title" One="C:\folder1\folder2\folder3\folder4" Two="A" Three="5"/>

So, from the two files above (file 2 & file 3), I need to be able to extract two values. The first being between the |Primary_1| and |/Primary_1| indicators...in this case I would need to pull the "1.2.3.4.5" value. The second being the value after the |Details="| and before the |" One=| indicators...in this case I would need to pull the "Title" value.

I searched around and couldn't find anything that quite fit the bill. The closest I found was the "...on the same line..." code (Extract part of a text file using batch dos), but I kept getting errors. Any help would be greatly appreciated. Thank you.

解决方案

Try this when both lines are in file.txt

It works for the txt as given, if TABs aren't in the file.

@echo off
for /f "tokens=2 delims=<> " %%a in ('find "<Primary_1>" ^< "file.txt" ') do echo "%%a"
for /f "tokens=2 delims==" %%a in ('find "<Details =" ^< "file.txt" ') do SET "xtitle=%%a"
SET ntitle=%xtitle:~1%
SET xtitle="%xtitle%"
ECHO +%ntitle%+ or +%xtitle%+ - your choice...

There is a more robust method using a helper batch file if your wanted text contains spaces.

(little tickle by Magoo - allows spaces in the quoted "Title" string - but I don't know whether the requirement is for quoted or unquoted variable contents...so you get both. (no extra charge)

这篇关于使用批处理从文本文件中提取特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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