Split text file into array based on an empty line or any non used character Split text file into array based on an empty line or any non used character bash bash

Split text file into array based on an empty line or any non used character


This script should do what you want:

#!/bin/bashi=1s=1declare -a arrwhile read -r line do    # If we find an empty line, then we increase the counter (i),     # set the flag (s) to one, and skip to the next line    [[ $line == "" ]] && ((i++)) && s=1 && continue     # If the flag (s) is zero, then we are not in a new line of the block    # so we set the value of the array to be the previous value concatenated    # with the current line    [[ $s == 0 ]] && arr[$i]="${arr[$i]}$line" || {             # Otherwise we are in the first line of the block, so we set the value            # of the array to the current line, and then we reset the flag (s) to zero             arr[$i]="$line"            s=0;     }done < filefor i in "${arr[@]}"do   echo "================"   echo "$i"done 

Test file:

$ cat fileasdf dsf s dfsdaf ssadfds fdsa fads f dsaf asfdsafds f dsf ds afd f saf dsfsdfsfs dfadsfsafsdfsafds fdsafads fd saf adsfassdfdsfds fdsfd saf dsa fds fads f

Output:

================asdf dsf s dfsdaf ssadfds fdsa fads f dsaf as================fdsafds f dsf ds afd f saf dsfsdfsfs dfadsfsaf================sdfsafds fdsafads fd saf adsfassdfdsfds fdsfd saf dsa fds fads f

Update:

In order to ignore lines beginning with #, you can add this line after the do:

[[ $line =~ ^# ]] && continue


First of all, by design, variables set with var=foo command are only made available to command and won't be set for the rest of the script.

As for your problem, read reads a record until the first delimiter (-d, default: line feed), and then splits that up into fields by $IFS.

To loop over your items, you can use

sed -e 's/^$/\xBF/' | while read -d $'\xBF' vardo    printf "Value: %s\n-----\n" "$var"done

To read them all into an array from a string, you can read up until some character you hopefully don't have, like a NUL byte:

IFS=$'\xBF' read -d '' -a array <<< "$var"