title: Bash Loop Through Variable With Rows and Columns
body: <p>After searching around for many hours testing different solutions, I've yet to find a working solution. Bash and Shell Scripting is not my strongest area. </p>
  
  <p>I have a variable which has rows (new lines) and columns (tab separated). What I want to do is loop through the rows and get 'Column X' then put that item into a variable so I can do something with that Cell. i.e. echo out for example. </p>
  
  <p>There are an unknown number of rows, but known number of columns, so that it is possible to always get Column 3 for example. </p>
  
  <p>I'm running Bash v.4 on Linux. </p>
  
  <p>Example Input (can't find a way to actually 'tab' things on here?);</p>
  
  <pre><code>UniqueID1 -tab- Description -tab- FriendlyNameABC
  UniqueID1 -tab- Description -tab- FriendlyNameXYZ
  </code></pre>
  
  <p>Example output would be to loop through the listings, get FriendlyNameABC (col 3) and echo that out. </p>
  
  <p>I am trying to expand this script here, <a href="https://www.raspberrypi.org/forums/viewtopic.php?f=63&amp;t=77277&amp;p=1178478#p1178478" rel="nofollow noreferrer">https://www.raspberrypi.org/forums/viewtopic.php?f=63&amp;t=77277&amp;p=1178478#p1178478</a> - which is a shell script to connect to WiFi via WPS. The script provided on that page (below for reference) works, but it is reliant on selecting only the strongest WiFi signal, which isn't always going to be the network you want to connect to. </p>
  
  <pre><code>#!/bin/bash
  # only run if there's no current wifi connection
  if  ! ifconfig wlan0 | grep -q "inet addr:" ; then
         # grab AP's that support WPS, sort by strength and select the strongest 
          wpa_cli scan_results | grep WPS | sort -r -k3 | awk 'END{print $NF}'  &gt;/tmp/wifi
          read ssid &lt; /tmp/wifi
          wpa_cli wps-pbc $ssid
  fi
  </code></pre>
  
  <p>Currently, I've managed to modify the script above so that the line starting with 'wpa_cli..' is now reading as follows, which puts the entire identified WiFi networks in a variable, but then I'm struggling to split this all out. </p>
  
  <pre><code>wpa_cli scan_results | grep WPS | sort -r -k3 $allConnections
  </code></pre>
  
  <p>In essence, the above code I need translating into this pseudo code;</p>
  
  <pre><code>#!/bin/bash
  # only run if there's no current wifi connection
  if  ! ifconfig wlan0 | grep -q "inet addr:" ; then
         # grab AP's that support WPS, sort by strength and select the strongest 
          wpa_cli scan_results | grep WPS | sort -r -k3 | awk 'END{print $NF}'  &gt;/tmp/wifi
          read ssid &lt; /tmp/wifi
  
          #This is the section below that I'm struggling to implement (this is pseudo code, the only bit of working code is the line starting with wpa_cli...)
          while(ssid.hasNext(){
              #Try connecting to the network
              wpa_cli wps-pbc $ssid
              if(successful){break();}
          }
  fi
  </code></pre>
  
  <p>I should add, I've assumed that the data is tab and new line delimited based on how the layout looks when I run the command;</p>
  
  <pre><code>wpa_cli scan_results
  </code></pre>
  
  <p>Is there a way to confirm the delimiter when running commands at the Linux command line?</p>