This script should do what you want:
#!/bin/bash
i=1
s=1
declare -a arr
while 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 < file
for i in "${arr[@]}"
do
echo "================"
echo "$i"
done
Test file:
$ cat file
asdf dsf s dfsdaf s
sadfds fdsa fads f dsaf as
fdsafds f dsf ds afd f saf dsf
sdfsfs dfadsfsaf
sdfsafds fdsafads fd saf adsfas
sdfdsfds fdsfd saf dsa fds fads f
Output:
================
asdf dsf s dfsdaf s
sadfds fdsa fads f dsaf as
================
fdsafds f dsf ds afd f saf dsf
sdfsfs dfadsfsaf
================
sdfsafds fdsafads fd saf adsfas
sdfdsfds fdsfd saf dsa fds fads f
Update:
In order to ignore lines beginning with #
, you can add this line after the do
:
[[ $line =~ ^# ]] && continue