Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SHELL

split string in shell

#!/usr/bin/env bash

# There are different method of splitting a string.
# Two of those methods are shown below

# a sample string delimeted by ";"
IN="FirstName=John; LastName=Doe; Email=jd@someone.com"

# First method:
# splits the string on ';' and puts them into an array
person=$(echo $IN | tr ";" "
")

# you can loop through the array
for info in $person
do
    echo "> [$info]"
done



# Second method:
echo "$IN" | cut -d ";" -f 1  # returns the first part
echo "$IN" | cut -d ";" -f 2  # returns the second part

#and so on.

Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #split #string #shell
ADD COMMENT
Topic
Name
4+3 =