Robby75
Robby75

Reputation: 3455

How to tokenize string in dash

I read data from a string that is formatted like firstfield|secondfield|thirdfield, in bash I use this construct to achieve this:

i="firstfield|secondfield|thirdfield"
defaultIFS=$IFS
IFS="|"
set -- $i
arr=( $i )
IFS=$defaultIFS

FIRST=${arr[0]}
SECOND=${arr[1]}
THIRD=${arr[2]}

Is it possible to get $FIRST, $SECOND and $THIRD in dash without using external programs like awk/sed?

Upvotes: 1

Views: 374

Answers (3)

zrajm
zrajm

Reputation: 1391

Scrutinizer's read method works, but you can also use set to set the positional parameters ($1, $2 etc.) which is about as close to an array you can get in dash. If you do this cleverly (using local) it can be quite useful.

Heres a little function, that splits $STRING on $SEPARATOR and then evals $CODE for each token. The variable $TOKEN is set when the eval is called, so you can use that. With a modification you could give it the token position as well. (And if you do anything advanced in your $CODE you might want to to reset $IFS as well.)

# Usage: foreach_token SEPARATOR STRING CODE
foreach_token() {
    local IFS="$1" STRING="$2" CODE="$3" TOKEN    # get args
    set -- $STRING                                # split $STRING on $IFS
    for TOKEN; do                                 # foreach $@
        eval "$CODE"
    done
}

Call the function like this:

foreach_token "|" "firstfield|secondfield|thirdfield" 'echo "$TOKEN"'

Upvotes: 0

Maxim Mai
Maxim Mai

Reputation: 157

cut with the option of -d often does the job.

i="firstfield|secondfield|thirdfield"  
echo $i | cut -d "|" -f1

-d "|" means to tokenize the string with delimiter "|", then -f1 gets the first field, -f2 gets the second filed, -f3 gets the third field.

Upvotes: 0

Scrutinizer
Scrutinizer

Reputation: 9936

You can use this, which would also work in bash:

IFS='|' read FIRST SECOND THIRD dummy << EOF
$i
EOF

If there are always exactly 3 fields, you do not need the dummy variable. Note that there is no need to reset the IFS variable, since it is used local to the read command.

Upvotes: 1

Related Questions