Reputation: 501
i have a req to read 2 files say file1 and file2
file1 has search strings
file2 has some data
Requirement is to read search string line by line from file 1 and search that string against lines in file 2 and if found add "_done" for 2 field in file2.
Example: file1: has
BEN2T
KEN3T
MILDRED
file2: has
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
Now expected output.txt will be like below:
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
tried:
#!/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='')
but this is replacing the file.
Im okay with shell or python
Thankyou
Upvotes: 0
Views: 88
Reputation: 133600
Could you please try following.
awk 'FNR==NR{a[tolower($0)];next} {$2=tolower($1) in a?$2"_done":$2} 1' Input_file1 Input_file2
Or as per @blhsing's comment following may also help 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.
}
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.
Explanation: Adding explanation for code now.
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.
Output will be as follows.
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
Upvotes: 3
Reputation: 106881
You can use read file1
into a set so that you can use a generator expression to look for matches with names generated from the csv.reader
method:
import csv
done = set(map(str.rstrip, open('file1')))
with open('file2_new', 'w') as f:
csv.writer(f).writerows((name, freq + 'done', *rest) for name, freq, *rest in csv.reader(open('file2'), delimiter=' ', skipinitialspace=True) if name in done)
Upvotes: 0