我有两个文件。
在第一个文件中,它充满了IP地址, 在第二个文件中,我正在寻找这些IP地址的所有匹配项。 然后,我想在第二个文件中IP所在的行的前面加上一个字符#,并覆盖文件(第二个)。
似乎所有/都引起了我的问题,但我不确定
file1.txt
10.148.88.137
file2.txt
#
# Accounting file(s)
#
<acct-file /var/log/pmta/acct.csv>
delete-after 30d
move-interval 5m
max-size 50M
records d
record-fields d timeLogged,bounceCat,vmta,orig,rcpt,srcMta,dlvSourceIp,jobId,dsnStatus,dsnMta,dsnDiag,header_x-id
</acct-file>
<acct-file /var/log/pmta/bounce.csv>
delete-after 30d
move-interval 5m
max-size 50M
records b,rb
record-fields b timeLogged,bounceCat,vmta,orig,rcpt,srcMta,dlvSourceIp,jobId,dsnStatus,dsnMta,dsnDiag,header_x-id
record-fields rb *,header_x-id
</acct-file>
############################################################################################
############################### Global Domain Directive ####################################
<domain *>
backoff-retry-after 15m
backoff-to-normal-after 1m
bounce-after 72h
bounce-upon-5xx-greeting true
dkim-algorithm rsa-sha256
dkim-body-canon simple
dkim-sign yes
ignore-8bitmime true
ignore-chunking yes
max-connect-rate 10/m
max-msg-per-connection 450
max-msg-rate 60/m
max-rcpt-per-message 100
max-smtp-out 5
mx-connection-attempts 10
require-starttls no
retry-upon-new-mail true
smtp-553-means-invalid-mailbox yes
smtp-pattern-list blockList
use-starttls yes
</domain>
smtp-listener 10.148.88.137
smtp-listener 10.148.1.137
##################################################################################################
# EOF
File 2 Overwritten (file2.txt)
#
# Accounting file(s)
#
<acct-file /var/log/pmta/acct.csv>
delete-after 30d
move-interval 5m
max-size 50M
records d
record-fields d timeLogged,bounceCat,vmta,orig,rcpt,srcMta,dlvSourceIp,jobId,dsnStatus,dsnMta,dsnDiag,header_x-id
</acct-file>
<acct-file /var/log/pmta/bounce.csv>
delete-after 30d
move-interval 5m
max-size 50M
records b,rb
record-fields b timeLogged,bounceCat,vmta,orig,rcpt,srcMta,dlvSourceIp,jobId,dsnStatus,dsnMta,dsnDiag,header_x-id
record-fields rb *,header_x-id
</acct-file>
############################################################################################
############################### Global Domain Directive ####################################
<domain *>
backoff-retry-after 15m
backoff-to-normal-after 1m
bounce-after 72h
bounce-upon-5xx-greeting true
dkim-algorithm rsa-sha256
dkim-body-canon simple
dkim-sign yes
ignore-8bitmime true
ignore-chunking yes
max-connect-rate 10/m
max-msg-per-connection 450
max-msg-rate 60/m
max-rcpt-per-message 100
max-smtp-out 5
mx-connection-attempts 10
require-starttls no
retry-upon-new-mail true
smtp-553-means-invalid-mailbox yes
smtp-pattern-list blockList
use-starttls yes
</domain>
#smtp-listener 10.148.88.137
smtp-listener 10.148.1.137
##################################################################################################
# EOF
非常感谢您的帮助
答案 0 :(得分:0)
编辑: :由于OP更改了Input_file2,因此现在要根据它添加解决方案。
awk 'FNR==NR{a[$0];next} match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/) && substr($0,RSTART,RLENGTH) in a{$0="#"$0} 1' Input_file1 Input_file2 > temp_file && mv temp_file Input_file2
现在添加上述解决方案的非一个内衬形式。
awk '
FNR==NR{
a[$0]
next
}
prev!=FILENAME{
close(out)
System("mv " out OFS prev)
}
match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/) && substr($0,RSTART,RLENGTH) in a{
$0="#"$0
}
{
prev=FILENAME
out=prev".temp"
print $0 > out
}
END{
close(out)
system("mv " out OFS prev)
}
' Input_file Input_file2
请您尝试以下。
awk '
FNR==NR{
a[$0]=$0
next
}
($2 in a){
$0="#"$0
}
1
' Input_file1 Input_file2 > tmp_file && mv tmp_file Input_file2
答案 1 :(得分:0)
如果第一个文件仅包含IP,并在一行中列出每个IP,请尝试gnu sed,
sed -Ez 's/\n/\|/g; s!(.*)\|!s/^\\w.+(\1)/# \\1/!' file1.txt| sed -Ef - file2.txt