从文件中读取搜索字符串,然后在另一个文件中为字段添加字符串

时间:2019-02-27 18:35:19

标签: python bash shell awk sed

我有一个要求,可以读取2个文件,例如file1和file2

file1具有搜索字符串

file2包含一些数据

要求是从文件1逐行读取搜索字符串,并针对文件2中的行搜索该字符串,如果找到,则在文件2中的2字段中添加“ _done”。

示例: file1:具有

BEN2T
KEN3T
MILDRED

文件2:具有

RICKy2 Monthly    "CASE, WORKLOAD, INVENTORY" Workload-cli
BEN2T Monthly   INTERFACES      Interface-cli
KEN3T Daily   INTERFACES      Interface-cli
MARCUS3 Monthly   "CASE, WORKLOAD, EMPLOYMENT SERVICES, INVENTORY"    Workload-cli
NANCY2 Monthly   "CASE, WORKLOAD, EMPLOYMENT SERVICES, INVENTORY"    Workload-cli
MILDRED Monthly "FISCAL, CLAIMS"    Port

现在预期的output.txt如下所示:

RICKy2 Monthly    "CASE, WORKLOAD, INVENTORY" Workload-cli
BEN2T Monthly_done   INTERFACES      Interface-cli
KEN3T Daily_done   INTERFACES      Interface-cli
MARCUS3 Monthly   "CASE, WORKLOAD, EMPLOYMENT SERVICES, INVENTORY"    Workload-cli
NANCY2 Monthly   "CASE, WORKLOAD, EMPLOYMENT SERVICES, INVENTORY"    Workload-cli
MILDRED Monthly_done    "FISCAL, CLAIMS"    Port

尝试:

    #!/usr/bin/env python3
import fileinput

with fileinput.FileInput(filename, inplace=True, backup='.bak') as file:
    for line in file:
        print(line.replace(text_to_search, replacement_text), end='')

但这是替换文件。

我可以使用shell或python

谢谢

2 个答案:

答案 0 :(得分:3)

请您尝试以下。

awk 'FNR==NR{a[tolower($0)];next} {$2=tolower($1) in a?$2"_done":$2} 1' Input_file1   Input_file2

或者按照@blhsing的评论,在这里也可能有帮助。

awk '                                   ##Starting awk program here.
FNR==NR{                                ##checking condition FNR==NR which will be TRUE when fir Input_file isbeing read.
  a[tolower($0)]                        ##Creating an array named a whose index is value of current line andtolower changes line to all lower characters.
  next                                  ##next will skip all further statements from here.
}
tolower($1) in a{                       ##checking if lower value of $1 is present in array a if yes then do following.
  $2=$2"_done"                          ##Appending _done to value of $2 here.
}
1
'   file1  file2                        ##Mentioning Input_file names here.

说明: 立即添加代码说明。

awk '                                   ##Starting awk program here.
FNR==NR{                                ##checking condition FNR==NR which will be TRUE when fir Input_file isbeing read.
  a[tolower($0)]                        ##Creating an array named a whose index is value of current line andtolower changes line to all lower characters.
  next                                  ##next will skip all further statements from here.
}
{                                       ##Starting a block here which will be executed once 2nd Input_file named Input_file2 is being read.
  $2=tolower($1) in a?$2"_done":$2      ##Here checking condition if lower value of $1 is present in array a if yes then append _done to$2 or keep it as it is.
}                                       ##Closing block here.
1                                       ##Mentioning 1 here will print edited/non-edited value of line.
'   Input_file1   Input_file2           ##Mentioning Input_file names here.

输出如下。

RICKy2 Monthly "CASE, WORKLOAD, INVENTORY" Workload-cli
BEN2T Monthly_done INTERFACES Interface-cli
KEN3T Daily_done INTERFACES Interface-cli
MARCUS3 Monthly "CASE, WORKLOAD, EMPLOYMENT SERVICES, INVENTORY" Workload-cli
NANCY2 Monthly "CASE, WORKLOAD, EMPLOYMENT SERVICES, INVENTORY" Workload-cli
MILDRED Monthly_done "FISCAL, CLAIMS" Port

答案 1 :(得分:0)

您可以将read file1 放入一个集合中,以便可以使用生成器表达式来查找与通过 csv.reader 方法生成的名称匹配:

 导入csv
完成=设置(map(str.rstrip,open('file1')))
与open('file2_new','w')为f:
    csv.writer(f).writerows((name,freq +'done',* rest)for name,freq,* rest in csv.reader(open('file2'),delimiter ='',skipinitialspace = True)如果名称完成