使用批处理脚本在所有行中的文本文件中查找特定字符串或正则表达式,并使用for循环将它们写入另一个文本文件

时间:2021-04-19 23:59:26

I have a text file that contains these 9 lines:

我有一个包含这9行的文本文件:

Successfully started load asymmetric-song-851:bqjob_r08ac53fc_0000014bfdd9d371_1
Successfully started load asymmetric-song-851:bqjob_r44afbc8a_0000014bfdd9e792_1
Successfully started load asymmetric-song-851:bqjob_r7874a1e7_0000014bfdd9f7ca_1
Successfully started load asymmetric-song-851:bqjob_r11fb8a32_0000014bfdda0583_1
Successfully started load asymmetric-song-851:bqjob_r6b174b76_0000014bfdda1486_1
Successfully started load asymmetric-song-851:bqjob_r65772621_0000014bfdda239f_1
Successfully started load asymmetric-song-851:bqjob_r6e1918e0_0000014bfdda312d_1
Successfully started load asymmetric-song-851:bqjob_r08e15175_0000014bfdda3fa7_1
Successfully started load asymmetric-song-851:bqjob_r319141ff_0000014bfdda4e9e_1

I want to capture only jobid from each line,i.e. bqjob_r08ac53fc_0000014bfdd9d371_1. So the output text file should be like this:

我想从每一行只捕获jobid,即。 bqjob_r08ac53fc_0000014bfdd9d371_1。所以输出文本文件应该是这样的:

bqjob_r0f2cb934_0000014bfe9c563c_1  
bqjob_r6022172e_0000014bfe9c6318_1  
bqjob_r67eae0a9_0000014bfe9c6f12_1  
bqjob_r56ab0212_0000014bfe9c7d67_1  
bqjob_r75d2e18d_0000014bfe9c89aa_1  
bqjob_r1c73e74f_0000014bfe9c9576_1  
bqjob_r31dae331_0000014bfe9ca28e_1  
bqjob_r56873b54_0000014bfe9caeb7_1  
bqjob_r0edb7092_0000014bfe9cbb8c_1 

1 个解决方案

#1


1  

Split the string on : and echo the second part into a new file.

拆分字符串:并将第二部分回显到新文件中。

for /F "tokens=1,2 delims=:" %%A in (data.txt) do echo %%B>>output.txt

This code assumes that your text file is called data.txt and that the lines you've shown us are the only lines in the file.

此代码假定您的文本文件名为data.txt,并且您向我们显示的行是文件中的唯一行。

#1


1  

Split the string on : and echo the second part into a new file.

拆分字符串:并将第二部分回显到新文件中。

for /F "tokens=1,2 delims=:" %%A in (data.txt) do echo %%B>>output.txt

This code assumes that your text file is called data.txt and that the lines you've shown us are the only lines in the file.

此代码假定您的文本文件名为data.txt,并且您向我们显示的行是文件中的唯一行。