使用批处理从文件中提取包含某些单词的行 [英] Extract lines from file with certain words using batch
问题描述
我有一个文件,其中每一行的格式如下:
I have a file where each line has the following format:
1yjo ASN 1 A GLN 3 B 8.85
我必须提取每一行,其中第二个标记等于 PHE
和第五个标记等于 LEU
或 反之亦然.例如:
I have to extract each line where the second token is equal to PHE
and the fifth token is equal to LEU
or vice versa. For example:
2beg LEU 17 A VAL 18 C 8.76
2beg LEU 17 A PHE 19 A 7.2
2beg PHE 19 A ALA 21 B 8.88
2beg PHE 19 D LEU 34 E 13.28
如果这是输入文件,我将不得不提取第二行和第四行.最好将它们提取到一个单独的文件中.
If this was the input file, I would have to extract the second and the fourth line. It would be good to extract them to a separate file.
这是我迄今为止尝试过的:
Here's what I've tried so far:
@echo off
set inputfile=a.dat
for /F "tokens=1,2,3,4,5,6,7,8" %%a in (%inputfile%) do (
if %%b == "LEU" and %%e == "PHE" do (
@echo %%b %%e
)
)
...但它只是复制整个输入文件,即 IF 条件没有任何影响...
...but it just copies the entire input file, i.e. the IF condition didn't have any effect...
我也试过:
...
if %%b == "LEU" (
if %%e == "PHE" (
@echo %%a %%b %%c %%d %%e %%f %%g %%h
)
)
...
...它不会复制任何东西.
...and it doesn't copy anything.
我也不太确定我是否可以使用and"和or"运算符,或者我必须使用多个嵌套的 if...
I'm also not so sure if I can use "and" and "or" operators, or I have to do it with multiple nested if's...
我该怎么做?
推荐答案
我刚刚找到了一个解决方案:
I've just found a solution:
@echo off
for /F "tokens=1,2,3,4,5,6,7,8" %%a in (%1) do (
if "%%b" == "%2" (
if "%%e" == "%3" (
@echo %%a %%b %%c %%d %%e %%f %%g %%h >> output.txt
)
)
if "%%b" == "%3" (
if "%%e" == "%2" (
@echo %%a %%b %%c %%d %%e %%f %%g %%h >> output.txt
)
)
)
我还使用了输入文件名 (%1) 和两个字符串(%2 和 %3)的输入参数,它的作用就像一个魅力:)
I have also used the input parameters for the input file name (%1) and the two strings (%2 and %3) and it works like a charm :)
这篇关于使用批处理从文件中提取包含某些单词的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!