Append data of multiple files to new file separated by a line

I have more than a hundred firewall rules for many different customers in discrete files in a folder.

Suppose the filenames are:

1.wawa.com_firewall_rule

2.newa.com_firewall_rule

Each file contains the IPs of the customers. What I need is to extract the IPs for each customer from the file, and append it to a SINGLE new file in the following format:

wawa.com
192.168.1.1
newa.com
8.8.8.8
9.9.9.9

Right now, I have been able to extract the IPs and save them in new files. But I need to save all in a single file. (as a report) – see my code below

#!/bin/bash
cd /opt/zeus/zxtm/conf/rules/sba_filter/test
for f in *; do 
#echo $f > hosts.txt
grep -E -o "([0-9]{1,3}[.]){3}[0-9]{1,3}" "$f" > "$f.txt"
done

Answer

Preserving your work, a possible solution could be:

#!/bin/bash
cd /home/prova
for f in *; do 
  basename $f '_firewall_rule' >> output.txt
  printf "n" >> output.txt
  grep -E -o "([0-9]{1,3}[.]){3}[0-9]{1,3}" "$f" | while read -r line ; do
    echo $line >> output.txt
    printf "n" >> output.txt
  done
done

See optimization contributions in comments

Leave a Reply

Your email address will not be published. Required fields are marked *