sh - split string by delimiter

13,116

Solution 1

You seem to be using sh to execute the script. Herestrings aren't supported in sh; hence the error.

Ensure that you're using bash to execute the script.

Solution 2

A here string is just a shortcut for a small here document. This should work in any POSIX shell:

s='id;some text here with possible ; inside'
IFS=';' read -r id string <<EOF
$s
EOF
echo "$id"

Solution 3

save it in file split

#!/bin/sh

string=$1
c=0

while [ $c -le $3 ] 
do

val1=`echo "$string" |sed 's/\(^[^'$2']*\)\('$2'\+\)\(.*$\)/\1/'`
string=`echo "$string" |sed 's/\(^[^'$2']*\)\('$2'\+\)\(.*$\)/\3/'`

if [ "$val1" == "$string" ]
then
string=''
fi

if [ "$val1" == "" ]
then
let c=$3
fi

let c=c+1

done

echo $val1 

and it work:

root@IPHONE:~# ./split 'a;b;c' ';' 0
a
root@IPHONE:~# ./split 'a/b/c' '\/' 0
a
root@IPHONE:~# ./split 'a/b/c' '\/' 2
c
root@IPHONE:~# ./split 'a/b/c' '\/' 1
b
Share:
13,116
clarkk
Author by

clarkk

https://dynaccount.dk https://dynaccount.dk/bogfoeringsprogram/ https://dynaccount.dk/regnskabsprogram/

Updated on September 05, 2022

Comments

  • clarkk
    clarkk over 1 year

    code

    s='id;some text here with possible ; inside'
    IFS=';' read -r id string <<< "$s"
    echo "$id"
    

    error

    restore.sh: 2: restore.sh: Syntax error: redirection unexpected
    

    bash version GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu)

  • clarkk
    clarkk over 10 years
    what is the difference between sh and bash?
  • Radu Rădeanu
    Radu Rădeanu over 10 years
  • chepner
    chepner over 10 years
    Some Linux distributions use dash (not bash) as the shell that implements sh. dash is (mostly?) limited to the features specified by the POSIX specification, without the extra features like <<< implemented by bash.
  • clarkk
    clarkk over 10 years
    how can I then do this operation with sh?
  • devnull
    devnull over 10 years
    @clarkk Right; as mentioned in the answer herestrings aren't supported in sh.
  • devnull
    devnull over 10 years
    @clarkk Maybe you could use cut. echo "$s" | cut -d; -f1 would return the first field.
  • clarkk
    clarkk over 10 years
    this works echo "$s" | cut -d";" -f1 but how can I assign it into a variable?
  • anishsane
    anishsane over 10 years
    @clarkk: Use var=$(echo "$s" | cut -d";" -f1)
  • Jess
    Jess about 5 years
    Am new to shell but from Java background. Can you explain your code ?
  • yassen
    yassen over 2 years
    @Jess: very late comment but anyway, my two cents: POSIX read expects a line on standard input, reads that and splits it using $IFS contents as a separator, then assigns each chunk to variables given as arguments to read (id and string; id getting the first chunk and string getting all the remaining chunks). The trick with the two EOFs is called a "heredoc" and AFAIK is the only way to send that string to read's standard input. See also pubs.opengroup.org/onlinepubs/9699919799/utilities/read.html‌​.