title: Bash: splitting a list of strings each containing space-separated words in different variables for each word
body: <p>I'm trying to parse the <strong>apache error log</strong> to grep the lines that corresponds to the &quot;offending&quot; IPs found in the <strong>fail2ban</strong> log.</p>
  <p>I'm using a script in <strong>bash</strong>.</p>
  <p>First I extract the offending IPs:</p>
  <pre><code>offenders=$(grep -F &quot;[apache-errors] Found&quot; /var/log/fail2ban.log | awk '{print $8}' | sort | uniq)
  </code></pre>
  <p>Then for each IP I get the entries from the fail2ban.log; there may be multiple entries, because the IP may have done requests at multiple times:</p>
  <pre><code>for ip in $offenders; do
      entries=$(grep -F &quot;[apache-errors] Found $ip&quot; /var/log/fail2ban.log | awk '{print $8&quot; &quot;$10&quot; &quot;$11}' | sort | uniq)
   
      declare _count_entries=$(echo &quot;${entries[@]}&quot; | wc -l)
      echo &quot;Found $_count_entries error entries for IP $ip&quot;
  
      for entry in &quot;${entries[@]}&quot;; do
          echo &quot;$entry&quot;
      done
  done
  </code></pre>
  <p>This is what I get so far (IPs have been anonymized):</p>
  <pre><code>[INFO] Found 1 error entries for IP 10.10.0.29
  10.10.0.29 2021-12-20 06:33:12
  [INFO] Found 2 error entries for IP 10.20.0.242
  10.20.0.242 2021-12-21 10:51:44
  10.20.0.242 2021-12-30 12:03:55
  [INFO] Found 3 error entries for IP 10.30.0.186
  10.30.0.186 2022-01-02 05:20:49
  10.30.0.186 2022-01-02 05:40:24
  10.30.0.186 2022-01-02 07:38:55
  </code></pre>
  <p>Now what I want to do is, for <em>each</em> line extract the ip, date and time portions. I tried something like this, but <strong>IT DOES NOT WORK</strong>, it prints only the (ip,date,time) for the first <code>entry</code>:</p>
  <pre><code>for ip in $offenders; do
      entries=$(grep -F &quot;[apache-errors] Found $ip&quot; /var/log/fail2ban.log | awk '{print $8&quot; &quot;$10&quot; &quot;$11}' | sort | uniq)
      
      for entry in &quot;${entries[@]}&quot;; do
  
          echo &quot;$entry&quot;
  
          _ip=($(echo &quot;$entry&quot; | cut -d ' ' -f1))
          _date=($(echo &quot;$entry&quot; | cut -d ' ' -f2))
          _time=($(echo &quot;$entry&quot; | cut -d ' ' -f3))
          echo &quot;ip=$_ip , date=$_date , time=$_time&quot;
  
      done
  done
  </code></pre>
  <p>Output: for each entry, only the (ip,date,time) portions of the first one is echoed:</p>
  <pre><code>[INFO] Found 1 error entries for IP 10.10.0.29
  10.10.0.29 2021-12-20 06:33:12
  ip=10.10.0.29 , date=2021-12-20 , time=06:33:12
  [INFO] Found 2 error entries for IP 10.20.0.242
  10.20.0.242 2021-12-21 10:51:44
  10.20.0.242 2021-12-30 12:03:55
  ip=10.20.0.242 , date=2021-12-21 , time=10:51:44
  [INFO] Found 3 error entries for IP 10.30.0.186
  10.30.0.186 2022-01-02 05:20:49
  10.30.0.186 2022-01-02 05:40:24
  10.30.0.186 2022-01-02 07:38:55
  ip=10.30.0.186 , date=2022-01-02 , time=05:20:49
  </code></pre>
  <p>The desired output would be:</p>
  <pre><code>[INFO] Found 1 error entries for IP 10.10.0.29
  10.10.0.29 2021-12-20 06:33:12
  ip=10.10.0.29 , date=2021-12-20 , time=06:33:12
  [INFO] Found 2 error entries for IP 10.20.0.242
  10.20.0.242 2021-12-21 10:51:44
  10.20.0.242 2021-12-30 12:03:55
  ip=10.20.0.242 , date=2021-12-21 , time=10:51:44
  ip=10.20.0.242 , date=2021-12-30 , time=12:03:55
  [INFO] Found 3 error entries for IP 10.30.0.186
  10.30.0.186 2022-01-02 05:20:49
  10.30.0.186 2022-01-02 05:40:24
  10.30.0.186 2022-01-02 07:38:55
  ip=10.30.0.186 , date=2022-01-02 , time=05:20:49
  ip=10.30.0.186 , date=2022-01-02 , time=05:40:24
  ip=10.30.0.186 , date=2022-01-02 , time=07:38:55
  </code></pre>
  <p>So how can I do that in bash?</p>
  <p>The final goal is to use the ip, date and time portions to build a regex like this, because I want to grep the lines from the error logs that correspond exactly to the findings in the fail2ban log:</p>
  <pre><code>grep -P &quot;^(\[$_date $_time)(.+\[client )($_ip).+$&quot; /var/log/apache2/error.log
  </code></pre>