#] #] ********************* #] "$d_SysMaint"'Linux/readarray notes.txt' - ??? # www.BillHowell.ca ??Mar2023 initial # view in text editor, using constant-width font (eg courier), tabWidth = 3 #48************************************************48 #24************************24 # Table of Contents, generate with : # $ grep "^#]" "$d_SysMaint"'Linux/readarray notes.txt' | sed "s/^#\]/ /" # #24************************24 # Setup, ToDos, #08********08 #] ??Mar2023 #08********08 #] ??Mar2023 #08********08 #] 16Mar2023 search 'Linux bash and how do I copy an array?' b=("${a[@]}") +-----+ https://stackoverflow.com/questions/19417015/how-to-copy-an-array-in-bash How to copy an array in Bash? Asked 9 years, 5 months ago Modified 7 months ago Viewed 91k times +--+ a=(foo bar "foo 1" "bar two") #create an array b=("${a[@]}") #copy the array in another one for value in "${b[@]}" ; do #print the new array echo "$value" done answered Jun 20, 2014 at 8:44 user1088530 4 Is it ok to just write b=(${a[@]}) ? – Indicator Nov 15, 2015 at 23:16 16 try to execute the command with your modifications , you will undestand why it's not correct to write without quoting... the value "foo 1" will not displayed like it shoudl but it will create more value for the array foo AND 1 , bar AND two , instead of "foo 1" and "bar two" – user1088530 Dec 25, 2015 at 20:19 #08********08 #] 16Mar2023 readarray -t fndL611 <"$pinn", want a txtL from readarray - NOT an evaluation pinn="$d_bin"'0_test/fileops/pinn_sedFndGetPut_pout/pSedFnd.txt' adapted from fileops.sh : $ readarray -t fndL611 < <( "$pFnd611" ) $ printf "%s\n" "${fndL611[@]}" >> no workee Try : $ readarray -t fndL611 <"$pinn" $ printf "%s\n" "${fndL611[@]}" echo "$orgStk611[i611]" | sed 's|^[a-zA-Z0-9]\(.*\)||' if [ "$orgStk611[i611]" == '' ]; then echo ''; else echo 'bad'; fi echo "$orgStk611[i611]" | sed 's|^[a-zA-Z0-9]\(.*\)||' if [ "$orgStk611[i611]" == '' ]; then echo ''; else echo 'bad'; fi echo "$orgStk611[i611]" | sed 's|^[a-zA-Z0-9]\(.*\)||' if [ "$orgStk611[i611]" == '' ]; then echo ''; else echo 'bad'; fi >> OK, that does it! $ nFnd611=${#fndL611[@]} $ echo "nFnd: $nFnd611" echo '' #08********08 #] 16Mar2023 search 'Linux readarray and how do I get a list of text without evaluation?' #] read -a arr < demo.txt +-----+ https://stackoverflow.com/questions/26634978/how-to-use-readarray-in-bash-to-read-lines-from-a-file-into-a-2d-array How to use 'readarray' in bash to read lines from a file into a 2D array Asked 8 years, 4 months ago Modified 3 years, 2 months ago Viewed 130k times +--+ This is the expected behavior. readarray will create an array where each element of the array is a line in the input. If you want to see the whole array you need to use echo "${myarray[@]}" as echo "$myarray will only output myarray[0], and ${myarray[1]} is the second line of the data. What you are looking for is a two-dimensional array. See for instance this : https://stackoverflow.com/questions/16487258/how-to-declare-2d-array-in-bash If you want an array with the content of the first line, you can do like this: $ read -a arr < demo.txt $ echo ${arr[0]} 1 $ echo ${arr[1]} 2 $ echo ${arr[2]} 3 edited May 23, 2017 at 11:46 CommunityBot 111 silver badge answered Oct 29, 2014 at 16:00 damienfrancois +--+ How can accesses each line separately and in that line get access to each member? Per the Bash Reference Manual, Bash provides one-dimensional indexed and associative array variables. So you cannot expect matrix[1][2] or similar to work. However, you can emulate matrix access using a bash associative arrays, where the key denotes a multiple dimension. For example, matrix[1,2] uses the string "1,2" as the associative array key denoting the 1st row, 2nd column. Combining this with readarray: typeset -A matrix function load() { declare -a a=( $2 ) for (( c=0; c < ${#a[@]}; c++ )) do matrix[$1,$c]=${a[$c]} done } readarray -C load -c 1 <<< $'1 2 3\n4 5 6\n7 8 9' declare -p matrix edited May 23, 2017 at 12:17 CommunityBot 111 silver badge answered Oct 13, 2016 at 16:00 bishop #08********08 #] 16Mar2023 man readarray https://helpmanual.io/builtin/readarray/ readarray • man page Read lines from the standard input into the indexed array variable array, or... MailchimpGet personalized content recommendations to make your emails more engaging. Sign up for Mailchimp today. readarray NAME readarray - Read lines from the standard input into the indexed array variable array, or from file descriptor fd if the -u option is supplied SYNOPSIS logout Exit a login shell. mapfile [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array] readarray [-d delim] [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array] DESCRIPTION Read lines from the standard input into the indexed array variable array, or from file descriptor fd if the -u option is supplied. The variable MAPFILE is the default array. Options, if supplied, have the following meanings: -d The first character of delim is used to terminate each input line, rather than newline. -n Copy at most count lines. If count is 0, all lines are copied. -O Begin assigning to array at index origin. The default index is 0. -s Discard the first count lines read. -t Remove a trailing delim (default newline) from each line read. -u Read lines from file descriptor fd instead of the standard input. -C Evaluate callback each time quantum lines are read. The -c option specifies quantum. -c Specify the number of lines read between each call to callback. If -C is specified without -c, the default quantum is 5000. When callback is evaluated, it is supplied the index of the next array element to be assigned and the line to be assigned to that element as additional arguments. callback is evaluated after the line is read but before the array element is assigned. If not supplied with an explicit origin, mapfile will clear array before assigning to it. mapfile returns successfully unless an invalid option or option argument is supplied, array is invalid or unassignable, or if array is not an indexed array. SEE ALSO This is extracted from the main bash man page, see there for more details. #08********08 #] 01Mar2023 search 'Linux bash and how do I assign the lines of a file to an array?' readarray +-----+ https://stackoverflow.com/questions/9449417/how-do-i-assign-the-output-of-a-command-into-an-array How do I assign the output of a command into an array? Asked 11 years ago Modified 12 days ago Viewed 117k times +--+ @Charles Duffy linked the Bash anti-pattern docs in a comment, and those give the most correct answer: readarray -t arr < <(grep -n "search term" file.txt | sed 's/:.*//') His comment: Note that array=( $(command) ) is considered an antipattern, and is the topic of BashPitfalls #50. – Charles Duffy Nov 16, 2020 at 14:07 answered Feb 16 at 21:13 Asfand Qazi YES!! readarray -t lineLOld146 < <( cat "$pOld146" ) # | sed '1~2d' In GNU sed, the address n~m addresses every m:th line starting at line n. This is a GNU extension to standard sed, for convenience. num146=${#lineLOld146[@]} echo "numTotal: $num146" for (( i146=0; i146<${num146}; i146++ )); do echo "lineOld[$i146]: ${lineLOld146[$i146]}" done result : $ bash "$d_bin"'fileops run.sh' /home/bill/web/bin/fileops.sh: line 843: $: command not found /home/bill/web/bin/fileops.sh: line 844: No: command not found /home/bill/web/Bill Howells videos/Birkeland rotation in galaxy - not dark matter/Birkeland rotation in galaxy - not dark matter.html numTotal: 9 lineOld[0]: /home/bill/web/Bill Howells videos/Birkeland rotation in galaxy - not dark matter/Birkeland rotation in galaxy - not dark matter.html lineOld[1]: /home/bill/web/economics, markets/PE Schiller forward vs 10yr Tbills/S&P 500 Shiller-forward PE versus 10y Treasury bond rates.html lineOld[2]: /home/bill/web/page hosted subsites.html lineOld[3]: /home/bill/web/Projects - major/Charvatova solar inertial motion & activity/_Charvatova - solar inertial motion & activity.html lineOld[4]: /home/bill/web/Projects - major/Civilisations and sun/_Civilisations and the sun.html lineOld[5]: /home/bill/web/webOther/Neil Howell/_Neil Howell.html lineOld[6]: /home/bill/web/Projects - major/Solar modeling and forecasting/_Solar modeling & forecasting.html lineOld[7]: /home/bill/web/Projects - mini/Galactic rays and evolution/_Galactic rays and evolution - life, the mind, civilisation, economics, financial markets.html lineOld[8]: /home/bill/web/Projects - mini/Mythology/Cardona - gods are planets, planets are gods (plus Sun).html # enddoc