Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

How to split a string in bash

string="you got a friend in me"
IFS=' ' read -ra split <<< "$string"
echo "${split[*]}"
# Output: you got a friend in me
echo "${split[3]}"
# Output: friend
Comment

split string in shell script

IN="bla@some.com;john@home.com"
arrIN=(${IN//;/ })
echo ${arrIN[1]}                  # Output: john@home.com
Comment

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.

Comment

split sh string

#YOURSTRING="this_is_an_example"
#output:
#this
#is
#an
#example
for i in $(echo $YOURSTRING | tr "_" "
")
do
	echo $i
done
Comment

split bash string

IN="bla@some.com;john@home.com"
arrIN=(${IN//;/ })
Comment

split string using linux cmd

$ s='one_two_three_four_five'

$ A="$(cut -d'_' -f2 <<<"$s")"
$ echo "$A"
two

$ B="$(cut -d'_' -f4 <<<"$s")"
$ echo "$B"
four
Comment

PREVIOUS NEXT
Code Example
Shell :: get Operating system command 
Shell :: linux check core count 
Shell :: delete logs older than 7 days linux 
Shell :: soundcloud for ubuntu install command 
Shell :: vendor/autoload.php download 
Shell :: linux count number of times word appears in file 
Shell :: git SSL certificate problem: self signed certificate 
Shell :: shell set environment variable 
Shell :: gatsby-plugin-react-helmet npm 
Shell :: install yarm for redhat linux 
Shell :: change user linux 
Shell :: ubuntu docker host ip 
Shell :: find text in all files linux 
Shell :: blackeye linux 
Shell :: git log my commits 
Shell :: login to github vscode using personal access tokens 
Shell :: how to set environment variable in linux permanently 
Shell :: setup wordpress on ubuntu 
Shell :: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root? 
Shell :: pdf pages to images imagemagick 
Shell :: powershell create file content 
Shell :: batch script if statement 
Shell :: edit file as root ubuntu 
Shell :: git check merged branches 
Shell :: split sh string 
Shell :: body-parser use 
Shell :: how to remove all files from staging area git 
Shell :: ssh and execute command in one line 
Shell :: localhost wsl 
Shell :: git pull shows already up to date 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =