Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Read file with escaped newlines into array



On 19 November 2015 at 20:17, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Nov 19,  6:34pm, Sebastian Gniazdowski wrote:
> }
> } fc -W stores history to a given file. It escapes newlines with \. How
> } to read such file into array and have "\^M" lines put back together
> } into single array entries?
>
> The best way is probably to use the history mechanism to read it:
>
>     () { fc -ap -R the_given_file ; the_desired_array=( ${history} ) }
>
> Although the docs don't yet say so, the special associative array
> $history is not implemented as a hash table, so the entries are
> guaranteed to be in a fixed order from most recent (largest history
> number) to longest ago.  Which is the reverse of the way you'd read
> them from the file, so you might want ${(Oa)the_desired_array} when
> referencing.

The way $history is implemented is crucial to me and solves my
problem. I needed ordered history started from most recent, without
"\n" substituted for newlines, and "$history[@]" does that. Thanks.
This could be mentioned in documentation just to protect that
functionality

Best regards,
Sebastian Gniazdowski



Messages sorted by: Reverse Date, Date, Thread, Author