title: How to create a function for split that uses only awk and accepts any string as input and as delimiter?
body: <p>The proposal is to be a function using only awk for splitting strings that accepts any string as a delimiter and any string as an input.</p>
  <p>How to create a function for split that uses only awk and accepts any string as input and as delimiter?</p>
  <p>There are many, many proposals (<a href="https://stackoverflow.com/a/45201229/3223785">see this example</a>) for string splitting with bash commands, but all of them only work in specific cases and not according to our proposal.</p>
  <p>We decided to present our code as an example, but despite being fully functional, there are several points that we think can be improved/adjusted/corrected.</p>
  <h3>Example function (f_split)</h3>
  <pre><code>F_PRESERVE_BLANK_LINES_R=&quot;&quot;
  f_preserve_blank_lines() {
      : 'Remove &quot;single quotes&quot; used to prevent blank lines being erroneously removed.
  
      The &quot;single quotes&quot; are used at the beginning and end of the strings to prevent
      blank lines with no other characters in the sequence being erroneously removed.
      We do not know the reason for this side effect. This problem occurs, for example,
      in commands that involve &quot;awk&quot;.
  
      Args:
          STR_TO_TREAT_P (str): String to be treated.
  
      Returns:
          F_PRESERVE_BLANK_LINES_R (str): String treated.
      '
  
      F_PRESERVE_BLANK_LINES_R=&quot;&quot;
      STR_TO_TREAT_P=$1
      STR_TO_TREAT_P=${STR_TO_TREAT_P%?}
      F_PRESERVE_BLANK_LINES_R=${STR_TO_TREAT_P#?}
  }
  
  F_SPLIT_R=()
  f_split() {
      : 'It does a &quot;split&quot; into a given string and returns an array.
  
      Args:
          TARGET_P (str): Target string to &quot;split&quot;.
          DELIMITER_P (Optional[str]): Delimiter used to &quot;split&quot;. If not informed the
      split will be done by spaces.
  
      Returns:
          F_SPLIT_R (array): Array with the provided string separated by the informed
      delimiter.
      '
  
      F_SPLIT_R=()
      TARGET_P=$1
      DELIMITER_P=$2
      if [ -z &quot;$DELIMITER_P&quot; ] ; then
          DELIMITER_P=&quot; &quot;
      fi
  
      REMOVE_N=1
      if [ &quot;$DELIMITER_P&quot; == &quot;\n&quot; ] ; then
          REMOVE_N=0
      fi
  
      # PROBLEM: This was the only parameter that has been a problem so far... There are
      # probably others. Maybe a scheme using &quot;sed&quot; would solve the problem...
      if [ &quot;$DELIMITER_P&quot; == &quot;./&quot; ] ; then
          DELIMITER_P=&quot;[.]/&quot;
      fi
  
      if [ ${REMOVE_N} -eq 1 ] ; then
  
          # PROBLEM: Due to certain limitations we have some problems getting the output
          # of a split by awk inside an array and so we need to use &quot;line break&quot; (\n)
          # to succeed. Seen this, we remove the line breaks momentarily afterwards
          # we reintegrate them. The problem is that if there is a line break in the
          # &quot;string&quot; informed, this line break will be lost, that is, it is erroneously
          # removed in the output...
          TARGET_P=$(awk 'BEGIN {RS=&quot;dn&quot;} {gsub(&quot;\n&quot;, &quot;3F2C417D448C46918289218B7337FCAF&quot;); printf $0}' &lt;&lt;&lt; &quot;${TARGET_P}&quot;)
  
      fi
  
      # PROBLEM: The replace of &quot;\n&quot; by &quot;3F2C417D448C46918289218B7337FCAF&quot; results in
      # more occurrences of &quot;3F2C417D448C46918289218B7337FCAF&quot; than the amount of &quot;\n&quot;
      # that there was originally in the string (one more occurrence at the end of
      # the string). We can not explain the reason for this side effect. The line below
      # corrects this problem...
      TARGET_P=${TARGET_P%????????????????????????????????}
  
      SPLIT_NOW=$(awk -F &quot;$DELIMITER_P&quot; '{for(i=1; i&lt;=NF; i++){printf &quot;%s\n&quot;, $i}}' &lt;&lt;&lt; &quot;${TARGET_P}&quot;)
  
      while IFS= read -r LINE_NOW ; do
          if [ ${REMOVE_N} -eq 1 ] ; then
              LN_NOW_WITH_N=$(awk 'BEGIN {RS=&quot;dn&quot;} {gsub(&quot;3F2C417D448C46918289218B7337FCAF&quot;, &quot;\n&quot;); printf $0}' &lt;&lt;&lt; &quot;'${LINE_NOW}'&quot;)
  
              # PROBLEM: It would be perfect if we didn't need to use the function below...
              f_preserve_blank_lines &quot;$LN_NOW_WITH_N&quot;
  
              LN_NOW_WITH_N=&quot;$F_PRESERVE_BLANK_LINES_R&quot;
              F_SPLIT_R+=(&quot;$LN_NOW_WITH_N&quot;)
          else
              F_SPLIT_R+=(&quot;$LINE_NOW&quot;)
          fi
      done &lt;&lt;&lt; &quot;$SPLIT_NOW&quot;
  }
  </code></pre>
  <h3>Usage</h3>
  <pre><code>read -r -d '' FILE_CONTENT &lt;&lt; 'HEREDOC'
  BEGIN
  15
  
  It may also be helpful to note (though understandably you had no room to do so) that the -d option to readarray first appears in Bash 4.4. – 
  fbicknel
   Aug 18, 2017 at 15:57
  4
  
  Great answer (+1). If you change your awk to awk '{ gsub(/,[ ]+|$/,&quot;\0&quot;); print }' ./  and eliminate that concatenation of the final &quot;, &quot; then you don't have to go through the gymnastics on eliminating the final record. So: readarray -td '' a &lt; &lt;(awk '{ gsub(/,[ ]+/,&quot;\0&quot;); print; }' &lt;&lt;&lt;&quot;$string&quot;) on Bash that supports readarray. Note your method is Bash 4.4+ I think because of the -d in readarray – 
  dawg
   Nov 26, 2017 at 22:28 
  10
  
  Wow, what a brilliant answer! Hee hee, my response: ditched the bash script and fired up python! – 
  artfulrobot
   May 14, 2018 at 11:32
  11
  
  I'd move your right answers up to the top, I had to scroll through a lot of rubbish to find out how to do it properly :-) – 
  paxdiablo
   Jan 9, 2020 at 12:31
  44
  
  This is exactly the kind of thing that will convince you to never code in bash. An astoundingly simple task that has 8 incorrect solutions. Btw, this is without a design constraint of, &quot;Make it as obscure and finicky as possible&quot;
  END
  HEREDOC
  FILE_CONTENT=&quot;${FILE_CONTENT:6:-3}&quot;
  DELIMITER_P=&quot;int }' ./  and eliminate&quot;
  f_split &quot;$FILE_CONTENT&quot; &quot;$DELIMITER_P&quot;
  LENGTH=${#F_SPLIT_R[*]}
  for ((i=0;i&lt;=$(($LENGTH-1));i++)); do
      echo &quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&quot;
      echo &quot;${F_SPLIT_R[$i]}&quot;
      echo &quot;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&quot;
  done
  </code></pre>