[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] How to split fields like awk with FS="\t"? (read)
From: |
Stephane Chazelas |
Subject: |
Re: [Help-bash] How to split fields like awk with FS="\t"? (read) |
Date: |
Fri, 15 May 2015 12:18:22 +0100 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
2015-05-14 21:00:37 -0500, Peng Yu:
> Hi, The following code shows that continuos TAB is treated as single
> field separator as explained in "Word Splitting". But such a
> definition is none intuitive. Sometime people what to treat each TAB
> as a separator. Is there a way to do so in bash? Thanks.
>
> IFS=$'\t' read -r -d $'\n' v x y z < <(printf "%s\t%s\t%s\t%s\n" a b '' c)
> echo $v
> echo $x
> echo $y
> echo $z
[...]
In zsh or ksh93, you can be doubling \t in $IFS, not in bash
yet.
$ bash -c $'IFS="\t\t"; read -r a b c; echo "$c"' <<< $'a\t\tb\tc'
c
$ zsh -c $'IFS="\t\t"; read -r a b c; echo "$c"' <<< $'a\t\tb\tc'
b c
$ ksh93 -c $'IFS="\t\t"; read -r a b c; echo "$c"' <<< $'a\t\tb\tc'
b c
Note that in bash or ksh93, contrary to awk or zsh, IFS is field
delimiter, not field separator, so foo:bar: with IFS=: is split
into "foo" and "bar" only, not "foo", "bar" and "".
--
Stephane