Note that there are some explanatory texts on larger screens.

plurals
  1. PObash string to array with spaces and extra delimiters
    text
    copied!<p>I'm trying to create arrays from strings that have pipe ("|") as delimiters and include spaces. I've been looking around for a while and I've gotten close thanks to sources like <a href="https://stackoverflow.com/questions/918886/split-string-based-on-delimiter-in-bash">How do I split a string on a delimiter in Bash?</a>, <a href="https://stackoverflow.com/questions/1617771/splitting-string-into-array">Splitting string into array</a> and a bunch more. I'm close but it's not quite working. The two main problems are that there are spaces in the strings, there are starting and ending delimiters, and some of the fields are blank. Also, instead of just echoing the values, I need to assign them to variables. Here's the format of the source data:</p> <pre><code>|username|full name|phone1|phone2|date added|servers|comments| </code></pre> <p>Example:</p> <pre><code>|jdoe | John Doe| 555-1212 | |1/1/11 | workstation1, server1 | added by me | </code></pre> <p>Here's what I need:</p> <pre><code>Username: jdoe Fullname: John Doe Phone1: 555-1212 Phone2: Date_added: 1/1/11 Servers: workstation1, server1 Comments: guest account </code></pre> <p>Edit: I use sed to strip out the first and last delimiter and spaces before and after each delimiter, input is now:</p> <pre><code>jdoe|John Doe|555-1212||1/1/11|workstation1, server1|added by me </code></pre> <p>Here's things I've tried:</p> <pre><code>oIFS="$IFS"; IFS='|' for line in `cat $userList`; do arr=("$line") echo "Username: ${arr[0]}" #not assigning a variable, just testing the output echo "Full Name: ${arr[1]}" echo "Phone 1: ${arr[2]}" echo "Phone 2: ${arr[3]}" # etc.. done IFS="$oIFS" </code></pre> <p>Output:</p> <pre><code>Username: Full Name: Phone 1: Phone 2: Username: jdoe Full Name: Phone 1: Phone 2: Username: John Doe Full Name: Phone 1: Phone 2: </code></pre> <p>Another thing I tried:</p> <pre><code>for line in `cat $userList`; do arr=(${line//|/ }) echo "Username: ${arr[0]}" echo "Full Name: ${arr[1]}" echo "Phone 1: ${arr[2]}" echo "Phone 2: ${arr[3]}" # etc done </code></pre> <p>Output:</p> <pre><code>Username: jdoe Full Name: John Phone 1: Phone 2: Username: Doe Full Name: 555-1212 Phone 1: Phone 2: </code></pre> <p>Any suggestions? Thanks!!</p> <hr> <p><strong>Solved:</strong> per the first suggestion below, the script now looks like this:</p> <pre><code>#!/bin/bash userList=`cat userlist | sed 's/^|//; s/|$//; s/[ ]*|[ ]*/|/g;'` oIFS="$IFS"; IFS=$'\n' for line in $userList; do IFS='|' arr=($line) echo "Username: ${arr[0]}" echo "Full Name: ${arr[1]}" echo "Phone 1: ${arr[2]}" echo "Phone 2: ${arr[3]}" #etc done IFS="$oIFS" </code></pre> <p>Output:</p> <pre><code>Username: jdoe Full Name: John Doe Phone 1: 555-1212 Phone 2: Username: jdoe2 Full Name: Jane Doe Phone 1: 555-1212 Phone 2: </code></pre> <p>Works like a charm, thank you very much! I haven't tried the other suggestions below but I will next time I need to do something similar. Thanks everyone!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload