Dennis
Dennis

Reputation: 21

find all matches between two files then append character to beginning all matches in the second file

I have two files Listed.csv and Config.txt and what i would like to do is find all exact string matches between the two files and then append a # to the beginning of the matching lines in the Config.txt file.

I should note that there are periods in the file(s) as they are mainly made up of IP addresses (IPv4)

Example of what i'm looking to do

Listed.csv

smtp-listener 10.0.0.1
smtp-listener 10.0.0.2

Config.txt

smtp-listener 10.0.0.1
smtp-listener 10.0.0.2

Output Desired

Config.txt

#smtp-listener 10.0.0.1
#smtp-listener 10.0.0.2

Code I've already tried

#!/bin/bash
echo "Replace Periods with {}"
sed -i 's/\./{}/g' config.txt
sed -i 's/\./{}/g' Listed.csv

CONFIG="$(cat Listed.csv)"
sed -i "s/$CONFIG/#$CONFIG/g" config.txt

echo "Replace {} with Periods"
sed -i 's/{}/\./g' config.txt
sed -i 's/{}/\./g' Listed.csv

echo "Done"

Upvotes: 0

Views: 26

Answers (1)

karakfa
karakfa

Reputation: 67507

awk to the rescue!

$ awk 'NR==FNR{a[$0]; next} $0 in a{$0="#" $0}1' listed config > config.updated

Upvotes: 2

Related Questions