根据其他文件中的字符串拆分文件 [英] Split a file based on the string from the other file

查看:60
本文介绍了根据其他文件中的字符串拆分文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件,我想将file1划分成与file2中的行数相等的部分数.

I have two files and i want to divide file1 into number of parts which are equals to number of lines in file2.

将file2中的每一行与file1进行比较,并将file1中所有匹配的记录形成为新文件.

Here each line from file2 will be compared to file1 and all the matched records from file1 are formed as new file.

file1

  <AUDIT_RECORD TIMESTAMP="2013-08-26T19:31:17" NAME="Query" CONNECTION_ID="21974096" STATUS="0" SQLTEXT="SHOW COLLATION"/> 
  <AUDIT_RECORD TIMESTAMP="2013-08-26T19:31:17" NAME="Query" CONNECTION_ID="21974099" STATUS="0" SQLTEXT="SHOW TABLES"/> 
  <AUDIT_RECORD TIMESTAMP="2013-08-26T19:31:17" NAME="Query" CONNECTION_ID="21974095" STATUS="0" SQLTEXT="SHOW COLLATION"/> 
  <AUDIT_RECORD TIMESTAMP="2013-08-26T19:31:17" NAME="Query" CONNECTION_ID="21974094" STATUS="0" SQLTEXT="SHOW COLLATION"/> 
  <AUDIT_RECORD TIMESTAMP="2013-08-26T19:31:17" NAME="Query" CONNECTION_ID="21974099" STATUS="0" SQLTEXT="SHOW COLLATION"/> 
  <AUDIT_RECORD TIMESTAMP="2013-08-26T19:31:17" NAME="Query" CONNECTION_ID="21974094" STATUS="0" SQLTEXT="SET NAMES utf8"/> 

file2

21974096
21974100
21974095
21974094
21974099

必需的输出:

21974094.txt

==========================================
TIMESTAMP="2013-08-26T19:31:17"
SQLTEXT="SET NAMES utf8"

==========================================
TIMESTAMP="2013-08-26T19:31:17"
SQLTEXT="SHOW COLLATION"


21974099.txt

==========================================
TIMESTAMP="2013-08-26T19:31:17"
SQLTEXT="SHOW COLLATION"

==========================================
TIMESTAMP="2013-08-26T19:31:17"
SQLTEXT="SHOW TABLES"


21974095.txt

==========================================
TIMESTAMP="2013-08-26T19:31:17"
SQLTEXT="SHOW COLLATION"

...

要通过awk实现此目的,我编写了如下代码,但它不能满足我的要求.

To achieve this with awk i have written a code like below but it is not satisfying my requirement.

awk 'NR==FNR{a[$4];next}!($4 in a){ print $2 "\n" $6 "\n=========\n" > $4 ".txt"}' file2  file1

任何人都可以通过awk或任何其他shell命令帮助我如何达到上述要求. (它必须至少生成10000个文件,并且最多应在10分钟内完成文件的生成.)

Can any one please help me how to achieve the above requirement with awk or any other shell command. (It has to generate minimum of 10000 files and it should complete generating files in max 10 minutes.)

如果我已经执行了以下命令,则几乎可以达到,但不能完全满足要求.

If i have executed below command, it is almost reached but not completely satisfying the requirement.

awk 'NR==FNR{a[$1];next}{split($4,b,"\"")}(b[2] in a){print $2"\n"$10"\n=========\n" > b[2]".txt"}' file2 file1

从文件之一中输出

TIMESTAMP="2013-08-26T19:57:34"
SQLTEXT="/*
=========

TIMESTAMP="2013-08-26T19:57:34"
SQLTEXT="/*
=========

TIMESTAMP="2013-08-26T19:57:34"
SQLTEXT="SHOW

但是我想要像下面这样的输出

But i want OUTPUT like below

TIMESTAMP="2013-08-26T19:57:34"
SQLTEXT="/*show variables"
=========

TIMESTAMP="2013-08-26T19:57:34"
SQLTEXT="/* select * from table "
=========

TIMESTAMP="2013-08-26T19:57:34"
SQLTEXT="SHOW collations "

在这里,我需要仅将file1的定界符指定为'',而没有将file2的定界符指定为..

Means here i need to specify delimiter for only file1 as '"' and no delimiters for file2..

任何人都可以帮忙吗?

推荐答案

您应该使用split函数用"字符分隔第四个单词,以便获得与file2中的值匹配的数字.您还应该在第一个块中a[$1].

You should use the split function to separate the fourth word by the " character, so that you get the number that matches the values in file2. You should also a[$1] in the firth block.

此脚本应该起作用:

awk 'NR==FNR{a[$1];next}{split($4,b,"\"")}(b[2] in a){print $2"\n"$6"\n=========\n" > b[2]".txt"}' file2  file1

更新:

只要file1中没有多余的引号,我们就可以使用"字符作为字段分隔符:

As long as there are not extra quotes in file1, we can use the " character as a field separator:

awk -F\" 'NR==FNR{a[$1];next}($6 in a){print "TIMESTAMP=\""$2"\"\nSQLTEXT=\""$10"\"\n=========\n" > $6".txt"}' file2  file1

我们用"字符分隔输入文件,所以字段$ 2是时间戳,字段$ 6是conn. id,并且提交的$ 10是SQLTEXT.

We split the input file with the " character as a delimiter, so field $2 is the timestamp, field $6 is the conn. id, and filed $10 is the SQLTEXT.

第一个块NR==FNR{a[$1];next}用file2中的连接ID填充数组(第一个文件的NR == FNR).使用($6 in a),我们过滤第二个文件的行(因为我们在第一个块中调用了next),其连接ID是表a的索引.如果找到匹配项,则执行块{print "TIMESTAMP=\""$2"\"\nSQLTEXT=\""$10"\"\n=========\n" > $6".txt"}',它将相关信息打印到文件conn_id.txt

The first block NR==FNR{a[$1];next} populates the array with the coonection ids from file2 ( NR == FNR for the first file). With ($6 in a) we filter the lines of the second file (since we called next in the first block), for which the connection id is an index of table a. If we find a match, then the block {print "TIMESTAMP=\""$2"\"\nSQLTEXT=\""$10"\"\n=========\n" > $6".txt"}' is executed, which prints the relevant information to the file conn_id.txt

这篇关于根据其他文件中的字符串拆分文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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