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

时间:2015-03-09 13:12:28

标签: regex batch-file

我有一个包含以下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

我想从每一行只捕获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 个答案:

答案 0 :(得分:1)

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

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

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