Re: Parsing a char array w/fields delimited by /0xA
- From: William Park <opengeometry@xxxxxxxx>
- Date: Wed, 13 Sep 2006 11:50:48 -0400
Stephane Chazelas <stephane_chazelas@xxxxxxxx> wrote:
On Tue, 12 Sep 2006 20:16:36 -0400, William Park wrote:
Hufnus <tonyb@xxxxxxxxxx> wrote:
I have been crackin my head trying to write a bash script
to break a large string with a bunch of concatenated variable
length strings separated by linefeeds (0x0A), into an array.
$string -> $field[i] i=0...n
Key insight:
This is "split/join" operation which you see a lot in Python, Perl,
Ruby, ... context.
In Bash, it's even simpler with the right C extension, :-)
field=( "${string|,$'\n'}" )
In zsh:
field=(${(f)string})
(shortcut for field=(${(ps:\n:)string}))
However, this discards the empty tokens.
IFS=$'\n\n'
field=($=string)
Would preserve them.
This is slightly more standard than your bash extension as zsh
can be found on more than one computer in the world, but again
better is to use standard/open syntax and use the proper tool.
If you need arrays, you probably don't want a shell, use
perl/python/ruby...
If you're going to use a shell, do it the shell way, using
filters working concurrently.
Not sure if Zsh has this feature, but OP may want to experiment with
while read; do ... done <<< "$string"
since this is essentially reading line by line.
--
William Park <opengeometry@xxxxxxxx>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive
http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell
http://freshmeat.net/projects/bashdiff/
.
- Follow-Ups:
- Re: Parsing a char array w/fields delimited by /0xA
- From: Stephane Chazelas
- Re: Parsing a char array w/fields delimited by /0xA
- References:
- Parsing a char array w/fields delimited by /0xA
- From: Hufnus
- Re: Parsing a char array w/fields delimited by /0xA
- From: William Park
- Re: Parsing a char array w/fields delimited by /0xA
- From: Stephane Chazelas
- Parsing a char array w/fields delimited by /0xA
- Prev by Date: Korn Shell Script
- Next by Date: Re: Korn Shell Script
- Previous by thread: Re: Parsing a char array w/fields delimited by /0xA
- Next by thread: Re: Parsing a char array w/fields delimited by /0xA
- Index(es):
Relevant Pages
|