Zsh: read lines from file into array

痴心易碎 提交于 2019-12-21 17:23:40

问题


I'm trying to read in a file as an array of lines and then iterate over it zsh, and the code I've got works most of the time, except if the input file contains certain characters (such as brackets). Here's a snippet of it:

#!/bin/zsh
LIST=$(cat /path/to/some/file.txt)
SIZE=${${(f)LIST}[(I)${${(f)LIST}[-1]}]}
POS=${${(f)LIST}[(I)${${(f)LIST}[-1]}]}
while [[ $POS -le $SIZE ]] ; do
    ITEM=${${(f)LIST}[$POS]}
    # Do stuff
    ((POS=POS+1))
done

What would be an easier way of doing this? I also need to count the number of lines in the file.


回答1:


#!/bin/zsh
zmodload zsh/mapfile
FNAME=/path/to/some/file.txt
FLINES=( "${(f)mapfile[$FNAME]}" )
LIST="${mapfile[$FNAME]}" # Not required unless stuff uses it
integer POS=1             # Not required unless stuff uses it
integer SIZE=$#FLINES     # Number of lines, not required unless stuff uses it
for ITEM in $FLINES
    # Do stuff
    (( POS++ ))
endfor

You have some strange things in your code:

  1. Why are you splitting LIST each time instead of making it an array variable? It is just a waste of CPU time.
  2. Why don’t you use for ITEM in ${(f)LIST}?
  3. There is a possibility to directly ask zsh about array length: $#ARRAY. No need in determining the index of the last occurrence of the last element.
  4. POS gets the same value as SIZE in your code. Hence it will iterate only once.
  5. Brackets are problems likely because of 3.: (I) is matching against a pattern. Do read documentation.



回答2:


I know it's been a lot of time since the question was answered but I think it's worth posting a simpler answer (which doesn't require the zsh/mapfile external module):

#!/bin/zsh
for line in "${(@f)"$(</path/to/some/file.txt)"}"
{
  // do something with each $line
}


来源:https://stackoverflow.com/questions/12651355/zsh-read-lines-from-file-into-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!