1

I need to read a file into an array. Then store in a new array only the first column of each line

example file:

aa,1,2,3
bb,4,5,2
cc,7,1,4

mapfile -t arrFile < file

so in arrFile, I got all the rows

${arrFile[0]} , returns 'aa,1,2,3'

echo ${arrFile[0]} | cut -d ";" -f1 returns 'aa'

How can I copy the firstcolumns from arrFile in another array, possibly without looping in a while

1
  • No need for external commands like cut. Bash (even Bourne shell) has suffix removal, using ${var%%<pattern>}} Commented Nov 8, 2017 at 14:32

1 Answer 1

1

Why copy? Perhaps it is enough if you simply use ${arrFile[0]%%,*} ?

Or you can copy, using arr2=(${arrFile[@]%%,*})

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.