grep – Shell:查找多个文件中的匹配行
我正在尝试使用
shell脚本(以及“单行”)来查找大约50个文件之间的任何公共行.
编辑:注意我正在寻找所有文件中出现的一行(行) 到目前为止,我已经尝试过grep grep -v -x -f file1.sp *,它只是匹配所有其他文件中的文件内容. 我也试过grep -v -x -f file1.sp file2.sp | grep -v -x -f – file3.sp | grep -v -x -f – file4.sp | grep -v -x -f-file5.sp等…但我相信使用要搜索的文件作为STD搜索而不是匹配的模式. 有谁知道如何用grep或其他工具做到这一点? 我不介意运行需要一段时间,我必须在大约500个文件中添加几行代码,并希望在每个文件中找到一条公共行,以便插入’after'(它们最初是只是从一个文件c& p所以希望有一些共同的线!) 谢谢你的时间, 解决方法
old,bash answer(O(n);打开2 * n个文件)
从@ mjgpy3回答,你只需要创建一个for循环并使用comm,就像这样: #!/bin/bash tmp1="/tmp/tmp1$RANDOM" tmp2="/tmp/tmp2$RANDOM" cp "$1" "$tmp1" shift for file in "$@" do comm -1 -2 "$tmp1" "$file" > "$tmp2" mv "$tmp2" "$tmp1" done cat "$tmp1" rm "$tmp1" 保存在comm.sh中,使其可执行,然后调用 ./comm.sh *.sp 假设您的所有文件名都以.sp结尾. 更新的答案,python,只打开每个文件一次 看看其他答案,我想给每个文件打开一次,而不使用任何临时文件,并支持重复的行.另外,让我们并行处理文件. 你去(在python3中): #!/bin/env python import argparse import sys import multiprocessing import os EOLS = {'native': os.linesep.encode('ascii'),'unix': b'n','windows': b'rn'} def extract_set(filename): with open(filename,'rb') as f: return set(line.rstrip(b'rn') for line in f) def find_common_lines(filenames): pool = multiprocessing.Pool() line_sets = pool.map(extract_set,filenames) return set.intersection(*line_sets) if __name__ == '__main__': # usage info and argument parsing parser = argparse.ArgumentParser() parser.add_argument("in_files",nargs='+',help="find common lines in these files") parser.add_argument('--out',type=argparse.FileType('wb'),help="the output file (default stdout)") parser.add_argument('--eol-style',choices=EOLS.keys(),default='native',help="(default: native)") args = parser.parse_args() # actual stuff common_lines = find_common_lines(args.in_files) # write results to output to_print = EOLS[args.eol_style].join(common_lines) if args.out is None: # find out stdout's encoding,utf-8 if absent encoding = sys.stdout.encoding or 'utf-8' sys.stdout.write(to_print.decode(encoding)) else: args.out.write(to_print) 将其保存到find_common_lines.py中,然后调用 python ./find_common_lines.py *.sp 使用–help选项获取更多使用信息. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |