Issue
I have two files.
In the the first file it is full of ip addresses, In the second file i am looking to find all the matches of those IP addreses. I then want to append to the front of the line that IP is located on in the second file with a character # and overwrite the file (second).
It seems like all the / are causing my issue but i am unsure
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
Thanks for your help in advance
Solution
EDIT: Since OP has changed Input_file2 so adding solution according to it now.
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
Adding a non-one liner form of above solution now.
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
Could you please try following.
awk '
FNR==NR{
a[$0]=$0
next
}
($2 in a){
$0="#"$0
}
1
' Input_file1 Input_file2 > tmp_file && mv tmp_file Input_file2
Answered By - RavinderSingh13