How do I split a text file into an array by blank lines?

C J

I have a bash command that outputs text in the following format:


Header 1
- Point 1
- Point 2

Header 2
- Point 1
- Point 2

Header 3
-Point 1
- Point 2

...

I want to parse this text into an array, separating on the empty line so that array[0] for example contains:

Header 1
- Point 1
- Point 2

And then I want to edit some of the data in the array if it satisfies certain conditions.

I was looking at something like this Separate by blank lines in bash but I'm completely new to bash so I don't understand how to save the output from awk RS=null to an array instead of printing it out. Could someone please point me in the right direction?

anubhava

You can use readarray command to populate a bash array after reading your file with gnu awk command with empty RS that lets awk split records on empty lines and using ORS as \0 (NUL) byte:

IFS= readarray -d '' arr < <(awk -v RS= -v ORS='\0' '1' file)

Check output:

echo "${arr[0]}"
Header 1
- Point 1
- Point 2

echo "${arr[1]}"
Header 2
- Point 1
- Point 2

echo "${arr[2]}"
Header 3
-Point 1
- Point 2

Online Demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I split string into an array using blank lines as delimiter in ruby?

How do I split and name the text file (based on the no. of lines of content) for bigdata?

How Do I Ignore Blank Lines Processing a CSV File In Clojure?

How can I split a long text into array based on empty lines?

SPLIT Function: Text from a cell into an array but ignore the blank lines

How do I split a "/proc/*/environ" file in separate lines?

How do I split a record of text into text file in C++

How can I delete blank lines in a file?

How do I process text file into an array?

How do I grab the first 10% of lines in a text file?

How do I remove multiple empty lines in a text file

How do I combine lines in a text file in a specific order?

How do I count number of lines in an imported to array CSV file?

How do I Split a string by empty lines?

How do I string Split text in a text file into a text box and change the text of the radio button?

How to add N blank lines between all rows of a text file?

How to remove blank lines from text file using powershell

How to keep blank lines while reading text file Python

How to not make text file whos lines I split not have duplicate output using a set

Split file after X lines at blank line

How do I remove all the lines in a text file that are present in another text file in Windows?

Split text file into chunks of lines

Python: How do I split a .txt file into two or more files with the same number of lines in each?

How do I remove blank lines from a string in Python?

VSCode: How do I KEEP blank lines (in scss files or others)?

Is it possible to Skip Blank Lines in a Dataframe? If Yes then how I can do this

How do I remove blank lines from files?

How can I prevent my program from adding unwanted blank lines when reading and printing from text file - Python3

How do I delete all lines in text file below certain text?