Using bash, I'm trying to replace multiple lines in a stored variable with sed and then print the variable with the line changes.
The list variable looks like this - the virtmx elements will all be unique - non repeating
$list variable:
domain1.com =====> dest.domain.com:25
domain2.com =====> virtmx-0050:25
domain3.com =====> dest.domain2.com:25
domain4.com =====> domain.example.com:25
domain5.com =====> dest.domain3.com:25
domain6.com =====> virtmx-0051:25
domain7.com =====> dest.domain4.com:25
domain8.com =====> dest.domain5.com:25
domain9.com =====> dest.domain6.com:25
where I want to replace all virtmx-[num] with another line from a file on the system.
somefile.log has both line[num] and the replacement line together:
example of lines in file.log:
cat /somedir/somefile.log | grep virtmx-*
<mta id="50" host="virtmx-0050:25" dns_type="MX" desc="mxgroupname">
<mta id="51" host="virtmx-0051:25" dns_type="MX" desc="mxgroupname2">
example of line extraction with awk:
cat /somedir/file.log | grep virtmx-0050 | awk -F "\"" '{print $4,$8}'
virtmx-0050:25 mxgroupname
virtmx-0051:25 mxgroupname2
desired output:
domain1.com =====> dest.domain.com:25
domain2.com =====> mxgroupname
domain3.com =====> dest.domain2.com:25
domain4.com =====> domain.example.com:25
domain5.com =====> dest.domain3.com:25
domain6.com =====> mxgroupname2
domain7.com =====> dest.domain4.com:25
domain8.com =====> dest.domain5.com:25
domain9.com =====> dest.domain6.com:25
$listoflines holds just the lines themselves:
line1
line2
I'm using awk to extract the lines then create a sed replacement string - which I use to replace entries in the $output variable:
line1/replacement1
I think I'm close but the below code is only replacing the first instance in the loop then start over and printing the whole variable again with a new replacement, but missing the first. Instead I want to print the whole variable once with all the replacements. The amount of replacements can vary so that's why I'm trying to use a for loop.
for i in $listoflines
do
replace=`cat /dir/somemfile.log | grep $i | awk -F "\"" '{print $4,$8}' | sed 's/ /\//'`
echo "$list" | sed 's/'$replace'/'
done
line2? Also why are you trying to split the file on"rather than spaces. Also, you almost definitly need to read your variable line by line here you will always print your$outputlines incorrectly twice.somefile.logalso?