|
Home > Archive > Unix Shell > October 2005 > [ksh] Parsing line-delimited word list into CSV
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
[ksh] Parsing line-delimited word list into CSV
|
|
| travis.james@gmail.com 2005-10-31, 8:50 pm |
| This following snip from a script has been used to take a list of words
in a file and convert them to CSV.
The problem is that if a word on a line has a space, it breaks that
into 2 words.
Instead of: one, two three, four [which is what I want]
I get: one, two, three, four
I've mucked around with $IFS and some variations using "read" which
caused other nightmares.
Anyone see a simple tweak? Thanks.
== File: words.txt ==
one
two three
four
== Script: word_test.ksh ==
#!ksh
#
WORDFILE=words.txt
WORDS=
convertWords()
{
for inParms
do
if [ $WORDS ]
then
WORDS=$WORDS,$inParms
else
WORDS=$inParms
fi
done
}
convertWords `cat $WORDFILE`
echo $WORDS
| |
| Dan Mercer 2005-10-31, 8:50 pm |
|
<travis.james@gmail.com> wrote in message news:1130804613.728612.176040@g47g2000cwa.googlegroups.com...
: This following snip from a script has been used to take a list of words
: in a file and convert them to CSV.
:
: The problem is that if a word on a line has a space, it breaks that
: into 2 words.
: Instead of: one, two three, four [which is what I want]
: I get: one, two, three, four
:
: I've mucked around with $IFS and some variations using "read" which
: caused other nightmares.
:
: Anyone see a simple tweak? Thanks.
:
: == File: words.txt ==
: one
: two three
: four
:
: == Script: word_test.ksh ==
: #!ksh
: #
:
: WORDFILE=words.txt
:
: WORDS=
:
: convertWords()
: {
: for inParms
: do
: if [ $WORDS ]
: then
: WORDS=$WORDS,$inParms
: else
: WORDS=$inParms
: fi
: done
: }
:
: convertWords `cat $WORDFILE`
:
: echo $WORDS
function convertWords
{
# define IFS locally so it won't affect global setting
typeset IFS ifs=$IFS
typeset file=${1:?Filename required}
typeset arr
IFS=${IFS#??} # set IFS to newline
set -A arr -- $(<file) # read file into array
IFS=, # set IFS to comma
print -- "${arr[*]}" # print the contents of the array separated by first char of IFS
}
Dan Mercer
:
| |
| Chris F.A. Johnson 2005-10-31, 8:50 pm |
| On 2005-11-01, travis.james@gmail.com wrote:
>
> This following snip from a script has been used to take a list of words
> in a file and convert them to CSV.
>
> The problem is that if a word on a line has a space, it breaks that
> into 2 words.
> Instead of: one, two three, four [which is what I want]
> I get: one, two, three, four
>
> I've mucked around with $IFS and some variations using "read" which
> caused other nightmares.
>
> Anyone see a simple tweak? Thanks.
>
> == File: words.txt ==
> one
> two three
> four
>
> == Script: word_test.ksh ==
> #!ksh
> #
>
> WORDFILE=words.txt
>
> WORDS=
>
> convertWords()
> {
> for inParms
> do
> if [ $WORDS ]
> then
> WORDS=$WORDS,$inParms
> else
> WORDS=$inParms
> fi
> done
> }
>
> convertWords `cat $WORDFILE`
This is hardly ever the correct way to read a file (though it can
work if you redefine IFS to a newline); it makes each word in the
file a separate argument.
while IFS= read -r line
do
WORDS=${WORDS:+"$WORDS",}$line
done < "$WORDFILE"
printf "%s\n" "$WORDS"
> echo $WORDS
echo "$WORDS"
:::: If posting through Google Groups, please see:
:::: <http://cfaj.freeshell.org/google>
--
Chris F.A. Johnson | Author:
<http://cfaj.freeshell.org> | Shell Scripting Recipes:
Any code in this message is released | A Problem-Solution Approach,
under the GNU General Public Licence | 2005, Apress
| |
| John W. Krahn 2005-10-31, 8:50 pm |
| travis.james@gmail.com wrote:
> This following snip from a script has been used to take a list of words
> in a file and convert them to CSV.
>
> The problem is that if a word on a line has a space, it breaks that
> into 2 words.
> Instead of: one, two three, four [which is what I want]
> I get: one, two, three, four
>
> I've mucked around with $IFS and some variations using "read" which
> caused other nightmares.
>
> Anyone see a simple tweak? Thanks.
>
> == File: words.txt ==
> one
> two three
> four
perl -l054pe1 words.txt
John
--
use Perl;
program
fulfillment
| |
| Chris F.A. Johnson 2005-10-31, 8:50 pm |
| On 2005-11-01, Chris F.A. Johnson wrote:
> On 2005-11-01, travis.james@gmail.com wrote:
>
> while IFS= read -r line
> do
> WORDS=${WORDS:+"$WORDS",}$line
> done < "$WORDFILE"
> printf "%s\n" "$WORDS"
>
>
> echo "$WORDS"
Or:
WORDS=$( tr '\012' ',' < "$WORDFILE" )
WORDS=${WORDS%,}
--
Chris F.A. Johnson | Author:
<http://cfaj.freeshell.org> | Shell Scripting Recipes:
Any code in this post is released | A Problem-Solution Approach,
under the GNU General Public Licence | 2005, Apress
|
|
|
|
|