1

So i need to read all the lines from a text file(as an argument when i call the script) which contains numbers in this form(1 new line not 2):
num1:num2

num3:num4 etc

I use this command block:

while IFS= read line do IFS=':' read -r -a X <<< "$line" done < "$1" 

to read the lines and numbers and store it into array X but the array goes only to position 0 and 1 and when it changes line it just write the new number(eg num3) where the old number was(eg num1 in pos 0)

Any solution to this?

1
  • Insert before your code array=() and before done insert array+=( "${X[@]}" ) to append array X to array array. Commented Oct 12, 2019 at 8:52

1 Answer 1

2

With bash. Replace all : with line break and use mapfile to fill array x.

mapfile -t x < <(tr ':' '\n' < file) declare -p x 

Output:

 declare -a x='([0]="num1" [1]="num2" [2]="num3" [3]="num4")' 

See: help mapfile

Sign up to request clarification or add additional context in comments.

2 Comments

And what if i want to store the first number in one array and the second in a different array Y in each line
I recommend that you ask a new question (no comment) about this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.