help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: mapfile question: Is there a better way to avoid the trailing newlin


From: Greg Wooledge
Subject: Re: mapfile question: Is there a better way to avoid the trailing newline?
Date: Wed, 1 Oct 2025 15:28:47 -0400
User-agent: Mutt/1.10.1 (2018-07-13)

On Wed, Oct 01, 2025 at 13:15:41 -0600, Stan Marsh wrote:
> >> --- Cut Here ---
> >> $ foo=$'this is a\ttest of this\tthat one'
> >> $ declare -p foo
> >> declare -- foo=$'this is a\ttest of this\tthat one'
> >> $ mapfile -t -d $'\t' <<< "$foo"; declare -p MAPFILE
> >> declare -a MAPFILE=([0]="this is a" [1]="test of this" [2]=$'that one\n')
> >> $
> >> --- Cut Here ---
> >> 
> >> Note the trailing newline on the last element of MAPFILE.
> 
> >This looks like an X-Y question, honestly.  You're reading a tab-delimited
> >stream, but you're using <<< to feed that stream to your reader, and
> >the <<< is adding a newline.
> 
> It actually is not an X-Y problem.
> My data really is: field<tab>field<tab>field

I would bet money you're parsing a Comma-Separated Value (csv) file.

You're probably doing something like this:

    while IFS= read -r line; do
        mapfile -t -d $'\t' fields <<< "$line"
    done

I strongly recommend against doing this.  It can't handle anything but
the most trivial csv files -- no quoting, no embedded newlines within
fields, etc.

There's a loadable builtin named csv that may or may not be useful for
real world csv parsing.  I've never tried it.

My preferred solution would be to rewrite the application in a language
that has a proper csv library.

If you *absolutely* must do this in bash, and can't use the loadable
builtin, then at least you can replace <<< with <(printf %s as already
shown.  It'll still fail on all nontrivial csv files.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]